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
    Modifier and Type
    Interface
    Description
    static enum 
    Specifies the way
  • Method Summary

    Modifier and Type
    Method
    Description
    default Container<V>
    asForking(BiFunction<Container<V>,V,Container<V>> forkConstructor)
     
    default Container<V>
    asProtected(Predicate<StackTraceElement> predicate, boolean newProtected)
     
    static <V> Container<V>
    A mutable container which stores the current value in an atomic reference making it thread safe.
     
    await(boolean listen)
    Awaits a value in this container if supported.
    default Container<V>
     
    static <V> Container<V>
    awaitable(Container<V> container)
    Create a new container wrapping the provided, already existent container, to introduce awaiting functionality.
    default <R> Container<R>
    biMap(Function<V,R> toFunction, Function<R,V> fromFunction)
     
    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 between V and R for setting and getting.
    default boolean
    Get if you can await/listen a value.
    default Container<V>
    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> T
    getAs(Class<T> tClass)
    Get the value currently stored casted to type T.
    default Container<V>
     
    static <V> Container<V>
    immutable(Container<V> container)
    Wraps the provided container to make it immutable.
    boolean
    Get if a value is currently set.
    default Result<V>
    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>
    lazy(Supplier<V> supplier)
    Create a new container which lazy loads the value using the provided supplier when it is first queried.
    default <R> Container<R>
    map(Function<V,R> function)
     
    static <V, R> Container<R>
    mapped(Container<V> container, Function<V,R> function)
    Wraps the given container with the provided mapping function to return an immutable container which maps the queried value of type V to a value of type T.
    Get this containers mutability.
    static <V> Container<V>
    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(V val)
    Set the value stored to val.
  • 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.
    • 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

      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.
    • immutable

      static <V> Container<V> immutable(Container<V> container)
      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

      static <V> Container<V> lazy(Supplier<V> supplier)
      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

      static <V> Container<V> 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

      static <V, R> Container<R> mapped(Container<V> container, Function<V,R> function)
      Wraps the given container with the provided mapping function to return an immutable container which maps the queried value of type V to a value of type T.
      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 between V and R for 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

      default Result<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 calls get() and catches any errors it might throw to produce a result.
      Returns:
      The result.
    • 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)
    • immutable

      default Container<V> immutable()
    • map

      default <R> Container<R> map(Function<V,R> function)
    • biMap

      default <R> Container<R> biMap(Function<V,R> toFunction, Function<R,V> fromFunction)
    • awaitable

      default Container<V> awaitable()