Class NumberMorpher

All Implemented Interfaces:
Morpher, ObjectMorpher

public final class NumberMorpher extends AbstractObjectMorpher
Morphs to a subclass of Number.
Supported types are - Byte, Short, Integer, Long, Float, BigInteger, BigtDecimal.
  • Field Details

    • defaultValue

      private Number defaultValue
    • type

      private Class type
  • Constructor Details

    • NumberMorpher

      public NumberMorpher(Class type)
      Creates a new morpher for the target type.
      Parameters:
      type - must be a primitive or wrapper type. BigDecimal and BigInteger are also supported.
    • NumberMorpher

      public NumberMorpher(Class type, Number defaultValue)
      Creates a new morpher for the target type with a default value.
      The defaultValue should be of the same class as the target type.
      Parameters:
      type - must be a primitive or wrapper type. BigDecimal and BigInteger are also supported.
      defaultValue - return value if the value to be morphed is null
  • Method Details

    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • getDefaultValue

      public Number getDefaultValue()
      Returns the default value for this Morpher.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • morph

      public Object morph(Object value)
      Description copied from interface: ObjectMorpher
      Morphs the input object into an output object of the supported type.
      Parameters:
      value - The input value to be morphed
    • morphsTo

      public Class morphsTo()
      Description copied from interface: Morpher
      Returns the target Class for conversion.
      Returns:
      the target Class for conversion.
    • setDefaultValue

      public void setDefaultValue(Number defaultValue)
      Sets the defaultValue to use if the value to be morphed is null.
      The defaultValue should be of the same class as the type this morpher returns with morphsTo().
      Parameters:
      defaultValue - return value if the value to be morphed is null
    • isDecimalNumber

      private boolean isDecimalNumber(Class type)
    • morphToBigDecimal

      private Object morphToBigDecimal(String str)
    • morphToBigInteger

      private Object morphToBigInteger(String str)
    • morphToByte

      private Object morphToByte(String str)
    • morphToDouble

      private Object morphToDouble(String str)
    • morphToFloat

      private Object morphToFloat(String str)
    • morphToInteger

      private Object morphToInteger(String str)
    • morphToLong

      private Object morphToLong(String str)
    • morphToShort

      private Object morphToShort(String str)