Interface ThreadLocalDirectory.Updater<AGGREGATOR,SAMPLE>

Type Parameters:
AGGREGATOR - the type of the data container to produce
SAMPLE - the type of the incoming data to store in the container.
All Known Subinterfaces:
ThreadLocalDirectory.ObservableUpdater<AGGREGATOR,SAMPLE>
Enclosing class:
ThreadLocalDirectory<AGGREGATOR,SAMPLE>

public static interface ThreadLocalDirectory.Updater<AGGREGATOR,SAMPLE>
Factory interface to create the data container for each generation of samples, and putting data into it.

The method for actual insertion of a single sample into the current data generation exists separate from LocalInstance.AGGREGATOR to make it possible to use e.g. Integer and List as AGGREGATOR types.

The allocation and sampling is placed in the same class, since you always need to implement both.

  • Method Details

    • createGenerationInstance

      AGGREGATOR createGenerationInstance(AGGREGATOR previous)
      Create data container to receive produced data. This is invoked once on every instance every time ThreadLocalDirectory.fetch() is invoked. This might be an empty list, creating a new counter set to zero, or even copying the current state of LocalInstance.current. LocalInstance.current will be set to the value received from this factory after invocation this method.

      The first time this method is invoked for a thread, previous will be null.

      If using mutable objects, an implementation should always create a new instance in this method, as the previous data generation will be transmitted to the consuming thread. This obviously does not matter if using immutable (value) objects.

      Examples:

      Using a mutable aggregator (a list of integers):

       if (previous == null) {
           return new ArrayList<Integer>();
       } else {
           return new ArrayList<Integer>(previous.size());
       }
       

      Using an immutable aggregator (an integer):

       return Integer.valueOf(0);
       
      Returns:
      a fresh structure to receive data
    • update

      AGGREGATOR update(AGGREGATOR current, SAMPLE x)
      Insert a data element of type S into the current generation of data carrier T. This could be e.g. adding to a list, putting into a local histogram or increasing a counter.

      The method may or may not return a fresh instance of the current value for each invocation, if using a mutable aggregator the typical case will be returning the same instance for the new and old value of current, while if using an immutable aggregator, one is forced to return new instances.

      Examples:

      Using a mutable aggregator (a list of instances of type SAMPLE):

       current.add(x);
       return current;
       

      Using an immutable aggregator (Integer) while also using Integer as type for SAMPLE:

       return Integer.valueOf(current.intValue() + x.intValue());
       
      Parameters:
      current - the current generation's data container
      x - the data to insert
      Returns:
      the new current value, may be the same as previous