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 TypeMethodDescriptionbooleanvoidclear()Removes all classes from this set.booleanbooleanReturns true if this set equals another object.static ClassValueSetfrom(Collection<? extends Class<?>> collection) Factory method to create a ClassValueSet from an existing CollectioninthashCode()Returns the hash code value for this set.booleanisEmpty()iterator()static ClassValueSetFactory method that creates a set using the provided classesbooleanbooleanretainAll(Collection<?> c) Retains only the elements in this set that are contained in the specified collection.intsize()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
removeAllMethods inherited from class java.util.AbstractCollection
addAll, containsAll, toArray, toArray, toStringMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArrayMethods 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:
containsin interfaceCollection<Class<?>>- Specified by:
containsin interfaceSet<Class<?>>- Overrides:
containsin classAbstractCollection<Class<?>>
-
add
- Specified by:
addin interfaceCollection<Class<?>>- Specified by:
addin interfaceSet<Class<?>>- Overrides:
addin classAbstractCollection<Class<?>>
-
remove
- Specified by:
removein interfaceCollection<Class<?>>- Specified by:
removein interfaceSet<Class<?>>- Overrides:
removein classAbstractCollection<Class<?>>
-
clear
public void clear()Removes all classes from this set.- Specified by:
clearin interfaceCollection<Class<?>>- Specified by:
clearin interfaceSet<Class<?>>- Overrides:
clearin classAbstractCollection<Class<?>>
-
size
public int size()- Specified by:
sizein interfaceCollection<Class<?>>- Specified by:
sizein interfaceSet<Class<?>>- Specified by:
sizein classAbstractCollection<Class<?>>
-
isEmpty
public boolean isEmpty()- Specified by:
isEmptyin interfaceCollection<Class<?>>- Specified by:
isEmptyin interfaceSet<Class<?>>- Overrides:
isEmptyin classAbstractCollection<Class<?>>
-
equals
Returns true if this set equals another object. For sets, equality means they contain the same elements.- Specified by:
equalsin interfaceCollection<Class<?>>- Specified by:
equalsin interfaceSet<Class<?>>- Overrides:
equalsin 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:
hashCodein interfaceCollection<Class<?>>- Specified by:
hashCodein interfaceSet<Class<?>>- Overrides:
hashCodein classAbstractSet<Class<?>>
-
retainAll
Retains only the elements in this set that are contained in the specified collection.- Specified by:
retainAllin interfaceCollection<Class<?>>- Specified by:
retainAllin interfaceSet<Class<?>>- Overrides:
retainAllin 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
-