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>asProtected(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.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 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()Get the value currently stored.default <T> TGet the value currently stored casted to typeT.booleanisSet()Get if a value is currently set.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.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.
-
asProtected
static <V> Container<V> asProtected(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.
-
get
V get()Get the value currently stored.- Returns:
- The value of type
V
-
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
-