Package com.cedarsoftware.util
Class ClassValueSet
A Set implementation for Class objects that leverages a ClassValue cache for extremely
fast membership tests. This specialized collection is designed for scenarios where you
frequently need to check if a Class is a member of a set.
Performance Advantages
ClassValueSet provides significantly faster contains()
operations compared to standard
Set implementations:
- 2-10x faster than HashSet for membership checks
- 3-15x faster than ConcurrentHashMap.keySet() for concurrent access patterns
- The performance advantage increases with contention (multiple threads)
- Most significant when checking the same classes repeatedly
How It Works
The implementation utilizes Java's ClassValue
mechanism, which is specially optimized
in the JVM through:
- Thread-local caching for reduced contention
- Identity-based lookups (faster than equality checks)
- Special VM support that connects directly to Class metadata structures
- Optimized memory layout that can reduce cache misses
Ideal Use Cases
ClassValueSet is ideal for:
- High read-to-write ratio scenarios (read-mostly workloads)
- Relatively static sets of classes that are checked frequently
- Performance-critical operations in hot code paths
- Security blocklists (checking if a class is forbidden)
- Feature flags or capability testing based on class membership
- Type handling in serialization/deserialization frameworks
Trade-offs
The performance benefits come with some trade-offs:
- Higher memory usage (maintains both a backing set and ClassValue cache)
- Write operations (add/remove) aren't faster and may be slightly slower
- Only Class objects benefit from the optimized lookups
Thread Safety
This implementation is thread-safe for all operations.
Usage Example
// Create a set of blocked classes for security checks
ClassValueSet blockedClasses = ClassValueSet.of(
ClassLoader.class,
Runtime.class,
ProcessBuilder.class
);
// Fast membership check in a security-sensitive context
public void verifyClass(Class<?> clazz) {
if (blockedClasses.contains(clazz)) {
throw new SecurityException("Access to " + clazz.getName() + " is not allowed");
}
}
Important Performance Warning
Wrapping this class with standard collection wrappers like Collections.unmodifiableSet()
will destroy the ClassValue
performance benefits. Always use the raw ClassValueSet
directly
or use the provided unmodifiableView()
method if immutability is required.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
License
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. - See Also:
-
Constructor Summary
ConstructorsConstructorDescriptionCreates an empty ClassValueSet.ClassValueSet
(Collection<? extends Class<?>> c) Creates a ClassValueSet containing the elements of the specified collection. -
Method Summary
Modifier and TypeMethodDescriptionboolean
void
clear()
Removes all classes from this set.boolean
boolean
Returns true if this set equals another object.static ClassValueSet
from
(Collection<? extends Class<?>> collection) Factory method to create a ClassValueSet from an existing Collectionint
hashCode()
Returns the hash code value for this set.boolean
isEmpty()
iterator()
static ClassValueSet
Factory method that creates a set using the provided classesboolean
boolean
retainAll
(Collection<?> c) Retains only the elements in this set that are contained in the specified collection.int
size()
toSet()
Returns a new set containing all elements from this setReturns an unmodifiable view of this set that preserves ClassValue performance benefits.Methods inherited from class java.util.AbstractSet
removeAll
Methods inherited from class java.util.AbstractCollection
addAll, containsAll, toArray, toArray, toString
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream
Methods inherited from interface java.util.Set
addAll, containsAll, spliterator, toArray, toArray
-
Constructor Details
-
ClassValueSet
public ClassValueSet()Creates an empty ClassValueSet. -
ClassValueSet
Creates a ClassValueSet containing the elements of the specified collection.- Parameters:
c
- the collection whose elements are to be placed into this set
-
-
Method Details
-
contains
- Specified by:
contains
in interfaceCollection<Class<?>>
- Specified by:
contains
in interfaceSet<Class<?>>
- Overrides:
contains
in classAbstractCollection<Class<?>>
-
add
- Specified by:
add
in interfaceCollection<Class<?>>
- Specified by:
add
in interfaceSet<Class<?>>
- Overrides:
add
in classAbstractCollection<Class<?>>
-
remove
- Specified by:
remove
in interfaceCollection<Class<?>>
- Specified by:
remove
in interfaceSet<Class<?>>
- Overrides:
remove
in classAbstractCollection<Class<?>>
-
clear
public void clear()Removes all classes from this set.- Specified by:
clear
in interfaceCollection<Class<?>>
- Specified by:
clear
in interfaceSet<Class<?>>
- Overrides:
clear
in classAbstractCollection<Class<?>>
-
size
public int size()- Specified by:
size
in interfaceCollection<Class<?>>
- Specified by:
size
in interfaceSet<Class<?>>
- Specified by:
size
in classAbstractCollection<Class<?>>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmpty
in interfaceCollection<Class<?>>
- Specified by:
isEmpty
in interfaceSet<Class<?>>
- Overrides:
isEmpty
in classAbstractCollection<Class<?>>
-
equals
Returns true if this set equals another object. For sets, equality means they contain the same elements.- Specified by:
equals
in interfaceCollection<Class<?>>
- Specified by:
equals
in interfaceSet<Class<?>>
- Overrides:
equals
in classAbstractSet<Class<?>>
-
hashCode
public int hashCode()Returns the hash code value for this set. The hash code of a set is the sum of the hash codes of its elements.- Specified by:
hashCode
in interfaceCollection<Class<?>>
- Specified by:
hashCode
in interfaceSet<Class<?>>
- Overrides:
hashCode
in classAbstractSet<Class<?>>
-
retainAll
Retains only the elements in this set that are contained in the specified collection.- Specified by:
retainAll
in interfaceCollection<Class<?>>
- Specified by:
retainAll
in interfaceSet<Class<?>>
- Overrides:
retainAll
in classAbstractCollection<Class<?>>
- Parameters:
c
- collection containing elements to be retained in this set- Returns:
- true if this set changed as a result of the call
- Throws:
NullPointerException
- if the specified collection is null
-
iterator
-
toSet
Returns a new set containing all elements from this set- Returns:
- a new set containing the same elements
-
from
Factory method to create a ClassValueSet from an existing Collection- Parameters:
collection
- the source collection- Returns:
- a new ClassValueSet containing the same elements
-
of
Factory method that creates a set using the provided classes- Parameters:
classes
- the classes to include in the set- Returns:
- a new ClassValueSet containing the provided classes
-
unmodifiableView
Returns an unmodifiable view of this set that preserves ClassValue performance benefits. Unlike Collections.unmodifiableSet(), this method returns a view that maintains the fast membership-testing performance for Class elements.- Returns:
- an unmodifiable view of this set with preserved performance characteristics
-