Class NamedSet<T>

  • Type Parameters:
    T - the type of value stored

    public class NamedSet<T>
    extends LockSupport
    Simple, thread-safe associative array that relates a name to a value. Names are case-insensitive. This is optimized to use less memory (than a CaseInsensitiveMap (it uses a singly-liked list), though the cost of a lookup is more expensive. However, this is a good match against many of the structures inside a page instance, where most lookups occur only during page constructions, and the number of values is often small. Each NameSet has its own ReadWriteLock.
    • Method Detail

      • getNames

        public Set<StringgetNames()
        Returns a set of the names of all stored values.
      • getValues

        public Set<TgetValues()
        Returns a set of all the values in the set.
      • get

        public T get(String name)
        Gets the value for the provided name.
        Parameters:
        name - used to locate the value
        Returns:
        the value, or null if not found
      • put

        public void put(String name,
                        T newValue)
        Stores a new value into the set, replacing any previous value with the same name. Name comparisons are case insensitive.
        Parameters:
        name - to store the value. May not be blank.
        newValue - non-null value to store
      • eachValue

        public void eachValue(Worker<T> worker)
        Iterates over the values, passing each in turn to the supplied worker.
        Parameters:
        worker - performs an operation on, or using, the value
      • putIfNew

        public boolean putIfNew(String name,
                                T newValue)
        Puts a new value, but only if it does not already exist.
        Parameters:
        name - name to store (comparisons are case insensitive) may not be blank
        newValue - non-null value to store
        Returns:
        true if value stored, false if name already exists
      • create

        public static <T> NamedSet<T> create()
        Convenience method for creating a new, empty set.
      • get

        public static <T> T get(NamedSet<T> set,
                                String name)
        Convenience method for getting a value from a set that may be null.
        Type Parameters:
        T -
        Parameters:
        set - set to search, may be null
        name - name to lookup
        Returns:
        value from set, or null if not found, or if set is null
      • getNames

        public static Set<StringgetNames(NamedSet<?> set)
        Gets the names in the set, returning an empty set if the NamedSet is null.
      • getValues

        public static <T> Set<T> getValues(NamedSet<T> set)
        Returns the values in the set, returning an empty set if the NamedSet is null.