Class Primitive<T>

java.lang.Object
org.dellroad.stuff.java.Primitive<T>
Type Parameters:
T - Primitive wrapper type
All Implemented Interfaces:
Serializable, Comparator<T>

public abstract class Primitive<T> extends Object implements Comparator<T>, Serializable
Simple utility enumeration for working Java Class instances representing primitive types (including void).

Instances of this class represent the eight Java primitive types plus void.

See Also:
  • Field Details

  • Method Details

    • visit

      public abstract <R> R visit(PrimitiveSwitch<R> pswitch)
      Apply visitor pattern.
      Type Parameters:
      R - return type
      Parameters:
      pswitch - visit target
      Returns:
      result of visit
    • getDefaultValue

      public abstract T getDefaultValue()
      Get this primitive's default value.
      Returns:
      default value for this primitive type
    • getName

      public String getName()
      Get the short name for this primitive type, e.g., "int".
      Returns:
      short form name of this primitive type
    • getLongName

      public String getLongName()
      Get the long name for this primitive type, e.g., "Integer". Also the simple name of the wrapper type.
      Returns:
      long form name of this primitive type
    • getLetter

      public char getLetter()
      Get the single character descriptor for this primitive type, e.g., "I".
      Returns:
      letter associated with this primitive type
    • getSize

      public int getSize()
      Get the size of one value of this primitive type in bytes.
      Returns:
      primitive size in bytes
    • getType

      public Class<T> getType()
      Get the Class object representing this primitive type, e.g., Integer.TYPE.
      Returns:
      primitive type associated with this primitive type
    • getWrapperType

      public Class<T> getWrapperType()
      Get the wrapper Class object for this primitive type, e.g., Integer.class.
      Returns:
      wrapper type associated with this primitive type
    • getParsePattern

      public Pattern getParsePattern()
      Get a regular expression that matches all allowed inputs to parseValue(). The returned pattern may also accept some values that parseValue() rejects, such as 32768 for a short.
      Returns:
      pattern matching valid string encodings
    • getUnwrapMethod

      public Method getUnwrapMethod()
      Get the wrapper class' "unwrap" method for this primitive type, e.g., Integer.intValue().
      Returns:
      the unwrap method associated with this primitive type
    • compare

      public abstract int compare(T value1, T value2)
      Compare two primitive values.
      Specified by:
      compare in interface Comparator<T>
      Throws:
      NullPointerException - if either value is null
    • parseValue

      public T parseValue(String string)
      Parse a string-encoded value of this instance. This method is guaranteed to accept all possible return values from the primitive's toString(), and possibly other reasonable inputs (e.g., hex and octal values for integral types).
      Parameters:
      string - string representation of a value
      Returns:
      parsed value
      Throws:
      IllegalArgumentException - if string is null
      IllegalArgumentException - if string does not match the parse pattern
    • forName

      public static Primitive<?> forName(String name)
      Get the instance with the given name (e.g., "void", "int", etc.).
      Parameters:
      name - instance short name
      Returns:
      corresponding instance, or null if name does not correspond to any Primitive instance
      Throws:
      IllegalArgumentException - if name is null
    • values

      public static Set<Primitive<?>> values()
      Get all instances of this class.
      Returns:
      a modifiable set containing all instances; modifications have no effect on this class
    • get

      public static <T> Primitive<T> get(Class<T> c)
      Get the value corresponding to the given Java primitive or primitive wrapper type.
      Parameters:
      c - Java type class object
      Returns:
      the Primitive corresponding to c, or null if c is not a primitive or primitive wrapper type
    • unwrap

      public static <T> Class<T> unwrap(Class<T> type)
      Unwrap the given type into its primitive type if it's a primitive wrapper type.
      Parameters:
      type - given type
      Returns:
      primitive type corresponding to type, or type if type is not a primitive wrapper type, or null if type is null
    • wrap

      public static <T> Class<T> wrap(Class<T> type)
      Wrap the given primitive type into its wrapper type if it's a primitive type.
      Parameters:
      type - given type
      Returns:
      primitive wrapper type corresponding to type, or type if type is not a primitive type, or null if type is null
    • toString

      public String toString()
      Overrides:
      toString in class Object