Class StringDelegateCoder<T>

  • Type Parameters:
    T - The type of objects coded.
    All Implemented Interfaces:
    java.io.Serializable

    public final class StringDelegateCoder<T>
    extends CustomCoder<T>
    A Coder that wraps a Coder<String> and encodes/decodes values via string representations.

    To decode, the input byte stream is decoded to a String, and this is passed to the single-argument constructor for T.

    To encode, the input value is converted via toString(), and this string is encoded.

    In order for this to operate correctly for a class Clazz, it must be the case for any instance x that x.equals(new Clazz(x.toString())).

    This method of encoding is not designed for ease of evolution of Clazz; it should only be used in cases where the class is stable or the encoding is not important. If evolution of the class is important, see AvroCoder or any other evolution safe encoding.

    See Also:
    Serialized Form
    • Constructor Detail

      • StringDelegateCoder

        protected StringDelegateCoder​(java.lang.Class<T> clazz,
                                      TypeDescriptor<T> typeDescriptor)
    • Method Detail

      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • equals

        public boolean equals​(@Nullable java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • encode

        public void encode​(T value,
                           java.io.OutputStream outStream)
                    throws CoderException,
                           java.io.IOException
        Description copied from class: Coder
        Encodes the given value of type T onto the given output stream.
        Specified by:
        encode in class Coder<T>
        Throws:
        CoderException - if the value could not be encoded for some reason
        java.io.IOException - if writing to the OutputStream fails for some reason
      • encode

        public void encode​(T value,
                           java.io.OutputStream outStream,
                           Coder.Context context)
                    throws CoderException,
                           java.io.IOException
        Description copied from class: Coder
        Encodes the given value of type T onto the given output stream in the given context.
        Overrides:
        encode in class Coder<T>
        Throws:
        CoderException - if the value could not be encoded for some reason
        java.io.IOException - if writing to the OutputStream fails for some reason
      • decode

        public T decode​(java.io.InputStream inStream)
                 throws CoderException,
                        java.io.IOException
        Description copied from class: Coder
        Decodes a value of type T from the given input stream in the given context. Returns the decoded value.
        Specified by:
        decode in class Coder<T>
        Throws:
        CoderException - if the value could not be decoded for some reason
        java.io.IOException - if reading from the InputStream fails for some reason
      • decode

        public T decode​(java.io.InputStream inStream,
                        Coder.Context context)
                 throws CoderException,
                        java.io.IOException
        Description copied from class: Coder
        Decodes a value of type T from the given input stream in the given context. Returns the decoded value.
        Overrides:
        decode in class Coder<T>
        Throws:
        CoderException - if the value could not be decoded for some reason
        java.io.IOException - if reading from the InputStream fails for some reason
      • structuralValue

        public java.lang.Object structuralValue​(T value)
        Description copied from class: Coder
        Returns an object with an Object.equals() method that represents structural equality on the argument.

        For any two values x and y of type T, if their encoded bytes are the same, then it must be the case that structuralValue(x).equals(structuralValue(y)).

        Most notably:

        • The structural value for an array coder should perform a structural comparison of the contents of the arrays, rather than the default behavior of comparing according to object identity.
        • The structural value for a coder accepting null should be a proper object with an equals() method, even if the input value is null.

        See also Coder.consistentWithEquals().

        By default, if this coder is Coder.consistentWithEquals(), and the value is not null, returns the provided object. Otherwise, encodes the value into a byte[], and returns an object that performs array equality on the encoded bytes.

        Overrides:
        structuralValue in class Coder<T>