- Type Parameters:
K- key type of theparent data.V- values type of theparent data.O- type of the contained object.
- All Superinterfaces:
OptionalValue<O>
- All Known Implementing Classes:
ContainerImpl,NonExistentContainer
This class holds a value from an
The following code will do nothing if the key "some_value" does not exist. It throws an exception if the key does exist, but its value is
AbstractData (called parent data). It
offers several methods to process this value. All these methods will not have any effect on the parent data.
The value can be accessed using the OptionalValue.get() method and processed with the process(Consumer) method.
Code examples
The following code will throw an exception if the json did not contain a key "some_key" or if its value wasnull.
Otherwise, it will just print the value of "some_key":
JsonParser parser = new JsonParser();
SOData data = parser.parseReader(new StringReader("{...}"));
data.getContainer("some_key")
.requireNotNull()
.process((Object o) -> System.out.println("some_key: " + o));
The following code will do nothing if the key "some_value" does not exist. It throws an exception if the key does exist, but its value is
null. And it will print the value of "some_key" if this exists and its value is not
null:
JsonParser parser = new JsonParser();
SOData data = parser.parseReader(new StringReader("{...}"));
data.getContainer("some_key")
.ifExists()
.requireNotNull()
.process((Object o) -> System.out.println("some_key: " + o));
-
Method Summary
Modifier and TypeMethodDescriptiondefault @NotNull ListContainer<Object>asList()cast()Casts the value toContainer.castAndConvert(@NotNull Converter<C, R> converter) Casts the value toContainerand converts it with givenconverter.castAndConvertWithException(@NotNull ExceptionConverter<C, R, E> converter) Casts the value toContainerand converts it with givenconverter.createNewContainer(N newValue) Creates a newContainerwith given value.<T> @NotNull ListContainer<T>createNewListContainer(@Nullable List<T> newValue) Creates a newListContainerwith given value.getKey()@NotNull AbstractData<K,V> ifExists()If the value does notexist, a newNonExistentContainerwill be returned.This method is especially useful in combination withifExists()andrequireNotNull().requireNotNull(ExceptionSupplier<K, AbstractData<K, V>, E> supplier) Methods inherited from interface de.linusdev.data.OptionalValue
exists, get, getAs, isNull
-
Method Details
-
getKey
- Returns:
keyof the original valuesparent data.
-
getParentData
- Returns:
parent data.
-
createNewContainer
Creates a newContainerwith given value.- Type Parameters:
N- the type of the new value- Parameters:
newValue- the new value- Returns:
- a new
Container
-
createNewListContainer
@Internal @NotNull <T> @NotNull ListContainer<T> createNewListContainer(@Nullable @Nullable List<T> newValue) Creates a newListContainerwith given value.- Type Parameters:
T- the list-element type- Parameters:
newValue- the list value- Returns:
- a new
ListContainer
-
requireNotNull
@NotNull default <E extends Throwable> @NotNull Container<K,V, requireNotNullO> (ExceptionSupplier<K, AbstractData<K, throws EV>, E> supplier) - Type Parameters:
E- exception thrown if the value isnull.- Parameters:
supplier-ExceptionSupplierto supply an exception of your choice.- Returns:
- this
- Throws:
E- if the value isnull(OptionalValue.get()returnsnull).
-
requireNotNull
- Returns:
- this
- Throws:
NullPointerException- if the value isnull(OptionalValue.get()returnsnull).
-
asList
- Returns:
ListContaineras specified above.- Throws:
ClassCastException- if the value is not aListofObjects.
-
cast
Casts the value toContainer.- Type Parameters:
C- type to cast to.- Returns:
- a new
Containerwith the new cast value. - Throws:
ClassCastException- if the value is not of typeContainer.
-
castAndConvert
@NotNull default <C,R> @NotNull Container<K,V, castAndConvertR> (@NotNull @NotNull Converter<C, R> converter) Casts the value toContainerand converts it with givenconverter.- Type Parameters:
C- type to cast to.R- type toConverter.convert(Object)to.- Parameters:
converter- the converter toConverter.convert(Object)fromContainertoContainer.- Returns:
- a new
Containerwith the new cast and converted value. - Throws:
ClassCastException- if the value is not of typeContainer.
-
castAndConvertWithException
@NotNull default <C,R, @NotNull Container<K,E extends Throwable> V, castAndConvertWithExceptionR> (@NotNull @NotNull ExceptionConverter<C, R, throws EE> converter) Casts the value toContainerand converts it with givenconverter. The converter can throw an exception.- Type Parameters:
C- type to cast to.R- type toConverter.convert(Object)to.E- exception your converter may throw.- Parameters:
converter- the converter toExceptionConverter.convert(Object)fromContainertoContainer.- Returns:
- a new
Containerwith the new cast and converted value. - Throws:
E- if your converter throws this exception
-
ifExists
If the value does notexist, a newNonExistentContainerwill be returned. All Operations on this container will have no effect. This means:-
OptionalValue.get()will throw aNonExistentException. -
process(Consumer)will not execute the givenConsumer. -
requireNotNull()orrequireNotNull(ExceptionSupplier)will never throw an exception.
If the value does
exist, theContaineritself (this) is returned.This method should be mainly used in combination with the
process(Consumer)method and then functions similar toAbstractData.processIfContained(Object, Consumer), but can be combined with all other container methods.- Returns:
Container
-
-
process
This method is especially useful in combination withifExists()andrequireNotNull().- Parameters:
consumer-Consumerto process the value.- Returns:
- this
-