public class Value extends Object
It supports elegant null
handling and type
conversions.
Modifier and Type | Field and Description |
---|---|
static Value |
EMPTY
Represents an empty value which contains null as data.
|
Modifier and Type | Method and Description |
---|---|
String |
afterFirst(String separator)
Returns the substring of the internal value starting right after the first occurrence of the given separator.
|
String |
afterLast(String separator)
Returns the substring of the internal value starting right after the last occurrence of the given separator.
|
Value |
append(String separator,
Object value)
Returns a value which wraps
this + separator + value
If the current value is empty, the given value is returned (without the separator). |
boolean |
asBoolean()
Boilerplate method for
asBoolean(false) |
boolean |
asBoolean(boolean defaultValue)
Converts the wrapped value to a boolean or returns the given defaultValue
if no conversion is possible.
|
double |
asDouble(double defaultValue)
Returns the double value for the wrapped value or defaultValue if the wrapped value isn't a double and
cannot be converted to one.
|
<E extends Enum<E>> |
asEnum(Class<E> clazz)
Converts the wrapped value to an enum constant of the given clazz.
|
Instant |
asInstant(Instant defaultValue)
Returns the wrapped value as
Instant or defaultValue if the wrapped value
cannot be converted. |
Instant |
asInstantOfEpochMillis(Instant defaultValue)
Converts the wrapped number into an
Instant by assuming the number represents a
"unix timestamp" in milliseconds. |
Instant |
asInstantOfEpochSeconds(Instant defaultValue)
Converts the wrapped number into an
Instant by assuming the number represents a
"unix timestamp" in seconds. |
int |
asInt(int defaultValue)
Returns the int value for the wrapped value or defaultValue if the wrapped value isn't an integer and
cannot be converted to one.
|
LocalDate |
asLocalDate(LocalDate defaultValue)
Returns the wrapped value as
LocalDate or defaultValue if the wrapped value
cannot be converted. |
LocalDateTime |
asLocalDateTime(LocalDateTime defaultValue)
Returns the wrapped value as
LocalDateTime or defaultValue if the wrapped value
cannot be converted. |
LocalDateTime |
asLocalDateTimeOfEpochMillis(LocalDateTime defaultValue)
Converts the wrapped number into
LocalDateTime by assuming the number represents a
"unix timestamp" in milliseconds. |
LocalDateTime |
asLocalDateTimeOfEpochSeconds(LocalDateTime defaultValue)
Converts the wrapped number into
LocalDateTime by assuming the number represents a
"unix timestamp" in seconds. |
LocalTime |
asLocalTime(LocalTime defaultValue)
Returns the wrapped value as
LocalTime or defaultValue if the wrapped value
cannot be converted. |
long |
asLong(long defaultValue)
Returns the long value for the wrapped value or defaultValue if the wrapped value isn't a long and
cannot be converted to one.
|
<T> Optional<T> |
asOptional(Class<T> type)
Boilerplate for
Optional.ofNullable(get(type, null)) . |
Optional<Integer> |
asOptionalInt()
Returns the internal value wrapped as Optional while expecting it to be an integer number.
|
Optional<String> |
asOptionalString()
Returns the internal value wrapped as Optional.
|
String |
asRomanNumeral(int defaultValue)
Tries to convert the wrapped value to a roman numeral representation
This only works if the wrapped value can be converted to int and is >0 and <4000.
|
String |
asSmartRoundedString()
Returns the wrapped data converted to a string like
asString()
while "smart rounding" (NLS.smartRound(double) Double and BigDecimal values. |
String |
asString()
Returns the wrapped data converted to a string or "" if the wrapped value is null
The conversion method used is
NLS.toMachineString(Object) |
String |
asString(String defaultValue)
Returns the wrapped data converted to a string or defaultValue if the wrapped value is null
The conversion method used is
NLS.toMachineString(Object) |
ZonedDateTime |
asZonedDateTime(ZonedDateTime defaultValue)
Returns the wrapped value as
ZonedDateTime or defaultValue if the wrapped value
cannot be converted. |
String |
beforeFirst(String separator)
Returns the substring of the internal value containing everything up to the first occurrence of the given
separator.
|
String |
beforeLast(String separator)
Returns the substring of the internal value containing everything up to the last occurrence of the given
separator.
|
<T> T |
coerce(Class<T> targetClazz,
T defaultValue)
Converts or casts the wrapped object to the given targetClazz
|
boolean |
contains(String needle)
Checks if the given value is filled and contains the given needle in its
string representation
|
String |
eat(int maxNumberOfCharacters)
Cuts and returns the first n given characters of the string representation of this value.
|
boolean |
endsWith(String value)
Checks if the string representation of the wrapped value ends with the given string.
|
boolean |
equals(Object other) |
boolean |
equalsIgnoreCase(Object otherString)
Determines if the string representation of the wrapped value is equal to the string representation of the given
object.
|
Value |
first()
If the underlying data is a
Collection this will return the first element wrapped as value. |
Object |
get()
Returns the wrapped object
|
<V> V |
get(Class<V> clazz,
V defaultValue)
Returns the wrapped value if it is an instance of the given clazz or the defaultValue otherwise.
|
Object |
get(Object defaultValue)
Returns the internal data or the given defaultValue
|
Amount |
getAmount()
Returns the Amount for the wrapped value.
|
BigDecimal |
getBigDecimal()
Returns the BigDecimal value for the wrapped value or null if the wrapped value
isn't a BigDecimal and cannot be converted to one.
|
BigDecimal |
getBigDecimal(BigDecimal defaultValue)
Returns the BigDecimal value for the wrapped value or defaultValue if the wrapped value
isn't a BigDecimal and cannot be converted to one.
|
<E extends Enum<E>> |
getEnum(Class<E> clazz)
Converts the wrapped value to an enum constant of the given clazz.
|
Integer |
getInteger()
Returns the int value for the wrapped value or null if the wrapped value isn't an integer and
cannot be converted to one.
|
Long |
getLong()
Returns the long value for the wrapped value or null if the wrapped value isn't a long and
cannot be converted to one.
|
String |
getString()
Returns the data converted to a string, or null if the wrapped value is null
The conversion method used is
NLS.toMachineString(Object) |
int |
hashCode() |
Value |
ifFilled(Consumer<Value> consumer)
Calls the given consumer with this if the value is filled.
|
Value |
ignore(String... ignoredValues)
Returns a new
Value which will be empty its value equals one of the given ignored values. |
boolean |
in(Object... objects)
Determines if the wrapped value is equal to one of the given objects.
|
static Value |
indexOf(int index,
Collection<?> collection)
Returns the n-th (index-th) element of the given collection.
|
static Value |
indexOf(int index,
Object[] array)
Returns the n-th (index-th) element of the given array.
|
boolean |
is(Class<?>... classes)
Checks if the value implements one of the given classes.
|
boolean |
isEmptyString()
Determines if the wrapped value is an empty string.
|
boolean |
isFilled()
Determines if the wrapped value is not null.
|
boolean |
isNull()
Determines if the wrapped value is null
|
boolean |
isNumeric()
Checks if the current value is numeric (integer or double).
|
String |
left(int length)
Returns the first N (length) characters of the string representation of the wrapped value.
|
int |
length()
Returns the length of the string representation of the wrapped value.
|
<R> Optional<R> |
map(Function<Value,R> mapper)
Returns an optional value computed by the given mapper.
|
boolean |
notIn(Object... objects)
Determines if the wrapped value isn't equal to any of the given objects.
|
static Value |
of(Object data)
Creates a new wrapper for the given data.
|
Value |
prepend(String separator,
Object value)
Returns a value which wraps
value + separator + this
If the current value is empty, the given value is returned (without the separator). |
Value |
regExReplace(String pattern,
String replacement)
Replaces the given regular expression pattern with the given replacement in the string representation
of the wrapped object
|
Value |
replace(String pattern,
String replacement)
Replaces the given pattern with the given replacement in the string representation
of the wrapped object
|
Value |
replaceEmptyWith(Object replacement)
Returns a new Value which will wrap the given value, if the current value is empty.
|
Value |
replaceIfEmpty(Supplier<?> supplier)
Returns a new Value which will wrap the value produced by the given supplier, if the current
value is empty.
|
String |
right(int length)
Returns the last N (length) characters of the string representation of the wrapped value.
|
boolean |
startsWith(String value)
Checks if the string representation of the wrapped value starts with the given string.
|
String |
substring(int startIndex,
int endIndex)
Returns a substring of the string representation of the wrapped value.
|
String |
toLowerCase()
Returns an lowercase version of the string representation of the wrapped value.
|
String |
toString() |
String |
toUpperCase()
Returns an uppercase version of the string representation of the wrapped value.
|
Value |
translate()
Returns a Value containing a translated value using the string representation
of the wrapped value as key.
|
Value |
translate(String lang)
Returns a Value containing a translated value using the string representation
of the wrapped value as key.
|
String |
trim()
Returns a trimmed version of the string representation of the wrapped value.
|
public static final Value EMPTY
@Nonnull public static Value of(@Nullable Object data)
data
- the object wrap@Nonnull public static Value indexOf(int index, @Nullable Collection<?> collection)
index
- the zero based index of the element to fetchcollection
- the collection to pick the element from@Nonnull public static Value indexOf(int index, @Nullable Object[] array)
index
- the zero based index of the element to fetcharray
- the array to pick the element frompublic boolean isNull()
public boolean isEmptyString()
public boolean contains(String needle)
needle
- the substring to searchpublic boolean isFilled()
public Value ifFilled(Consumer<Value> consumer)
consumer
- the consumer to call with this object if it is filled@Nonnull public Value ignore(String... ignoredValues)
Value
which will be empty its value equals one of the given ignored values.ignoredValues
- the list of values which will be replaced by an empty value@Nonnull public Value replaceEmptyWith(@Nullable Object replacement)
replacement
- the value which is used as replacement if this value is empty@Nonnull public Value replaceIfEmpty(@Nonnull Supplier<?> supplier)
supplier
- the supplier used to compute a replacement value if this value is empty@Nonnull public <R> Optional<R> map(@Nonnull Function<Value,R> mapper)
R
- the type of the desired resultmapper
- the function used to convert the value into the desired object@Nonnull public <T> Optional<T> asOptional(@Nonnull Class<T> type)
Optional.ofNullable(get(type, null))
.
Returns the internal value wrapped as Optional.
T
- the expected type of the contents of this valuetype
- the desired type of the datapublic Optional<Integer> asOptionalInt()
If the value is empty or not an integer, an empty Optional will be returned.
public Optional<String> asOptionalString()
If the value is empty, an empty Optional will be returned.
@Nonnull public Value append(@Nullable String separator, @Nullable Object value)
this + separator + value
If the current value is empty, the given value is returned (without the separator). If the given value is an empty string, the current value is returned (without the separator).
separator
- the separator to be put in between the two. If the given value is null, "" is assumedvalue
- the value to be appended to the current value.@Nonnull public Value prepend(@Nullable String separator, @Nullable Object value)
value + separator + this
If the current value is empty, the given value is returned (without the separator). If the given value is an empty string, the current value is returned (without the separator).
separator
- the separator to be put in between the two. If the given value is null, "" is assumedvalue
- the value to be appended to the current value.@Nonnull public String eat(int maxNumberOfCharacters)
Note: This modifies the internal state of this value, since the number of characters is cut from the string representation of the current object and the remaining string is stored as new internal value.
If the wrapped value is empty, "" is returned. If the string representation of the wrapped object is shorter than maxNumberOfCharacters, the remaining string is returned and the internal value is set to null.
This can be used to cut a string into sub strings of a given length:
Value v = Value.of("This is a long string...");
while(v.isFilled()) {
System.out.println("Up to 5 chars of v: "+v.eat(5));
}
maxNumberOfCharacters
- the max length of the string to cut from the wrapped valuepublic boolean isNumeric()
@Nullable public Object get()
@Nonnull public Object get(@Nonnull Object defaultValue)
defaultValue
- the value to use if the inner value is null@Nonnull public Value first()
Collection
this will return the first element wrapped as value. Otherwise
this is returned.public <T> T coerce(Class<T> targetClazz, T defaultValue)
T
- the type to coerce totargetClazz
- the desired class to which the wrapped value should be converted or casted.defaultValue
- the default value if the wrapped object is empty or cannot be cast to the given target.IllegalArgumentException
- if the given targetClazz is unknownpublic <V> V get(Class<V> clazz, V defaultValue)
V
- the expected type of the wrapped valueclazz
- the desired class of the return typedefaultValue
- the value which is returned if the wrapped value is not assignable to the given class.@Nullable public String getString()
The conversion method used is NLS.toMachineString(Object)
@Nonnull public String asString(@Nonnull String defaultValue)
The conversion method used is NLS.toMachineString(Object)
defaultValue
- the value to use if the wrapped object was null@Nonnull public String asString()
The conversion method used is NLS.toMachineString(Object)
@Nonnull public String asSmartRoundedString()
asString()
while "smart rounding" (NLS.smartRound(double)
Double and BigDecimal values.
This method behaves just like asString, except for Double and BigDecimal values
where the output is "smart rounded". Therefore, 12.34 will be formatted as 12.34
but 1.000 will
be formatted as 1
NLS.smartRound(double)
public boolean asBoolean(boolean defaultValue)
To convert a value, Boolean.parseBoolean(String)
is used, where toString
is called on all
non-string objects.
defaultValue
- the value to be used if the wrapped value cannot be converted to a boolean."true"
. Returns false otherwise,
especially if the wrapped value is nullpublic boolean asBoolean()
asBoolean(false)
"true"
. Returns false otherwise,
especially if the wrapped value is nullpublic int asInt(int defaultValue)
If the wrapped value is an Integer or BigDecimal, it is either directly returned or converted
by calling BigDecimal.longValue()
.
Otherwise Integer.parseInt(String)
is called on the string representation of the wrapped value. If
parsing fails, or if the wrapped value was null, the defaultValue will be returned.
defaultValue
- the value to be used, if no conversion to int is possible.public String asRomanNumeral(int defaultValue)
defaultValue
- the value to be converted to roman numeral if the wrapped value can not be converted@Nullable public Integer getInteger()
If the wrapped value is an Integer or BigDecimal, it is either directly returned or converted
by calling BigDecimal.longValue()
.
Otherwise Integer.parseInt(String)
is called on the string representation of the wrapped value. If
parsing fails, or if the wrapped value was null, null will be returned.
public long asLong(long defaultValue)
If the wrapped value is a Long, Integer or BigDecimal,
it is either directly returned or converted by calling BigDecimal.longValue()
.
Otherwise Long.parseLong(String)
is called on the string representation of the wrapped value. If
parsing fails, or if the wrapped value was null, the defaultValue will be returned.
defaultValue
- the value to be used, if no conversion to long is possible.@Nullable public Long getLong()
If the wrapped value is a Long, Integer or BigDecimal, it is either directly
returned or by calling BigDecimal.longValue()
.
Otherwise Long.parseLong(String)
is called on the string representation of the wrapped value. If
parsing fails, or if the wrapped value was null, null will be returned.
public double asDouble(double defaultValue)
If the wrapped value is a Double, Long, Integer or BigDecimal,
it is either directly returned or converted by calling BigDecimal.doubleValue()
.
Otherwise Double.parseDouble(String)
is called on the string representation of the wrapped value. If
parsing fails, or if the wrapped value was null, the defaultValue will be returned.
defaultValue
- the value to be used, if no conversion to double is possible.public LocalDate asLocalDate(LocalDate defaultValue)
LocalDate
or defaultValue if the wrapped value
cannot be converted.
If the wrapped value is an Instant, LocalDateTime, ZonedDateTime,
Date, Calendar, java.sql.Date, Timestamp or long,
it is converted to a LocalDate
.
defaultValue
- the value to be used, if no conversion is possiblepublic LocalDateTime asLocalDateTime(LocalDateTime defaultValue)
LocalDateTime
or defaultValue if the wrapped value
cannot be converted.
If the wrapped value is an Instant, LocalDateTime, ZonedDateTime,
Date, Calendar, java.sql.Date, Timestamp or long,
it is converted to a LocalDateTime
.
defaultValue
- the value to be used, if no conversion is possiblepublic LocalTime asLocalTime(LocalTime defaultValue)
LocalTime
or defaultValue if the wrapped value
cannot be converted.
If the wrapped value is an Instant, LocalDateTime, ZonedDateTime,
Date, Calendar, java.sql.Date, Timestamp or long,
it is converted to a LocalTime
.
defaultValue
- the value to be used, if no conversion is possiblepublic ZonedDateTime asZonedDateTime(ZonedDateTime defaultValue)
ZonedDateTime
or defaultValue if the wrapped value
cannot be converted.
If the wrapped value is an Instant, LocalDateTime, ZonedDateTime,
Date, Calendar, java.sql.Date, Timestamp or long,
it is converted to a ZonedDateTime
.
defaultValue
- the value to be used, if no conversion is possiblepublic Instant asInstant(Instant defaultValue)
Instant
or defaultValue if the wrapped value
cannot be converted.
If the wrapped value is an Instant, LocalDateTime, ZonedDateTime,
Date, Calendar, java.sql.Date, Timestamp or long,
it is converted to an Instant
.
defaultValue
- the value to be used, if no conversion is possiblepublic Instant asInstantOfEpochSeconds(Instant defaultValue)
Instant
by assuming the number represents a
"unix timestamp" in seconds.defaultValue
- used if an invalid or non numeric value was foundpublic Instant asInstantOfEpochMillis(Instant defaultValue)
Instant
by assuming the number represents a
"unix timestamp" in milliseconds.defaultValue
- used if an invalid or non numeric value was foundpublic LocalDateTime asLocalDateTimeOfEpochMillis(LocalDateTime defaultValue)
LocalDateTime
by assuming the number represents a
"unix timestamp" in milliseconds.defaultValue
- used if an invalid or non numeric value was foundpublic LocalDateTime asLocalDateTimeOfEpochSeconds(LocalDateTime defaultValue)
LocalDateTime
by assuming the number represents a
"unix timestamp" in seconds.defaultValue
- used if an invalid or non numeric value was found@Nonnull public BigDecimal getBigDecimal(@Nonnull BigDecimal defaultValue)
If the wrapped value is a BigDecimal, Double, Long or Integer, it is either directly returned or converted by calling java.math.BigDecimal#valueOf.
Otherwise BigDecimal.BigDecimal(String, java.math.MathContext)
is called on the string representation
of the wrapped value (with "," replaced to ".") and MathContext.UNLIMITED. If parsing fails, or if
the wrapped value was null, the defaultValue will be returned.
defaultValue
- the value to be used, if no conversion to BigDecimal is possible.@Nullable public BigDecimal getBigDecimal()
If the wrapped value is a BigDecimal, Double, Long or Integer, it is either directly returned or converted by calling java.math.BigDecimal#valueOf.
Otherwise BigDecimal.BigDecimal(String, java.math.MathContext)
is called on the string representation
of the wrapped value (with "," replaced to ".") and MathContext.UNLIMITED. If parsing fails, or if
the wrapped value was null, the null will be returned.
public Amount getAmount()
If the wrapped value can be converted to a BigDecimal (getBigDecimal(java.math.BigDecimal)
,
an Amount for the result is returned. Otherwise an empty Amount is returned.
getBigDecimal(java.math.BigDecimal)
public <E extends Enum<E>> E asEnum(Class<E> clazz)
E
- the type of the enumclazz
- the type of the enum to usepublic <E extends Enum<E>> Optional<E> getEnum(Class<E> clazz)
E
- to generic type of the enumclazz
- the enum to convert topublic boolean startsWith(@Nonnull String value)
value
- the substring with which the string representation must startpublic boolean endsWith(@Nonnull String value)
value
- the substring with which the string representation must end@Nonnull public String trim()
The conversion method used is asString()
, therefore an empty value will yield ""
.
@Nonnull public String left(int length)
If the wrapped value is null, ""
will be returned. If the string representation is
shorter than length, the whole string is returned.
If length is negative, the string representation without the first N (length)
characters is returned. If the string representation is too short, ""
is returned.
length
- the number of characters to return or to omit (if length is negative)""
if the wrapped value is null@Nonnull public String right(int length)
If the wrapped value is null, ""
will be returned. If the string representation is
shorter than length, the whole string is returned.
If length is negative, the string representation without the last N (length)
characters is returned. If the string representation is too short, ""
is returned.
length
- the number of characters to return or to omit (if length is negative)""
if the wrapped value is null@Nonnull public String afterLast(@Nonnull String separator)
If the separator is not found in the string, or if the internal value is empty, "" is returned.
An example would be:
Value.of("test.tmp.pdf").afterLast("."); // returns "pdf"
separator
- the separator string to search for@Nonnull public String beforeLast(@Nonnull String separator)
If the separator is not found in the string, or if the internal value is empty, "" is returned.
An example would be:
Value.of("test.tmp.pdf").beforeLast("."); // returns "test.tmp"
separator
- the separator string to search for@Nonnull public String afterFirst(@Nonnull String separator)
If the separator is not found in the string, or if the internal value is empty, "" is returned.
An example would be:
Value.of("test.tmp.pdf").afterFirst("."); // returns "tmp.pdf"
separator
- the separator string to search for@Nonnull public String beforeFirst(@Nonnull String separator)
If the separator is not found in the string, or if the internal value is empty, "" is returned.
An example would be:
Value.of("test.tmp.pdf").beforeFirst("."); // returns "test"
separator
- the separator string to search for@Nonnull public String substring(int startIndex, int endIndex)
Returns the substring starting at startIndex and ending at endIndex. If the given
end index is greater than the string length, the complete substring from startIndex to the end of
the string is returned. If the startIndex is greater than the string length, ""
is
returned.
startIndex
- the index of the first character to be included in the sub stringendIndex
- the index of the last character to be included in the sub stringString.substring(int, int)
or ""
if the wrapped valuepublic int length()
@Nonnull public String toUpperCase()
""
if the
wrapped value is null@Nonnull public String toLowerCase()
""
if the
wrapped value is nullpublic boolean is(Class<?>... classes)
classes
- the classes to check against@Nonnull public Value replace(String pattern, String replacement)
pattern
- the pattern to replacereplacement
- the replacement to be used for pattern@Nonnull public Value regExReplace(String pattern, String replacement)
pattern
- the regular expression to replacereplacement
- the replacement to be used for patternpublic boolean in(Object... objects)
Instead of using if (!value.in(...)) {}
consider notIn(Object...)
.
objects
- the set of objects to check againstpublic boolean notIn(Object... objects)
This is the inverse of in(Object...)
objects
- the set of objects to check againstpublic boolean equalsIgnoreCase(Object otherString)
In this case equality does not take differences of upper and lower case characters into account. Therefore
this is boilerplate for asString().equalsIgnoreCase(otherString.toString())
(With proper null checks.)
otherString
- the input to compare againstString.equalsIgnoreCase(String)
@Nonnull @CheckReturnValue public Value translate()
NLS.get(String)
if the string representation of the wrapped value starts with $
.
The dollar sign is skipped when passing the key to NLS. Otherwise this is returned.NLS.get(String)
@Nonnull @CheckReturnValue public Value translate(String lang)
lang
- a two-letter language code for which the translation is requestedNLS.get(String, String)
if the string representation of the wrapped value starts with $
.
The dollar sign is skipped when passing the key to NLS. Otherwise this is returned.NLS.get(String, String)
Copyright © 2018. All rights reserved.