Class ImmutableValueObject

  • All Implemented Interfaces:
    Immutable

    public abstract class ImmutableValueObject
    extends Object
    implements Immutable
    The base type ImmutableValueObject supports creating immutable (i.e. an object where its values cannot change after object instantiation/creation) Value Object
    The core feature set of ImmutableValueObject is that it provides default implementations for toString(), equals(Object) and hashCode(), but you're always free to override this and provide your own implementation.

    Example:
    {@code
     public class ImmutableOrder extends ImmutableValueObject {
         public final OrderId                  orderId;
         public final CustomerId               customerId;
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean equals​(Object that)
      The logic of equals(Object) follows the standard approach where we don't accept subclasses of a type, we only accept the exact same type.
      All fields without the Exclude.EqualsAndHashCode annotation with be included in the equals operation.
      The fields are sorted alphabetically (ascending order) and values from the two objects being compared one by one.
      As soon a difference in field values is identified the comparison is stopped (to avoid wasting CPU cycles) and the result is returned.
      int hashCode()
      All fields without the Exclude.EqualsAndHashCode annotation with be included the calculation of the hash-code.
      The fields are sorted alphabetically (ascending order) and the hash-code will be calculated in the order of the fields names.
      The algorithm for calculating the hashcode follows the Objects#hash(Object...) method.
      String toString()
      All fields without the Exclude.ToString annotation with be included in the output.
      The fields are sorted alphabetically (ascending order) and then grouped into fields with and without value.
      We will first output non-null fields (in alphabetically order) and finally null fields (in alphabetically order)

      Example:
    • Constructor Detail

      • ImmutableValueObject

        public ImmutableValueObject()
    • Method Detail

      • hashCode

        public int hashCode()
        All fields without the Exclude.EqualsAndHashCode annotation with be included the calculation of the hash-code.
        The fields are sorted alphabetically (ascending order) and the hash-code will be calculated in the order of the fields names.
        The algorithm for calculating the hashcode follows the Objects#hash(Object...) method.
        Overrides:
        hashCode in class Object
        Returns:
        the calculated hashcode
        See Also:
        Object.hashCode()
      • equals

        public boolean equals​(Object that)
        The logic of equals(Object) follows the standard approach where we don't accept subclasses of a type, we only accept the exact same type.
        All fields without the Exclude.EqualsAndHashCode annotation with be included in the equals operation.
        The fields are sorted alphabetically (ascending order) and values from the two objects being compared one by one.
        As soon a difference in field values is identified the comparison is stopped (to avoid wasting CPU cycles) and the result is returned.
        Overrides:
        equals in class Object
        Parameters:
        that - the other object we're comparing our instance to
        Returns:
        true if this object is the same as the that argument otherwise false
        See Also:
        Object.equals(Object)
      • toString

        public String toString()
        All fields without the Exclude.ToString annotation with be included in the output.
        The fields are sorted alphabetically (ascending order) and then grouped into fields with and without value.
        We will first output non-null fields (in alphabetically order) and finally null fields (in alphabetically order)

        Example:
        {@code
         public class ImmutableOrder extends ImmutableValueObject {
             public final OrderId                  orderId;
             public final CustomerId               customerId;
        Overrides:
        toString in class Object
        Returns:
        all fields and their values (see method description)
        See Also:
        Object.toString()