Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some code for autoboxing semantics #47

Open
michaelheilmann opened this issue May 18, 2014 · 0 comments
Open

Some code for autoboxing semantics #47

michaelheilmann opened this issue May 18, 2014 · 0 comments

Comments

@michaelheilmann
Copy link

I have in my code base the following code for autoboxing support; maybe it can be added to TypeReference or wherever it may be appropriate. It seems like a nice addition when dealing with type signatures.

  /**
   * @brief
   *  Perform unboxing i.e. apply the following mapping
   *  <ul>
   *    <li>java.lang.Boolean -> boolean</li>
   *    <li>java.lang.Byte -> byte</li>
   *    <li>java.lang.Character -> char</li>
   *    <li>java.lang.Double -> double</li>
   *    <li>java.lang.Float -> float</li>
   *    <li>java.lang.Integer -> int</li>
   *    <li>java.lang.Long -> long</li>
   *    <li>java.lang.Short -> short</li>
   *  </ul>
   *  if @a t refers to one of the types on the left-hand side,
   *  otherwise simply return @a t.
   * @param t
   *  The type reference.
   * @return
   *  The type reference to the unboxed type or @a t.
   */
  public static TypeReference unbox(TypeReference t) {
    if (t == TypeReference.JavaLangBoolean) {
      return TypeReference.Boolean;
    } else if (t == TypeReference.JavaLangByte) {
      return TypeReference.Byte;
    } else if (t == TypeReference.JavaLangCharacter) {
      return TypeReference.Char;
    } else if (t == TypeReference.JavaLangDouble) {
      return TypeReference.Double;
    } else if (t == TypeReference.JavaLangFloat) {
      return TypeReference.Float;
    } else if (t == TypeReference.JavaLangInteger) {
      return TypeReference.Int;
    } else if (t == TypeReference.JavaLangLong) {
      return TypeReference.Long;
    } else if (t == TypeReference.JavaLangShort) {
      return TypeReference.Short;
    } else {
      return t;
    }
  }

  /**
   * @brief
   *  Perform boxing i.e. apply the following mapping
   *  <ul>
   *    <li>boolean -> java.lang.Boolean</li>
   *    <li>byte -> java.lang.Byte</li>
   *    <li>char -> java.lang.Character</li>
   *    <li>double -> java.lang.Double</li>
   *    <li>float -> java.lang.Float</li>
   *    <li>int -> java.lang.Integer</li>
   *    <li>long -> java.lang.Long</li>
   *    <li>short -> java.lang.Short</li>
   *  <ul>
   *  if @a t refers to one of the types on the left-hand side,
   *  otherwise simply return @a t.
   * @param t
   *  The type reference.
   * @return
   *  The type reference to the boxed type or @a t.
   */
  public static TypeReference box(TypeReference t) {
    if (t == TypeReference.Boolean) {
      return TypeReference.JavaLangBoolean;
    } else if (t == TypeReference.Byte) {
      return TypeReference.JavaLangByte;
    } else if (t == TypeReference.Char) {
      return TypeReference.JavaLangCharacter;
    } else if (t == TypeReference.Double) {
      return TypeReference.JavaLangDouble;
    } else if (t == TypeReference.Float) {
      return TypeReference.JavaLangFloat;
    } else if (t == TypeReference.Int) {
      return TypeReference.JavaLangInteger;
    } else if (t == TypeReference.Long) {
      return TypeReference.JavaLangLong;
    } else if (t == TypeReference.Short) {
      return TypeReference.JavaLangShort;
    } else {
      return t;
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant