Class ValueWrapper

  • All Implemented Interfaces:
    java.io.Serializable

    public final class ValueWrapper
    extends java.lang.Object
    implements java.io.Serializable
    Serializable representation of a value that was used in an assertion.

    This class only stores the value if it implements Serializable. In any case, it stores its runtime type, identity hash code, and string representation determined via String.valueOf(Object). If the invocation of String.valueOf(Object) throws an Exception, the string representation will take the form of "<Exception in toString(): " + e + ">", where "e" is the caught exception.

    The toString() method returns the string representation of the value along with its type and identity hash code.

    Since:
    1.0
    See Also:
    System.identityHashCode(java.lang.Object), Serialized Form
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private ValueWrapper​(java.lang.Object value)  
      private ValueWrapper​(java.lang.Object value, java.lang.String stringRepresentation)
      Reads and stores the supplied value's runtime type, string representation, and identity hash code.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static ValueWrapper create​(java.lang.Object value)
      Factory for creating a new ValueWrapper for the supplied value.
      static ValueWrapper create​(java.lang.Object value, java.lang.String stringRepresentation)
      Factory for creating a new ValueWrapper for the supplied value using the supplied custom stringRepresentation.
      java.lang.Object getEphemeralValue()
      Returns the original value supplied to create().
      int getIdentityHashCode()
      Returns the value's identity hash code.
      java.lang.String getStringRepresentation()
      Returns the value's string representation.
      java.lang.Class<?> getType()
      Returns the value's runtime type or null if the value is null.
      java.io.Serializable getValue()
      Returns the value supplied to create(Object) if the value implements Serializable; otherwise, null.
      private static java.lang.String safeValueToString​(java.lang.Object value)  
      java.lang.String toString()
      Returns the value's string representation along with its type and identity hash code.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • nullValueWrapper

        private static final ValueWrapper nullValueWrapper
      • value

        private final java.io.Serializable value
      • type

        private final java.lang.Class<?> type
      • stringRepresentation

        private final java.lang.String stringRepresentation
      • identityHashCode

        private final int identityHashCode
      • ephemeralValue

        private final transient java.lang.Object ephemeralValue
    • Constructor Detail

      • ValueWrapper

        private ValueWrapper​(java.lang.Object value,
                             java.lang.String stringRepresentation)
        Reads and stores the supplied value's runtime type, string representation, and identity hash code.
      • ValueWrapper

        private ValueWrapper​(java.lang.Object value)
    • Method Detail

      • create

        public static ValueWrapper create​(java.lang.Object value)
        Factory for creating a new ValueWrapper for the supplied value.

        If the supplied value is null, this method will return a cached ValueWrapper suitable for all null values. If the supplied value is already an instance of ValueWrapper, it will be returned as is.

        Parameters:
        value - the value to wrap; may be null
        Returns:
        a wrapper for the supplied value; never null
      • create

        public static ValueWrapper create​(java.lang.Object value,
                                          java.lang.String stringRepresentation)
        Factory for creating a new ValueWrapper for the supplied value using the supplied custom stringRepresentation.

        You should use this method when you don't want to rely on the result of the value's toString() method.

        If the supplied value is null, this method will return a cached ValueWrapper suitable for all null values. If the supplied value is already an instance of ValueWrapper, it will be returned as is if the stringRepresentation match, otherwise the original value will be unwrapped and a new ValueWrapper with the new stringRepresentation will be created.

        Parameters:
        value - the value to wrap; may be null
        stringRepresentation - a custom rendering of the value; will fallback to the default behavior if null
        Returns:
        a wrapper for the supplied value; never null
        Since:
        1.2
      • safeValueToString

        private static java.lang.String safeValueToString​(java.lang.Object value)
      • getValue

        public java.io.Serializable getValue()
        Returns the value supplied to create(Object) if the value implements Serializable; otherwise, null.
        See Also:
        getEphemeralValue()
      • getType

        public java.lang.Class<?> getType()
        Returns the value's runtime type or null if the value is null.
      • getStringRepresentation

        public java.lang.String getStringRepresentation()
        Returns the value's string representation.

        The string representation is generated by invoking String.valueOf(value) for the value supplied to create(Object).

        See Also:
        getValue()
      • getIdentityHashCode

        public int getIdentityHashCode()
        Returns the value's identity hash code.

        The identity hash code is generated by invoking System.identityHashCode(value) for the value supplied to create(Object).

        See Also:
        getValue()
      • getEphemeralValue

        public java.lang.Object getEphemeralValue()
        Returns the original value supplied to create().

        If this ValueWrapper was created by deserialization this method returns null.

        Since:
        1.2
        See Also:
        getValue()
      • toString

        public java.lang.String toString()
        Returns the value's string representation along with its type and identity hash code.
        Overrides:
        toString in class java.lang.Object