Class CollectionTypeUtils

java.lang.Object
tech.guilhermekaua.spigotboot.core.utils.CollectionTypeUtils

public final class CollectionTypeUtils extends Object
  • Constructor Details

    • CollectionTypeUtils

      public CollectionTypeUtils()
  • Method Details

    • extractCollectionTypeInfo

      @Nullable public static @Nullable CollectionTypeUtils.CollectionTypeInfo extractCollectionTypeInfo(@NotNull @NotNull Type type)
      Extracts collection type information from a Type, identifying both the collection class and its element type. This method analyzes parameterized types to determine if they represent a collection (e.g., List, Set, Collection) and extracts the generic element type. The method handles:
      • Parameterized collection types (e.g., List<String>, Set<Service>)
      • Wildcard types with upper bounds (e.g., List<? extends Service>)
      • Raw types and non-collection types return null
      Examples:
      • extractCollectionTypeInfo(new TypeToken<List<String>>(){}.getType()) returns CollectionTypeInfo with collection class List.class and element type String.class
      • extractCollectionTypeInfo(new TypeToken<Set<Service>>(){}.getType()) returns CollectionTypeInfo with collection class Set.class and element type Service.class
      • extractCollectionTypeInfo(String.class) returns null (not a collection)
      • extractCollectionTypeInfo(List.class) returns null (raw type, no generics)
      Parameters:
      type - the type to analyze, not null
      Returns:
      a CollectionTypeUtils.CollectionTypeInfo containing the collection class and element type, or null if the type is not a parameterized collection type
      Throws:
      NullPointerException - if type is null
    • getRawClass

      @Nullable public static <T> @Nullable Class<T> getRawClass(@NotNull @NotNull Type type)
      Extracts the raw class from a Type, handling both simple classes and parameterized types. This method is useful when you need to work with the actual class type, ignoring generic type parameters. For parameterized types (e.g., List<String>), it returns the raw type (e.g., List.class). For simple class types, it returns the class itself. Examples:
      • getRawClass(String.class) returns String.class
      • getRawClass(new TypeToken<List<String>>(){}.getType()) returns List.class
      • getRawClass(new TypeToken<Map<String, Integer>>(){}.getType()) returns Map.class
      Type Parameters:
      T - the type parameter for the returned class
      Parameters:
      type - the type to extract the raw class from, not null
      Returns:
      the raw class, or null if the type cannot be converted to a class
      Throws:
      NullPointerException - if type is null
    • extractMapTypeInfo

      @Nullable public static @Nullable CollectionTypeUtils.MapTypeInfo extractMapTypeInfo(@NotNull @NotNull Type type)
      Extracts map type information from a Type, identifying both the key and value types.

      This method analyzes parameterized types to determine if they represent a Map and extracts the generic key and value types.

      The method handles:

      • Parameterized map types (e.g., Map<String, Location>)
      • Wildcard types with upper bounds (e.g., Map<String, ? extends Location>)
      • Raw types and non-map types return null
      Parameters:
      type - the type to analyze, not null
      Returns:
      a CollectionTypeUtils.MapTypeInfo containing the key and value types, or null if the type is not a parameterized map type
      Throws:
      NullPointerException - if type is null