将 java 转换为 Scala 代码 - 方法签名的更改 [英] convert java to scala code - change of method signatures

查看:78
本文介绍了将 java 转换为 Scala 代码 - 方法签名的更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将一些 java 转换为 Scala 代码时,我遇到了在 java 世界中编译良好的不同方法签名的问题:

Trying to convert some java to scala code I face the problem of a different method signature which compiled fine in the java world:

Java 中的以下代码(来自 https://github.com/DataSystemsLab/GeoSpark/blob/master/babylon/src/main/java/org/datasyslab/babylon/showcase/Example.java#L122-L126)

The following code in java (from https://github.com/DataSystemsLab/GeoSpark/blob/master/babylon/src/main/java/org/datasyslab/babylon/showcase/Example.java#L122-L126)

visualizationOperator = new ScatterPlot(1000,600,USMainLandBoundary,false,-1,-1,true,true);
visualizationOperator.CustomizeColor(255, 255, 255, 255, Color.GREEN, true);
visualizationOperator.Visualize(sparkContext, spatialRDD);
imageGenerator = new SparkImageGenerator();
imageGenerator.SaveAsFile(visualizationOperator.distributedVectorImage, "file://"+outputPath,ImageType.SVG);

被翻译成https://github.com/geoHeil/geoSparkScalaSample/blob/master/src/main/scala/myOrg/visualization/Vis.scala#L45-L57

val vDistributedVector = new ScatterPlot(1000, 600, USMainLandBoundary, false, -1, -1, true, true)
vDistributedVector.CustomizeColor(255, 255, 255, 255, Color.GREEN, true)
vDistributedVector.Visualize(s, spatialRDD)
sparkImageGenerator.SaveAsFile(vDistributedVector.distributedVectorImage, outputPath + "distributedVector", ImageType.SVG)

哪个会抛出以下错误:

overloaded method value SaveAsFile with alternatives:
[error]   (x$1: java.util.List[String],x$2: String,x$3: org.datasyslab.babylon.utils.ImageType)Boolean <and>
[error]   (x$1: java.awt.image.BufferedImage,x$2: String,x$3: org.datasyslab.babylon.utils.ImageType)Boolean <and>
[error]   (x$1: org.apache.spark.api.java.JavaPairRDD,x$2: String,x$3: org.datasyslab.babylon.utils.ImageType)Boolean
[error]  cannot be applied to (org.apache.spark.api.java.JavaPairRDD[Integer,String], String, org.datasyslab.babylon.utils.ImageType)
[error]     sparkImageGenerator.SaveAsFile(vDistributedVector.distributedVectorImage, outputPath + "distributedVector", ImageType.SVG)

不幸的是,我不确定如何解决这个问题/如何在 Scala 中正确调用该方法.

Unfortunately, I am not really sure how to fix this / how to properly call the method in scala.

推荐答案

这是 ImageGenerator 的问题,由 SparkImageGenerator 继承.如您所见 这里,它有一个方法

This is a problem in ImageGenerator, inherited by SparkImageGenerator. As you can see here, it has a method

public boolean SaveAsFile(JavaPairRDD distributedImage, String outputPath, ImageType imageType)

使用原始类型(JavaPairRDD 没有 <...>).它们的存在主要是为了与 Java 5 之前的代码兼容,通常不应该否则使用.对于这段代码,当然没有充分的理由,因为它实际上需要特定的类型参数.使用原始类型只会失去类型安全性.也许某些子类(当前的或潜在的)可能会覆盖它并期望不同类型的参数,但这将是对继承的滥用,必须有更好的解决方案.

which uses a raw type (JavaPairRDD without <...>). They exist primarily for compatibility with pre-Java 5 code and shouldn't normally be used otherwise. For this code, there is certainly no good reason, as it actually expects specific type parameters. Using raw types merely loses type-safety. Maybe some subclasses (current or potential) might override it and expect different type parameters, but this would be a misuse of inheritance and there must be a better solution.

Scala 不以任何方式支持原始类型,因此您不能从中调用此方法(AFAIK).作为一种解决方法,您可以在 Java 中编写一个使用正确类型的包装器,并从 Scala 调用此包装器.我没记错,它是扩展 Java 类来扩展原始类型,这是不可能的,即使这样也有解决方法.

Scala doesn't support raw types in any way and so you can't call this method from it (AFAIK). As a workaround, you could write a wrapper in Java which used correct types and call this wrapper from Scala. I misremembered, it's extending Java classes extending raw types which was impossible, and even then there are workarounds.

您也许可以通过显式类型归属(最好是强制转换)来调用它:

You might be able to call it by explicit type ascription (preferable to casting):

sparkImageGenerator.SaveAsFile(
  (vDistributedVector.distributedVectorImage: JavaPairRDD[_, _]), 
  outputPath + "distributedVector", ImageType.SVG)

但鉴于错误消息仅显示 JavaPairRDD,我并不特别希望它能正常工作.如果失败,我仍然会使用 Java 包装器.

But given the error message shows just JavaPairRDD, I don't particularly expect it to work. If this fails, I'd still go with a Java wrapper.

这篇关于将 java 转换为 Scala 代码 - 方法签名的更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆