Frage

I'm writing the persistence layer for the SecureSocial plugin of Play 2 framework. I found an example at 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;
    }
}

But the API was changed so I can't use securesocial.core.java.OAuth2Info. SecureSocial is written by Scala and this class was a Java front-end. So I decided to use Scala directly where:

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

My result:

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));
    }
}

But I have problems with convertation of scala.Int to/from java.lang.Integer types. To convert scala.Int to java.lang.Integer I used scala.Int.unbox(). Is it connect way? And I don't know how to convert java.lang.Integer to scala.Int: in the code I typed pseudo code SOME_TRANSFORMATION(). What is the correct implementation of this SOME_TRANSFORMATION?

Thank you

War es hilfreich?

Lösung

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

scala.Int.box(3) gives Integer = 3

Andere Tipps

Scala boxes and unboxes automatically and transparently. Furthermore, scala.Int is sort of a fiction. It's a native int (à la Java / JVM) when stored in a local variable, passed as a formal parameter or stored in a class. But when it must be trafficked via a value of a parameterized / generic type, it must be boxed and unboxed. (There's an exception, when the type parameter in question is specialized, but that's another story.)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top