@Immutable public class GenericType<T> extends Object
This is used by type codecs to indicate which Java types they accept (TypeCodec.accepts(GenericType)
), and by generic getters and setters (such as GettableByIndex.get(int, GenericType)
in the driver's query API.
There are various ways to build instances of this class:
By using one of the static factory methods:
GenericType<List<String>> stringListType = GenericType.listOf(String.class);
By using an anonymous class:
GenericType<Foo<Bar>> fooBarType = new GenericType<Foo<Bar>>(){};
In a generic method, by using where(GenericTypeParameter, GenericType)
to substitute
free type variables with runtime types:
<T> GenericType<Optional<T>> optionalOf(GenericType<T> elementType) {
return new GenericType<Optional<T>>() {}
.where(new GenericTypeParameter<T>() {}, elementType);
}
...
GenericType<Optional<List<String>>> optionalStringListType = optionalOf(GenericType.listOf(String.class));
You are encouraged to store and reuse these instances.
Note that this class is a thin wrapper around Guava's TypeToken
. The only reason why
TypeToken
is not used directly is because Guava is not exposed in the driver's public API
(it's used internally, but shaded).
Modifier and Type | Field and Description |
---|---|
static GenericType<BigDecimal> |
BIG_DECIMAL |
static GenericType<BigInteger> |
BIG_INTEGER |
static GenericType<Boolean> |
BOOLEAN |
static GenericType<Byte> |
BYTE |
static GenericType<ByteBuffer> |
BYTE_BUFFER |
static GenericType<CqlDuration> |
CQL_DURATION |
static GenericType<Double> |
DOUBLE |
static GenericType<Duration> |
DURATION |
static GenericType<Float> |
FLOAT |
static GenericType<InetAddress> |
INET_ADDRESS |
static GenericType<Instant> |
INSTANT |
static GenericType<Integer> |
INTEGER |
static GenericType<LocalDate> |
LOCAL_DATE |
static GenericType<LocalTime> |
LOCAL_TIME |
static GenericType<Long> |
LONG |
static GenericType<Short> |
SHORT |
static GenericType<String> |
STRING |
static GenericType<TupleValue> |
TUPLE_VALUE |
static GenericType<UdtValue> |
UDT_VALUE |
static GenericType<UUID> |
UUID |
static GenericType<ZonedDateTime> |
ZONED_DATE_TIME |
Modifier | Constructor and Description |
---|---|
protected |
GenericType() |
Modifier and Type | Method and Description |
---|---|
com.datastax.oss.driver.shaded.guava.common.reflect.TypeToken<T> |
__getToken()
This method is for internal use, DO NOT use it from client code.
|
boolean |
equals(Object other) |
GenericType<?> |
getComponentType()
Returns the array component type if this type represents an array (
int[] , T[] ,
<? extends Map<String, Integer>[]> etc.), or else null is returned. |
Class<? super T> |
getRawType()
Returns the raw type of
T . |
GenericType<? extends T> |
getSubtype(Class<?> subclass)
Returns subtype of
this with subclass as the raw class. |
GenericType<? super T> |
getSupertype(Class<? super T> superclass)
Returns the generic form of
superclass . |
Type |
getType()
Returns the represented type.
|
int |
hashCode() |
boolean |
isArray()
Returns true if this type is known to be an array type, such as
int[] , T[] ,
<? extends Map<String, Integer>[]> etc. |
boolean |
isPrimitive()
Returns true if this type is one of the nine primitive types (including
void ). |
boolean |
isSubtypeOf(GenericType<?> type)
Returns true if this type is a subtype of the given
type . |
boolean |
isSupertypeOf(GenericType<?> type)
Returns true if this type is a supertype of the given
type . |
static <T> GenericType<List<T>> |
listOf(Class<T> elementType) |
static <T> GenericType<List<T>> |
listOf(GenericType<T> elementType) |
static <K,V> GenericType<Map<K,V>> |
mapOf(Class<K> keyType,
Class<V> valueType) |
static <K,V> GenericType<Map<K,V>> |
mapOf(GenericType<K> keyType,
GenericType<V> valueType) |
static <T> GenericType<T> |
of(Class<T> type) |
static GenericType<?> |
of(Type type) |
static <T> GenericType<Set<T>> |
setOf(Class<T> elementType) |
static <T> GenericType<Set<T>> |
setOf(GenericType<T> elementType) |
String |
toString() |
GenericType<T> |
unwrap()
Returns the corresponding primitive type if this is a wrapper type; otherwise returns
this itself. |
<X> GenericType<T> |
where(GenericTypeParameter<X> freeVariable,
Class<X> actualType)
Substitutes a free type variable with an actual type.
|
<X> GenericType<T> |
where(GenericTypeParameter<X> freeVariable,
GenericType<X> actualType)
Substitutes a free type variable with an actual type.
|
GenericType<T> |
wrap()
Returns the corresponding wrapper type if this is a primitive type; otherwise returns
this itself. |
public static final GenericType<Boolean> BOOLEAN
public static final GenericType<Byte> BYTE
public static final GenericType<Double> DOUBLE
public static final GenericType<Float> FLOAT
public static final GenericType<Integer> INTEGER
public static final GenericType<Long> LONG
public static final GenericType<Short> SHORT
public static final GenericType<Instant> INSTANT
public static final GenericType<ZonedDateTime> ZONED_DATE_TIME
public static final GenericType<LocalDate> LOCAL_DATE
public static final GenericType<LocalTime> LOCAL_TIME
public static final GenericType<ByteBuffer> BYTE_BUFFER
public static final GenericType<String> STRING
public static final GenericType<BigInteger> BIG_INTEGER
public static final GenericType<BigDecimal> BIG_DECIMAL
public static final GenericType<UUID> UUID
public static final GenericType<InetAddress> INET_ADDRESS
public static final GenericType<CqlDuration> CQL_DURATION
public static final GenericType<TupleValue> TUPLE_VALUE
public static final GenericType<UdtValue> UDT_VALUE
public static final GenericType<Duration> DURATION
@NonNull public static <T> GenericType<T> of(@NonNull Class<T> type)
@NonNull public static GenericType<?> of(@NonNull Type type)
@NonNull public static <T> GenericType<List<T>> listOf(@NonNull Class<T> elementType)
@NonNull public static <T> GenericType<List<T>> listOf(@NonNull GenericType<T> elementType)
@NonNull public static <T> GenericType<Set<T>> setOf(@NonNull Class<T> elementType)
@NonNull public static <T> GenericType<Set<T>> setOf(@NonNull GenericType<T> elementType)
@NonNull public static <K,V> GenericType<Map<K,V>> mapOf(@NonNull Class<K> keyType, @NonNull Class<V> valueType)
@NonNull public static <K,V> GenericType<Map<K,V>> mapOf(@NonNull GenericType<K> keyType, @NonNull GenericType<V> valueType)
public final boolean isSupertypeOf(@NonNull GenericType<?> type)
type
. "Supertype" is defined
according to the rules for type
arguments introduced with Java generics.public final boolean isSubtypeOf(@NonNull GenericType<?> type)
type
. "Subtype" is defined
according to the rules for type
arguments introduced with Java generics.public final boolean isArray()
int[]
, T[]
,
<? extends Map<String, Integer>[]>
etc.public final boolean isPrimitive()
void
).@NonNull public final GenericType<T> wrap()
this
itself. Idempotent.@NonNull public final GenericType<T> unwrap()
this
itself. Idempotent.@NonNull public final <X> GenericType<T> where(@NonNull GenericTypeParameter<X> freeVariable, @NonNull GenericType<X> actualType)
this class's
javadoc
for an example.@NonNull public final <X> GenericType<T> where(@NonNull GenericTypeParameter<X> freeVariable, @NonNull Class<X> actualType)
this class's
javadoc
for an example.@Nullable public final GenericType<?> getComponentType()
int[]
, T[]
,
<? extends Map<String, Integer>[]>
etc.), or else null
is returned.@NonNull public Class<? super T> getRawType()
T
. Formally speaking, if T
is returned by Method.getGenericReturnType()
, the raw type is what's returned by Method.getReturnType()
of the same method object. Specifically:
T
is a Class
itself, T
itself is returned.
T
is a parameterized type, the raw type of the parameterized type is returned.
T
is an array type , the returned type is the corresponding array class. For
example: List<Integer>[] => List[]
.
T
is a type variable or a wildcard type, the raw type of the first upper bound
is returned. For example: <X extends Foo> => Foo
.
@NonNull public final GenericType<? super T> getSupertype(@NonNull Class<? super T> superclass)
superclass
. For example, if this is ArrayList<String>
, Iterable<String>
is returned given the input Iterable.class
.@NonNull public final GenericType<? extends T> getSubtype(@NonNull Class<?> subclass)
this
with subclass
as the raw class. For example, if this is
Iterable<String>
and subclass
is List
, List<String>
is
returned.@NonNull public final Type getType()
@NonNull public com.datastax.oss.driver.shaded.guava.common.reflect.TypeToken<T> __getToken()
It leaks a shaded type. This should be part of the internal API, but due to internal implementation details it has to be exposed here.
Copyright © 2017–2024. All rights reserved.