类 Shared<T>

java.lang.Object
pkg.exoad.poprock.core.Shared<T>

public class Shared<T> extends Object

Shared Object Pooling

A Shared Object at the simplest represents an object that can perform basic atomicity operations, such as modifying a non-final variable inside a lambda.

However, the main quirk of a Shared Object is that all allocated SharedObjects are placed inside a pool. Similar to Java's String pool, if multiple objects have the same value, only one instance of it will exist in that pool, and one pointer. This method is not meant to save on memory, nor does it even achieve such a goal; instead, it is focused on making sure multiple instances of an object that can hold the same value don't exist at the same time. For example, in concurrency, Shared objects will help to maintain atomicity and mitigate race conditions.

The Shared Class itself does not hold the true value of the object, instead a singular ID or numerical pointer to the value in the pool. Furthermore, the usage of this class is very very simple and acts just like a normal Wrapper class that just exists to get rid of the "modification of non-final variable in a lambda" error thrown by Java. Just look at setValue(Object) and getValue() they can't be anymore easier! All the internal complexity are hidden away

Example Usage

 public record FooBar(int integerValue,String stringValue) {}
 // ...
 Shared fooBar=Shared.of(new FooBar(30,"Hello World"));
 fooBar.setValue(new FooBar(100,"Foo!"));
 System.out.println(fooBar.getValue().stringValue());
 
作者:
Jack Meng
  • 字段详细资料

  • 构造器详细资料

    • Shared

      private Shared(T e)
      Creates a new Shared Object pool with default initializations, mainly hooking up this Shared object to the pool.
      参数:
      e - The initial value
  • 方法详细资料

    • of

      public static <E> Shared<E> of(E value)
      Creator function used to return a new shared object handler.
      类型参数:
      E - Type of the value
      参数:
      value - The value
      返回:
      The Shared object handler
    • weakOf

      private static <T> WeakReference<T> weakOf(T obj)
    • setValue

      public void setValue(T e)
      Modifies the current value pointed to
      参数:
      e - value pointed to
    • expirePoolReserve

      public void expirePoolReserve()
    • getValue

      public T getValue()
      Please note that this method can return a nullable object because of garbage collection WeakReference
      返回:
      The shared object
    • computeEquality

      public static <V extends Shared<?>> boolean computeEquality(V a, V b)
    • equals

      public boolean equals(Object r)
      覆盖:
      equals 在类中 Object