Class NamedSet<T>
- java.lang.Object
-
- org.apache.tapestry5.commons.internal.util.LockSupport
-
- org.apache.tapestry5.internal.util.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 aCaseInsensitiveMap
(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 ownReadWriteLock
.
-
-
Constructor Summary
Constructors Constructor Description NamedSet()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> NamedSet<T>
create()
Convenience method for creating a new, empty set.void
eachValue(Worker<T> worker)
Iterates over the values, passing each in turn to the supplied worker.T
get(java.lang.String name)
Gets the value for the provided name.static <T> T
get(NamedSet<T> set, java.lang.String name)
Convenience method for getting a value from a set that may be null.java.util.Set<java.lang.String>
getNames()
Returns a set of the names of all stored values.static java.util.Set<java.lang.String>
getNames(NamedSet<?> set)
Gets the names in the set, returning an empty set if the NamedSet is null.java.util.Set<T>
getValues()
Returns a set of all the values in the set.static <T> java.util.Set<T>
getValues(NamedSet<T> set)
Returns the values in the set, returning an empty set if the NamedSet is null.void
put(java.lang.String name, T newValue)
Stores a new value into the set, replacing any previous value with the same name.boolean
putIfNew(java.lang.String name, T newValue)
Puts a new value, but only if it does not already exist.-
Methods inherited from class org.apache.tapestry5.commons.internal.util.LockSupport
acquireReadLock, downgradeWriteLockToReadLock, releaseReadLock, releaseWriteLock, takeWriteLock, upgradeReadLockToWriteLock
-
-
-
-
Constructor Detail
-
NamedSet
public NamedSet()
-
-
Method Detail
-
getNames
public java.util.Set<java.lang.String> getNames()
Returns a set of the names of all stored values.
-
get
public T get(java.lang.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(java.lang.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(java.lang.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 blanknewValue
- non-null value to store- Returns:
- true if value stored, false if name already exists
-
get
public static <T> T get(NamedSet<T> set, java.lang.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 nullname
- name to lookup- Returns:
- value from set, or null if not found, or if set is null
-
getNames
public static java.util.Set<java.lang.String> getNames(NamedSet<?> set)
Gets the names in the set, returning an empty set if the NamedSet is null.
-
-