Package com.kenshoo.pl.entity
Class Triptional<T>
- java.lang.Object
-
- com.kenshoo.pl.entity.Triptional<T>
-
- Type Parameters:
T- the type of value in theTriptional
public class Triptional<T> extends java.lang.ObjectA container object which has three possible distinct states:- Present with a non-
nullvalue - Present with a
nullvalue - Absent
Optional, which allows one to distinguish between a "present andnull" value and an "absent" value.Similar to
Optional, this is a value-based class; use of identity-sensitive operations (including reference equality (==), identity hash code, or synchronization) on instances ofTriptionalmay have unpredictable results and should be avoided. The motivation for creating this type is as follows:In the persistence-layer we manage objects representing DB values, and since the DB supports
NULLas a valid field value, this resulted in two types that require a three-state representation of fields:- In
Entitywhich holds fields fetched from the DB, a field can be either:- Fetched with a non-
nullvalue - Fetched with a
nullvalue - Not fetched
- Fetched with a non-
- In
EntityChange(command) a field can be either:- Changed by the command to a non-
nullvalue - Changed by the command to a
nullvalue - Not in the command (unchanged)
- Changed by the command to a non-
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> Triptional<T>absent()Returns an absentTriptionalinstance.java.util.Optional<T>asOptional()Returns an {code Optional} with the value, if present and not-null; otherwise (whether present with anullvalue or absent) - return an emptyOptional.
This method is in effect a "reduction" operation where two distinct states ofTriptional,nulland absent, are mapped to the same emptyOptional.booleanequals(java.lang.Object obj)Indicates whether some other object is "equal to" thisTriptional.booleanequals(java.lang.Object obj, java.util.function.BiFunction<T,T,java.lang.Boolean> valueEqualityFunction)Indicates whether some other object is "equal to" thisTriptionalusing the input function to test equality.
The other object is considered equal if: it is also aTriptionaland; both instances have no value present or; both instances have a value present (possiblynull), and when the valueEqualityFunction is applied to both values it returnstruebooleanequalsValue(T value)Returntrueif there is a value present (possiblynull), and it equals the input; otherwisefalseTriptional<T>filter(java.util.function.Predicate<? super T> predicate)If a value is present, and the value matches the given predicate, returns anTriptionaldescribing the value, otherwise returns an absentTriptional.<U> Triptional<U>flatMap(java.util.function.Function<? super T,Triptional<U>> mapper)If a value is present and not-null, apply the Triptional-bearing notNullMapper() function to it, and return that result.
If a value is present andnull, return anullTriptional.
Otherwise - return an absentTriptional.
This method is similar toOptional.flatMap(java.util.function.Function<? super T, ? extends java.util.Optional<? extends U>>)except that it will preserve a presentnullvalue as well (will not turn it into an absentTriptional}<U> Triptional<U>flatMap(java.util.function.Function<? super T,Triptional<U>> notNullMapper, java.util.function.Supplier<Triptional<U>> nullReplacer)If a value is present and not-null, apply the Triptional-bearing notNullMapper() function to it, and return that result.
If a value is present andnull, call the nullReplacer() Triptional-bearing function, and return that result.
Otherwise - return an absentTriptional.Tget()If a value is present in thisTriptional(including possiblynull), returns the value, otherwise throwsNoSuchElementException.inthashCode()Returns a hash code value which is the combination of: The present value, if any, or 0 (zero) if no value is present. An internal indicator to differentiate between a present and an absent value (without this, a present value ofnulland an absent value would produce the same hashcode)voidifNotNull(java.util.function.Consumer<? super T> consumer)If a value is present and not-null, invoke the specified consumer with the value, otherwise do nothing.booleanisAbsent()Returntrueif there is no value present, otherwisefalse.booleanisNotNull()Returntrueif the value is present and notnull, otherwisefalse.booleanisNull()Returntrueif the value is present andnull, otherwisefalse.booleanisNullOrAbsent()Returntrueif the value is present andnullor no value, otherwisefalse.booleanisPresent()Returntrueif there is a value present, otherwisefalse.<U> Triptional<U>map(java.util.function.Function<? super T,? extends U> mapper)If a value is present and not-null, apply the mapper() function to it, and return aTriptionaldescribing the result.
If a value is present andnull, return anullTriptional instance.
Otherwise - return an absentTriptional.
This method is similar toOptional.map(java.util.function.Function<? super T, ? extends U>)except that it will preserve a presentnullvalue as well (will not turn it into an absent Triptional}<U> Triptional<U>map(java.util.function.Function<? super T,? extends U> notNullMapper, java.util.function.Supplier<? extends U> nullReplacer)If a value is present and not-null, apply the notNullMapper() function to it, and return aTriptionaldescribing the result.
If a value is present andnull, call the nullReplacer() function, and return aTriptionalholding the result.
Otherwise - return an absentTriptional.<U> java.util.Optional<U>mapToOptional(java.util.function.Function<? super T,? extends U> mapper)If a value is present and not-null, apply the notNullMapper() function to it, and return aOptionalholding the result, or an emptyOptionalif the result isnull.
Otherwise - return an emptyOptional.<U> java.util.Optional<U>mapToOptional(java.util.function.Function<? super T,? extends U> notNullMapper, java.util.function.Supplier<? extends U> nullReplacer)If a value is present and not-null, apply the notNullMapper() function to it, and return aOptionalholding the result, or an emptyOptionalif the result isnull.
If a value is present andnull, call the nullReplacer() function, and return anOptionalholding the result, or an emptyOptionalif the result isnull.
Otherwise - return an emptyOptional.booleanmatches(java.util.function.Predicate<? super T> predicate)Returntrueif there is a value is present, and it matches the given predicate; otherwise returnfalsestatic <T> Triptional<T>nullInstance()Returns annullTriptionalinstance - meaning that it is present but with anullvalue.
Note Though it may be tempting to do so, avoid testing if an object isnullby comparing with==against instances returned byTriptional.nullInstance().static <T> Triptional<T>of(T value)Returns aTriptionalwith the specified present value, can benull.java.lang.StringtoString()Returns a non-empty string representation of thisTriptionalsuitable for debugging.
-
-
-
Method Detail
-
of
public static <T> Triptional<T> of(T value)
Returns aTriptionalwith the specified present value, can benull.- Type Parameters:
T- the class of the value- Parameters:
value- the value to be present, can benull- Returns:
- a
Triptionalwith the value present
-
nullInstance
public static <T> Triptional<T> nullInstance()
Returns annullTriptionalinstance - meaning that it is present but with anullvalue.
Note Though it may be tempting to do so, avoid testing if an object isnullby comparing with==against instances returned byTriptional.nullInstance(). There is no guarantee that it is a singleton. Instead, useisNull().- Type Parameters:
T- Type of thenullvalue- Returns:
- a
nullTriptional
-
absent
public static <T> Triptional<T> absent()
Returns an absentTriptionalinstance. No value is present for this Triptional. Note Though it may be tempting to do so, avoid testing if an object is empty by comparing with==against instances returned byTriptional.absent(). There is no guarantee that it is a singleton. Instead, use eitherisPresent()orisAbsent().- Type Parameters:
T- Type of the absent value- Returns:
- an absent
Triptional
-
get
public T get()
If a value is present in thisTriptional(including possiblynull), returns the value, otherwise throwsNoSuchElementException.- Returns:
- the value held by this
Triptional, may benull - Throws:
java.util.NoSuchElementException- if there is no value present- See Also:
isPresent()
-
ifNotNull
public void ifNotNull(java.util.function.Consumer<? super T> consumer)
If a value is present and not-null, invoke the specified consumer with the value, otherwise do nothing.- Parameters:
consumer- block to be executed if a value is present and not-null
-
map
public <U> Triptional<U> map(java.util.function.Function<? super T,? extends U> mapper)
If a value is present and not-null, apply the mapper() function to it, and return aTriptionaldescribing the result.
If a value is present andnull, return anullTriptional instance.
Otherwise - return an absentTriptional.
This method is similar toOptional.map(java.util.function.Function<? super T, ? extends U>)except that it will preserve a presentnullvalue as well (will not turn it into an absent Triptional}- Type Parameters:
U- The type of the result of the mapping function- Parameters:
mapper- a mapping function to apply to the value, if present and not-null- Returns:
- a
Triptionaldescribing the result of applying the mapper, if it is present and not-null; or a nullTriptional, if it is present andnull; otherwise - an absentTriptional - Throws:
java.lang.NullPointerException- if the mapper function isnull
-
map
public <U> Triptional<U> map(java.util.function.Function<? super T,? extends U> notNullMapper, java.util.function.Supplier<? extends U> nullReplacer)
If a value is present and not-null, apply the notNullMapper() function to it, and return aTriptionaldescribing the result.
If a value is present andnull, call the nullReplacer() function, and return aTriptionalholding the result.
Otherwise - return an absentTriptional. Note This method is similar toOptional.map(java.util.function.Function<? super T, ? extends U>)except that it also allows for a separately-defined replacement operation in case of a presentnullvalue. This is convenient in case some default value is required to replace thenull.
For example - the following code will convert numbers into their string representations, unless the number isnull, in which case it will be replaced with an empty string:final Integer number = readNumber(); final Triptional<String> triptional = Triptional.of(number).map(String::valueOf, () -> "");- Type Parameters:
U- The type of the result of the mapping function- Parameters:
notNullMapper- a mapping function to apply to the value, if present and not-nullnullReplacer- a function to calculate a replacement value, if present andnull- Returns:
- a
Triptionaldescribing the result of applying the notNullMapper function to the value of thisTriptional, if it is present and not-null; or aTriptionalholding the result of the nullReplacer function, if present andnull; otherwise - an absentTriptional - Throws:
java.lang.NullPointerException- if the notNullMapper function isnullor the nullReplacer function isnull
-
flatMap
public <U> Triptional<U> flatMap(java.util.function.Function<? super T,Triptional<U>> mapper)
If a value is present and not-null, apply the Triptional-bearing notNullMapper() function to it, and return that result.
If a value is present andnull, return anullTriptional.
Otherwise - return an absentTriptional.
This method is similar toOptional.flatMap(java.util.function.Function<? super T, ? extends java.util.Optional<? extends U>>)except that it will preserve a presentnullvalue as well (will not turn it into an absentTriptional}- Type Parameters:
U- The type of the result of the mapping function- Parameters:
mapper- a Triptional-bearing mapping function to apply to the value, if present and value not-null- Returns:
- the result of applying the notNullMapper function, if present and value not-
null; or anullTriptional, if present and value isnull; otherwise - an absentTriptional - Throws:
java.lang.NullPointerException- if the mapper function isnull, or if it returns anullresult
-
flatMap
public <U> Triptional<U> flatMap(java.util.function.Function<? super T,Triptional<U>> notNullMapper, java.util.function.Supplier<Triptional<U>> nullReplacer)
If a value is present and not-null, apply the Triptional-bearing notNullMapper() function to it, and return that result.
If a value is present andnull, call the nullReplacer() Triptional-bearing function, and return that result.
Otherwise - return an absentTriptional. Note This method is similar toOptional.flatMap(java.util.function.Function<? super T, ? extends java.util.Optional<? extends U>>)except that it also allows for a separately-defined replacement for anullvalue. This is convenient in case some operation requires a default value to replace thenull.
For example - the following code converts a nested Triptional holding a number into a single one, unless the inner object is anullTriptional, in which case it will be replaced by a zero:final Triptional<Triptional<Integer>> nested = readNestedTriptional(); final Triptional<Integer> flattened = nested.flatMap(Function.identity(), () -> 0);- Type Parameters:
U- The type of the result of the mapping function- Parameters:
notNullMapper- a Triptional-bearing mapping function to apply to the value, if present and value not-nullnullReplacer- a Triptional-bearing function to calculate a replacement instance, if present and value isnull- Returns:
- the result of applying the notNullMapper function, if present and value not-
null; or the result of calling the nullReplacer function, if present and value isnull; otherwise - an absentTriptional - Throws:
java.lang.NullPointerException- if either of the input functions arenull, or if either one of them returns anullresult
-
asOptional
public java.util.Optional<T> asOptional()
Returns an {code Optional} with the value, if present and not-null; otherwise (whether present with anullvalue or absent) - return an emptyOptional.
This method is in effect a "reduction" operation where two distinct states ofTriptional,nulland absent, are mapped to the same emptyOptional.- Returns:
- an {code Optional} with a value, if present and not-
null, otherwise - an emptyOptional
-
mapToOptional
public <U> java.util.Optional<U> mapToOptional(java.util.function.Function<? super T,? extends U> mapper)
If a value is present and not-null, apply the notNullMapper() function to it, and return aOptionalholding the result, or an emptyOptionalif the result isnull.
Otherwise - return an emptyOptional.- Type Parameters:
U- The type of the result of the mapping function- Parameters:
mapper- a mapping function to apply to the value, if present and not-null- Returns:
- an
Optionalholding the result of applying the mapper function to the value of thisTriptional, if it is present and not-null; otherwise - an emptyOptional - Throws:
java.lang.NullPointerException- if the mapper function isnull
-
mapToOptional
public <U> java.util.Optional<U> mapToOptional(java.util.function.Function<? super T,? extends U> notNullMapper, java.util.function.Supplier<? extends U> nullReplacer)
If a value is present and not-null, apply the notNullMapper() function to it, and return aOptionalholding the result, or an emptyOptionalif the result isnull.
If a value is present andnull, call the nullReplacer() function, and return anOptionalholding the result, or an emptyOptionalif the result isnull.
Otherwise - return an emptyOptional.- Type Parameters:
U- The type of the result of the mapping function- Parameters:
notNullMapper- a mapping function to apply to the value, if present and not-nullnullReplacer- a function to calculate a replacement value, if present andnull- Returns:
- an
Optionalholding the result of applying the notNullMapper function to the value of thisTriptional, if it is present and not-null; or anOptionalholding the result of the nullReplacer function, if present andnull; otherwise - an emptyOptional - Throws:
java.lang.NullPointerException- if the notNullMapper function isnullor the nullReplacer function isnull
-
filter
public Triptional<T> filter(java.util.function.Predicate<? super T> predicate)
If a value is present, and the value matches the given predicate, returns anTriptionaldescribing the value, otherwise returns an absentTriptional.- Parameters:
predicate- the predicate to apply to a value, if present- Returns:
- an
Triptionaldescribing the value of thisTriptional, if a value is present and the value matches the given predicate; otherwise an absentTriptional - Throws:
java.lang.NullPointerException- if the predicate isnull
-
matches
public boolean matches(java.util.function.Predicate<? super T> predicate)
Returntrueif there is a value is present, and it matches the given predicate; otherwise returnfalse- Parameters:
predicate- the predicate to apply to a value, if present- Returns:
trueif there is a value is present, and it matches the given predicate; otherwisefalse- Throws:
java.lang.NullPointerException- if the predicate isnull
-
isPresent
public boolean isPresent()
Returntrueif there is a value present, otherwisefalse.- Returns:
trueif there is a value present, otherwisefalse
-
isAbsent
public boolean isAbsent()
Returntrueif there is no value present, otherwisefalse.- Returns:
trueif there is no value present, otherwisefalse.
-
isNotNull
public boolean isNotNull()
Returntrueif the value is present and notnull, otherwisefalse.- Returns:
trueif the value is present and notnull, otherwisefalse.
-
isNullOrAbsent
public boolean isNullOrAbsent()
Returntrueif the value is present andnullor no value, otherwisefalse.- Returns:
trueif the value is present andnullor no value, otherwisefalse.
-
isNull
public boolean isNull()
Returntrueif the value is present andnull, otherwisefalse.- Returns:
trueif the value is present andnull, otherwisefalse.
-
equalsValue
public boolean equalsValue(T value)
Returntrueif there is a value present (possiblynull), and it equals the input; otherwisefalse- Parameters:
value- a value to compare to this value- Returns:
trueif there is a value present, and it equals the input; otherwisefalse
-
equals
public boolean equals(java.lang.Object obj)
Indicates whether some other object is "equal to" thisTriptional. The other object is considered equal if:- it is also a
Triptionaland; - both instances have no value present or;
- both instances have a present and
nullvalue or; - the present values are "equal to" each other via
equals().
- Overrides:
equalsin classjava.lang.Object- Parameters:
obj- an object to be tested for equality- Returns:
trueif the other object is "equal to" this object otherwisefalse
- it is also a
-
equals
public boolean equals(java.lang.Object obj, java.util.function.BiFunction<T,T,java.lang.Boolean> valueEqualityFunction)Indicates whether some other object is "equal to" thisTriptionalusing the input function to test equality.
The other object is considered equal if:- it is also a
Triptionaland; - both instances have no value present or;
- both instances have a value present (possibly
null), and when the valueEqualityFunction is applied to both values it returnstrue
- Parameters:
obj- an object to be tested for equalityvalueEqualityFunction- the function to use for testing equality of values, when present- Returns:
trueif the other object is "equal to" this object otherwisefalse- Throws:
java.lang.NullPointerException- if valueEqualityFunction isnull
- it is also a
-
hashCode
public int hashCode()
Returns a hash code value which is the combination of:- The present value, if any, or 0 (zero) if no value is present.
- An internal indicator to differentiate between a present and an absent value
(without this, a present value of
nulland an absent value would produce the same hashcode)
- Overrides:
hashCodein classjava.lang.Object- Returns:
- hash code value of this instance
-
toString
public java.lang.String toString()
Returns a non-empty string representation of thisTriptionalsuitable for debugging. The exact presentation format is unspecified and may vary between implementations and versions.
Implementation Note: If a value is present the result must include its string representation in the result. Absent,nulland not-nullTriptionals must all be unambiguously differentiable.- Overrides:
toStringin classjava.lang.Object- Returns:
- the string representation of this instance
-
-