Class TypeKey<T>

java.lang.Object
dev.mintychochip.mint.TypeKey<T>
Type Parameters:
T - the type this key represents

@AvailableSince("1.3.6") public abstract class TypeKey<T> extends Object
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

      @NotNull @AvailableSince("1.3.6") public @NotNull Type getType()
      Gets the captured type.
      Returns:
      the type represented by this key
    • getRawClass

      @NotNull @AvailableSince("1.3.6") public @NotNull Class<T> getRawClass()
      Gets the raw class of the captured type.
      Returns:
      the raw class
    • equals

      @AvailableSince("1.3.6") public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      @AvailableSince("1.3.6") public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      @AvailableSince("1.3.6") public String toString()
      Overrides:
      toString in class Object
    • 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