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.
  • Method Details

    • finalImmutable

      static <V> Container<V> finalImmutable(V value)
      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

      static <V> Container<V> futureImmutable()
      Create a new container whose value can only be set once.
      Type Parameters:
      V - The value type.
      Returns:
      The container instance.
    • mutable

      static <V> Container<V> mutable(V val)
      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

      static <V> Container<V> mutable()
      Create a new mutable container instance without a value pre-set. Just calls mutable(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

      static <V> Container<V> awaitable(Container<V> container)
      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 using Throwables.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

      default <T> T getAs(Class<T> tClass)
      Get the value currently stored casted to type T.
      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

      Container<V> set(V val)
      Set the value stored to val. This might not affect the current instance and instead return a new Container based 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

      default CompletableFuture<V> await(boolean listen)
      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 using canAwait(). By default, it will return a completed future if possible, otherwise it will throw an UnsupportedOperationException.
      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

      default CompletableFuture<V> await()
      See Also:
    • cloneMutable

      default Container<V> cloneMutable()
      Clones this container (copies the value reference) into a new mutable container instance.
      Returns:
      The new instance.
    • asProtected

      default Container<V> asProtected(Predicate<StackTraceElement> predicate, boolean newProtected)
    • asForking

      default Container<V> asForking(BiFunction<Container<V>,V,Container<V>> forkConstructor)