Home  >  Article  >  类库下载  >  Java/Scala interoperability practice 1: basic operations

Java/Scala interoperability practice 1: basic operations

高洛峰
高洛峰Original
2016-10-15 10:33:472660browse

Java Bean

A characteristic of Java Bean is that there are corresponding getter and setter methods for modifiable properties (final properties will only have getter methods). Objects defined in Java can be used directly in Scala, no different. Defining Java Beans in Scala is a little different.

In fact, Java Beans can be defined in Scala just like Java:

// Scala中默认为public访问权限,包括属性和方法class Person {
  // 下划线在这里是一个占位符,它代码相应属性对应类型的默认值
  private var id: Int = _ 
  private var name: String = _;  def getId: Int = id;  def setId(id: Int) {    this.id = id
  }  def getName: String = name;  def setName(name: String) {    this.name = name;
  }
}

Written like this, except for some differences in syntax from Java, the definition method is actually the same. But in fact, Scala provides annotations to automatically generate getter and setter functions:

import scala.beans.BeanPropertyclass Person {  @BeanProperty
  var id: Int = _  @BeanProperty
  var name: String = _  @BeanProperty
  val createdAt: LocalDateTime = _
}

In addition to using traditional classes, you can also use case classes to define POJOs in Scala:

case class SignRequest(@BeanProperty
                       account: String = null,                       @BeanProperty
                       password: String = null,                       @BeanProperty
                       captcha: String = null,                       @BeanProperty
                       var smsCode: String = null)

The parameters declared by the main constructor of case class will be done at the same time It is the performance of SignRequest and is val (similar to Java's public final). Here, account, password and captcha will only generate getter functions. And smsCode will generate getter and setter functions because it is decorated with var.

Here is a feature that is not available in Java: parameter default values. Like C++, Python, and ES6+, Scala parameters can be set to default values. Because the Java Bean specification requires that a class must have a default constructor with empty parameters, and when all parameters of the main constructor of a case class are set to default values, it will be equivalent to having an empty default constructor when instantiating this class.

Call case class in Java is visible: com/hualongdata/springstarter/data/repository/UserRepositoryImpl.java.

Annotation-based dependency injection

In Spring development, dependency injection is a very commonly used feature. Property-based annotation injection is the same in both Java and Scala. But constructor-based dependency injection is somewhat special in Scala. The code is as follows:

class SignController @Autowired()(userService: UserService,
                                  webUtils: WebUtils,
                                  hlTokenComponent: HlTokenComponent) {
  ......
}

In Scala, when a single annotation acts on a constructor, it requires a method call-like form: @Autowired(). And because in Scala, the main constructor must be defined in parentheses after the class name, so the annotation needs to follow the class name and before the left bracket of the main constructor.

It is a better practice in Scala to use the injected component in the main constructor, which also has private final access to the injected component. The same effect requires more Java code:

public SignController {
    private final UserService userService;
    private final WebUtils webUtils;
    private final HlTokenComponent hlTokenComponent;

    public SignController(UserService userService, WebUtils webUtils, HlTokenComponent hlTokenComponent) {
        this.userService = userService;
        this.webUtils = webUtils;
        this.hlTokenComponent = hlTokenComponent;
    }
}

As you can see, the Scala version has less code and looks more concise.

Annotation parameters

Array parameters

@RestController
@RequestMapping(Array("/sign"))
class SignController @Autowired()(userService: UserService,
  ......

In Scala, when only one element is set for annotated array parameters, it cannot be a string like Java, and an array must be explicitly defined.

Parameter values ​​must be constants

In Scala, when a certain parameter of an annotation is a low value, a constant must be used. Forms like: @RequestMapping(Array(Constants.API_BASE + "/sign")) are illegal of. It can only be cheap like this: @RequestMapping(Array("/aip/sign"))

Variable length parameters

In Scala, variable length parameters are defined by asterisks (*), the code is as follows:

def log(format: String, value: String*)

But this The defined variable parameters are not accessible in Java, because the value type in the default implementation of Scala is: Seq[Any], and the variable parameter type in Java is actually an array (String[]). It is very simple to solve this problem. Add the scala.annotation.varargs annotation before the function definition to force Scala to use the Java implementation to implement variable-length parameters.

Collection library

Scala有自己的一套集合库实现: scala.collection,分为不可变集合scala.collection.immutable和可变集合scala.collection.mutable。两者都实现了很多高阶函数,可以简化日常编程,同时Scala中推荐使用不可变集合。

Java集合到Scala集合

Scala提供了scala.collection.JavaConverters来转换Java集合到Scala集合:

import scala.collection.JavaConverters._  /**
    * 根据sheet名获取sheet所有单元格
    *
    * @param workbook  Excel [[Workbook]]对象
    * @param sheetName sheet 名
    * @return 返回所有有效单元格可迭代二维列表
    */
  def getSheetCells(workbook: Workbook, sheetName: String): Iterable[Iterable[RichCell]] = {
      workbook.getSheet(sheetName)
        .asScala
        .map(row => row.asScala.map(cell => new RichCell(cell)))
  }

workbook.getSheet方法返回的Sheet类型是实现了java.lang.Iterable接口的可迭代类型。为了使用Scala集合上提供的map高阶函数,我们需要把Java集合转换成Scala集合。可以通过在Java集合上调用.asScala函数来将其转换成Scala集合,这里运用了Scala里的隐式转换特性来实现。

Scala集合到Java集合

接下来我们看另外一个函数:

  @varargs
  def getSheets(workbook: Workbook, sheetNames: String*): java.util.List[Sheet] = {
    sheets(workbook, sheetNames: _ *).asJava
  }

这个函数实现的功能是根据传入的一个或多个Sheet名字从Excel里获取Sheet列表。sheets函数返回的是一个Scala集合:Seq[Sheet],通过getSheets代理函数将其转换成Java集合,通过在Seq[Sheet]上调用.asJava方法来实现自动转换。同样的,这里也运用了Scala的隐式转换特性。

Java代码中做集合转换

之前的例子都是在Scala代码中实现的,通过隐式转换这一特性我们发现做Java/Scala集合的相互转换是非常方便的。但在Java代码中做两者的转换就不那么直观了,因为Java没有隐式转换这一特性,我们需要显示的调用代码来先生成包装类,再调用.asScala或.asJava方法来转换集合类型:

import scala.collection.JavaConverters$;import scala.collection.mutable.Buffer;    public static void demo() {
        List<String> list = Arrays.asList("dd", "dd");        // Java List 到 Scala Buffer
        Buffer<String> scalaBuffer = JavaConverters$.MODULE$.asScalaBufferConverter(list).asScala();        // Scala Buffer 到 Java List
        List<String> javaList = JavaConverters$.MODULE$.bufferAsJavaListConverter(scalaBuffer).asJava();
    }

为Java和Scala同时提供API

当在项目中混用Java和Scala语言时,有个问题不得不重视。提供的API是用Java还是Scala来实现?实现的API是优先考虑兼容Java还是Scala?

对于API的实现,用Java或Scala均可。若使用Java实现,在Scala中调用是基本无压力的。而使用Scala实现时,为了兼容Java你可能不得不作一些折中。一个常用的方式是:使用Scala或Java来实现API,而再用Java或Scala来实现一个封装层(代理)作兼容。比如:Spark、Akka……,它们使用Scala来实现API,但提供了包装的Java API层。

一个好的实践是把Scala API放到scalaapi包路径(或者反之把Java API放到javaapi包路径)。

若我们只提供一个API,那就要尽量同时支持Java和Scala方便的 调用。比如使用@varargs注解来修饰变长参数。

对于参数需要集合类型,或返回值为集合类型的函数。我们除了使用上一节提供的JavaConverters来做自动/手动转换以外,也可以通过装饰器形式来提供Java或Scala专有的API。这里,我推荐Scala API函数名直接使用代表操作的名词/动词实现,而Java API在之前加上:get、set、create等前缀进行修饰。

def sheets(workbook: Workbook, sheetNames: String*): Seq[Sheet] = {
    sheetNames.map(sheetName => workbook.getSheet(sheetName))
  }  @varargs
  def getSheets(workbook: Workbook, sheetNames: String*): java.util.List[Sheet] = {
    sheets(workbook, sheetNames: _ *).asJava
  }

这里sheets和getSheets实现相同的功能,区别是第一个是Scala API,第二个是Java API。

结语

本文较详细的介绍了Java/Scala的互操作性,以上示例都来自作者及团队的实际工作。

这篇文章简单介绍了一些基础的Java/Scala互操作方法,接下来的文章将介绍些高级的互操作:Future、Optional/Option、lamdba函数、类与接口等。


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn