Package net.orbyfied.coldlib.util
Interface Container<V>
- Type Parameters:
V- The value type.
public interface Container<V>
A class responsible for holding a
value and providing access to that
value under certain conditions.
-
Nested Class Summary
Nested Classes -
Method Summary
Modifier and TypeMethodDescriptionasProtected(Predicate<StackTraceElement> predicate, boolean newProtected) static <V> Container<V>atomic()A mutable container which stores the current value in an atomic reference making it thread safe.default CompletableFuture<V>await()default CompletableFuture<V>await(boolean listen) Awaits a value in this container if supported.static <V> Container<V>Create a new container wrapping the provided, already existent container, to introduce awaiting functionality.default <R> Container<R>static <V,R> Container<R> Wraps the given container with a two-way mapping system returning a bi-mapped container, which converts betweenVandRfor setting and getting.default booleancanAwait()Get if you can await/listen a value.Clones this container (copies the value reference) into a new mutable container instance.static <V> Container<V>finalImmutable(V value) Create a new immutable container with the value already set.static <V> Container<V>forking(Container<V> container, BiFunction<Container<V>, V, Container<V>> forkConstructor) Create a new container wrapping the given container instance, which will, when modified, fork the instance, apply modification to the fork, and return the forked instance from the method.static <V> Container<V>Create a new container whose value can only be set once.get()Issue the value currently stored.default <T> TGet the value currently stored casted to typeT.static <V> Container<V>Wraps the provided container to make it immutable.booleanisSet()Get if a value is currently set.issue()Try to issue the value currently stored, return a successful result containing the value if successful, or failed when an error occurs or the operation simply failed.static <V> Container<V>Create a new container which lazy loads the value using the provided supplier when it is first queried.default <R> Container<R>static <V,R> Container<R> Wraps the given container with the provided mapping function to return an immutable container which maps the queried value of typeVto a value of typeT.Get this containers mutability.static <V> Container<V>mutable()Create a new mutable container instance without a value pre-set.static <V> Container<V>mutable(V val) Create a new mutable container instance with a value optionally pre-set.static <V> Container<V>protect(Container<V> container, Predicate<StackTraceElement> predicate, boolean protectNew) Wraps the given container in an access layer protecting it against denied callers according to the provided stack frame predicate.Set the value stored toval.
-
Method Details
-
finalImmutable
Create a new immutable container with the value already set.- Type Parameters:
V- The value type.- Parameters:
value- The final value.- Returns:
- The container instance.
-
futureImmutable
Create a new container whose value can only be set once.- Type Parameters:
V- The value type.- Returns:
- The container instance.
-
mutable
Create a new mutable container instance with a value optionally pre-set. Providing null to the value argument is essentially the same as providing nothing at all.- Type Parameters:
V- The value type.- Parameters:
val- The value to pre-set.- Returns:
- The container instance.
-
mutable
Create a new mutable container instance without a value pre-set. Just callsmutable(Object)with null provided as the pre-set value.- Type Parameters:
V- The value type.- Returns:
- The container instance.
-
protect
static <V> Container<V> protect(Container<V> container, Predicate<StackTraceElement> predicate, boolean protectNew) Wraps the given container in an access layer protecting it against denied callers according to the provided stack frame predicate.- Type Parameters:
V- The value type.- Parameters:
container- The container to wrap.predicate- The predicate to test the frames with.protectNew- If new instances created by any of the method calls of the wrapped instance should automatically be protected under the same conditions.- Returns:
- The new wrapped container.
-
forking
static <V> Container<V> forking(Container<V> container, BiFunction<Container<V>, V, Container<V>> forkConstructor) Create a new container wrapping the given container instance, which will, when modified, fork the instance, apply modification to the fork, and return the forked instance from the method.- Type Parameters:
V- The value type.- Parameters:
container- The container to wrap.forkConstructor- The fork function.- Returns:
- The wrapper container.
-
awaitable
Create a new container wrapping the provided, already existent container, to introduce awaiting functionality. It supports multiple futures awaiting an event at once. If an error is thrown, all futures will be completed exceptionally, after which the error will be rethrown usingThrowables.sneakyThrow(Throwable).- Type Parameters:
V- The value type.- Parameters:
container- The container to wrap.- Returns:
- The new, wrapper container.
-
immutable
Wraps the provided container to make it immutable. This does not affect the original instance, so you can still modify the value using the original instance.- Type Parameters:
V- The value type.- Parameters:
container- The container to wrap.- Returns:
- The immutable container wrapper.
-
lazy
Create a new container which lazy loads the value using the provided supplier when it is first queried. This container is immutable.- Type Parameters:
V- The value type.- Parameters:
supplier- The lazy loader.- Returns:
- The lazy loaded container instance.
-
atomic
A mutable container which stores the current value in an atomic reference making it thread safe.- Type Parameters:
V- The value type.- Returns:
- The atomic container instance.
-
mapped
Wraps the given container with the provided mapping function to return an immutable container which maps the queried value of typeVto a value of typeT.- Type Parameters:
V- The original value type.R- The mapped value type.- Parameters:
container- The container to wrap.function- The V -> R mapper.- Returns:
- The mapping container.
-
biMapped
static <V,R> Container<R> biMapped(Container<V> container, Function<V, R> toFunction, Function<R, V> fromFunction) Wraps the given container with a two-way mapping system returning a bi-mapped container, which converts betweenVandRfor setting and getting. This container retains the mutability of the wrapped container, meaning it can be mutable.- Type Parameters:
V- The original value type.R- The mapped value type.- Parameters:
container- The container to wrap.toFunction- The V -> R function.fromFunction- The R -> V function.- Returns:
- The mapping container.
-
get
V get()Issue the value currently stored. This operation doesn't necessarily return the exact currently stored value. Wrapper containers may (often do) override this method to lazy processing when called.- Returns:
- The value of type
V
-
issue
Try to issue the value currently stored, return a successful result containing the value if successful, or failed when an error occurs or the operation simply failed. This operation doesn't necessarily return the exact currently stored value. Wrapper containers may (often do) override this method to lazy processing when called. The default implementation callsget()and catches any errors it might throw to produce a result.- Returns:
- The result.
-
getAs
Get the value currently stored casted to typeT.- Type Parameters:
T- The target type.- Parameters:
tClass- The target type class.- Returns:
- The casted value.
- Throws:
ClassCastException- If V can not be used as T.
-
isSet
boolean isSet()Get if a value is currently set.- Returns:
- If a value is currently set.
-
set
Set the value stored toval. This might not affect the current instance and instead return a newContainerbased on the implementation.- Parameters:
val- The value to set.- Returns:
- This or a new instance with the new value.
- Throws:
UnsupportedOperationException- If setting is unsupported.
-
mutability
Container.Mutability mutability()Get this containers mutability. This may depends on the current state of the container, so using an old value may have unexpected effects.- Returns:
- The active mutability settings.
-
canAwait
default boolean canAwait()Get if you can await/listen a value.- Returns:
- True/false.
-
await
Awaits a value in this container if supported. If listen is set, it won't complete early if a value is already set. Otherwise, you might get a completed future if a value was already set. Check if it is supported usingcanAwait(). By default, it will return a completed future if possible, otherwise it will throw anUnsupportedOperationException.- Parameters:
listen- If it should listen for a value, or instead already complete if a value is set.- Returns:
- The future.
- Throws:
UnsupportedOperationException- If awaiting is unsupported.
-
await
- See Also:
-
cloneMutable
Clones this container (copies the value reference) into a new mutable container instance.- Returns:
- The new instance.
-
asProtected
-
asForking
-
immutable
-
map
-
biMap
-
awaitable
-