Package dev.mintychochip.mint
Class TypeKey<T>
java.lang.Object
dev.mintychochip.mint.TypeKey<T>
- Type Parameters:
T- the type this key represents
A type-safe key that captures generic type information at runtime.
This class uses the "super type token" pattern to capture generic type parameters that would normally be erased at runtime. By creating an anonymous subclass, the generic type information is preserved in the superclass reference.
Usage
// Create type keys for different generic types
TypeKey<String> stringKey = new TypeKey<String>() {};
TypeKey<List<String>> stringListKey = new TypeKey<List<String>>() {};
TypeKey<Map<String, Integer>> mapKey = new TypeKey<Map<String, Integer>>() {};
// Use as registry keys
registry.register(stringKey, stringPreferenceType);
registry.register(stringListKey, stringListPreferenceType);
- Since:
- 1.3.6
-
Constructor Details
-
TypeKey
@AvailableSince("1.3.6") protected TypeKey()Constructs a new TypeKey.This constructor must be called from an anonymous subclass to capture the generic type parameter. For example:
new TypeKey<String>() {}- Throws:
IllegalStateException- if not called from a properly parameterized subclass
-
-
Method Details
-
getType
Gets the captured type.- Returns:
- the type represented by this key
-
getRawClass
Gets the raw class of the captured type.- Returns:
- the raw class
-
equals
-
hashCode
@AvailableSince("1.3.6") public int hashCode() -
toString
-
of
@NotNull @AvailableSince("1.3.6") public static <T> @NotNull TypeKey<T> of(@NotNull @NotNull Class<T> clazz) Creates a TypeKey for a simple (non-generic) class.- Type Parameters:
T- the type- Parameters:
clazz- the class- Returns:
- a TypeKey for the class
-