gpt4 book ai didi

java - 在 Java 中实例化 scala.Int

转载 作者:行者123 更新时间:2023-12-02 14:06:07 25 4
gpt4 key购买 nike

我正在为 SecureSocial 编写持久层Play 2框架的插件。我在https://github.com/play-modules/modules.playframework.org/blob/master/app/models/ss/MPOOAuth2Info.java找到了一个例子:

package models.ss;

import models.AbstractModel;
import securesocial.core.java.OAuth2Info;
import javax.persistence.Entity;

@Entity
public class MPOOAuth2Info extends AbstractModel
{
public String accessToken;

public String tokenType;

public Integer expiresIn;

public String refreshToken;

public MPOOAuth2Info()
{
// no-op
}

public MPOOAuth2Info(OAuth2Info oAuth2Info)
{
this.accessToken = oAuth2Info.accessToken;
this.tokenType = oAuth2Info.tokenType;
this.expiresIn = oAuth2Info.expiresIn;
this.refreshToken = oAuth2Info.refreshToken;
}

public OAuth2Info toOAuth2Info()
{
OAuth2Info oAuth2Info = new OAuth2Info();

oAuth2Info.accessToken = this.accessToken;
oAuth2Info.tokenType = this.tokenType;
oAuth2Info.expiresIn = this.expiresIn;
oAuth2Info.refreshToken = this.refreshToken;

return oAuth2Info;
}
}

但 API 已更改,因此我无法使用 securesocial.core.java.OAuth2Info。 SecureSocial 是由 Scala 编写的,该类是一个 Java 前端。所以我决定直接使用Scala,其中:

case class OAuth2Info(accessToken: String, tokenType: Option[String] = None,
expiresIn: Option[Int] = None, refreshToken: Option[String] = None)

我的结果:

package models.security.securesocial;

import models.AbstractModel;
import scala.Option;
import securesocial.core.*;

import javax.persistence.Entity;

/**
* Persistence wrapper for SecureSocial's {@link } class.
*
* @author Steve Chaloner (steve@objectify.be)
*/
@Entity
public class MPOOAuth2Info extends AbstractModel
{
public String accessToken;

public String tokenType;

public Integer expiresIn;

public String refreshToken;

public MPOOAuth2Info(){
// no-op
}

public MPOOAuth2Info(OAuth2Info oAuth2Info){
this.accessToken = oAuth2Info.accessToken();
this.tokenType = oAuth2Info.tokenType().get();
this.expiresIn = scala.Int.unbox(oAuth2Info.expiresIn().get());
this.refreshToken = oAuth2Info.refreshToken().get();
}

public OAuth2Info toOAuth2Info(){
return new OAuth2Info(accessToken, Option.apply(tokenType), Option.apply(SOME_TRANSFORMATION(expiresIn)), Option.apply(refreshToken));
}
}

但是我在将 scala.Intjava.lang.Integer 类型转换时遇到问题。要将 scala.Int 转换为 java.lang.Integer,我使用了 scala.Int.unbox()。是连接方式吗?而且我不知道如何将 java.lang.Integer 转换为 scala.Int:在代码中我输入了伪代码 SOME_TRANSFORMATION() 。此 SOME_TRANSFORMATION 的正确实现是什么?

谢谢

最佳答案

scala.Int.unbox(new java.lang.Integer(3)) 给出 Int = 3

scala.Int.box(3) 给出 Integer = 3

关于java - 在 Java 中实例化 scala.Int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14944684/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com