Class ImmutableValueObject
- java.lang.Object
-
- dk.cloudcreate.essentials.immutable.ImmutableValueObject
-
- All Implemented Interfaces:
Immutable
public abstract class ImmutableValueObject extends Object implements Immutable
The base typeImmutableValueObjectsupports creating immutable (i.e. an object where its values cannot change after object instantiation/creation) Value Object
The core feature set ofImmutableValueObjectis that it provides default implementations fortoString(),equals(Object)andhashCode(), 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;
-
-
Constructor Summary
Constructors Constructor Description ImmutableValueObject()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanequals(Object that)The logic ofequals(Object)follows the standard approach where we don't accept subclasses of a type, we only accept the exact same type.
All fields without theExclude.EqualsAndHashCodeannotation 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.inthashCode()All fields without theExclude.EqualsAndHashCodeannotation with be included the calculation of thehash-code.
The fields are sorted alphabetically (ascending order) and thehash-codewill be calculated in the order of the fields names.
The algorithm for calculating the hashcode follows the Objects#hash(Object...) method.StringtoString()All fields without theExclude.ToStringannotation 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:
-
-
-
Method Detail
-
hashCode
public int hashCode()
All fields without theExclude.EqualsAndHashCodeannotation with be included the calculation of thehash-code.
The fields are sorted alphabetically (ascending order) and thehash-codewill be calculated in the order of the fields names.
The algorithm for calculating the hashcode follows the Objects#hash(Object...) method.- Overrides:
hashCodein classObject- Returns:
- the calculated hashcode
- See Also:
Object.hashCode()
-
equals
public boolean equals(Object that)
The logic ofequals(Object)follows the standard approach where we don't accept subclasses of a type, we only accept the exact same type.
All fields without theExclude.EqualsAndHashCodeannotation 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:
equalsin classObject- 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 theExclude.ToStringannotation 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:
toStringin classObject- Returns:
- all fields and their values (see method description)
- See Also:
Object.toString()
-
-