com.datastax.oss.driver.api.core.type.reflect.GenericType Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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.
*/
package com.datastax.oss.driver.api.core.type.reflect;
import com.datastax.oss.driver.api.core.data.CqlDuration;
import com.datastax.oss.driver.api.core.data.CqlVector;
import com.datastax.oss.driver.api.core.data.GettableByIndex;
import com.datastax.oss.driver.api.core.data.TupleValue;
import com.datastax.oss.driver.api.core.data.UdtValue;
import com.datastax.oss.driver.api.core.type.codec.TypeCodec;
import com.datastax.oss.driver.shaded.guava.common.primitives.Primitives;
import com.datastax.oss.driver.shaded.guava.common.reflect.TypeParameter;
import com.datastax.oss.driver.shaded.guava.common.reflect.TypeResolver;
import com.datastax.oss.driver.shaded.guava.common.reflect.TypeToken;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import java.lang.reflect.Type;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.time.Duration;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import net.jcip.annotations.Immutable;
/**
* Runtime representation of a generic Java type.
*
* This is used by type codecs to indicate which Java types they accept ({@link
* TypeCodec#accepts(GenericType)}), and by generic getters and setters (such as {@link
* 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:
*
*
{@code
* GenericType> stringListType = GenericType.listOf(String.class);
* }
*
* By using an anonymous class:
*
* {@code
* GenericType> fooBarType = new GenericType>(){};
* }
*
* In a generic method, by using {@link #where(GenericTypeParameter, GenericType)} to substitute
* free type variables with runtime types:
*
* {@code
* GenericType> optionalOf(GenericType elementType) {
* return new GenericType>() {}
* .where(new GenericTypeParameter() {}, elementType);
* }
* ...
* GenericType>> 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 {@code TypeToken}. The only reason why
* {@code TypeToken} is not used directly is because Guava is not exposed in the driver's public API
* (it's used internally, but shaded).
*/
@Immutable
public class GenericType {
public static final GenericType BOOLEAN = of(Boolean.class);
public static final GenericType BYTE = of(Byte.class);
public static final GenericType DOUBLE = of(Double.class);
public static final GenericType FLOAT = of(Float.class);
public static final GenericType INTEGER = of(Integer.class);
public static final GenericType LONG = of(Long.class);
public static final GenericType SHORT = of(Short.class);
public static final GenericType INSTANT = of(Instant.class);
public static final GenericType ZONED_DATE_TIME = of(ZonedDateTime.class);
public static final GenericType LOCAL_DATE = of(LocalDate.class);
public static final GenericType LOCAL_TIME = of(LocalTime.class);
public static final GenericType LOCAL_DATE_TIME = of(LocalDateTime.class);
public static final GenericType BYTE_BUFFER = of(ByteBuffer.class);
public static final GenericType STRING = of(String.class);
public static final GenericType BIG_INTEGER = of(BigInteger.class);
public static final GenericType BIG_DECIMAL = of(BigDecimal.class);
public static final GenericType UUID = of(UUID.class);
public static final GenericType INET_ADDRESS = of(InetAddress.class);
public static final GenericType CQL_DURATION = of(CqlDuration.class);
public static final GenericType TUPLE_VALUE = of(TupleValue.class);
public static final GenericType UDT_VALUE = of(UdtValue.class);
public static final GenericType DURATION = of(Duration.class);
@NonNull
public static GenericType of(@NonNull Class type) {
return new SimpleGenericType<>(type);
}
@NonNull
public static GenericType of(@NonNull java.lang.reflect.Type type) {
return new GenericType<>(TypeToken.of(type));
}
@NonNull
public static GenericType> listOf(@NonNull Class elementType) {
TypeToken> token =
new TypeToken>() {}.where(new TypeParameter() {}, TypeToken.of(elementType));
return new GenericType<>(token);
}
@NonNull
public static GenericType> listOf(@NonNull GenericType elementType) {
TypeToken> token =
new TypeToken>() {}.where(new TypeParameter() {}, elementType.token);
return new GenericType<>(token);
}
@NonNull
public static GenericType> setOf(@NonNull Class elementType) {
TypeToken> token =
new TypeToken>() {}.where(new TypeParameter() {}, TypeToken.of(elementType));
return new GenericType<>(token);
}
@NonNull
public static GenericType> setOf(@NonNull GenericType elementType) {
TypeToken> token =
new TypeToken>() {}.where(new TypeParameter() {}, elementType.token);
return new GenericType<>(token);
}
@NonNull
public static GenericType> vectorOf(
@NonNull Class elementType) {
TypeToken> token =
new TypeToken>() {}.where(
new TypeParameter() {}, TypeToken.of(elementType));
return new GenericType<>(token);
}
@NonNull
public static GenericType> vectorOf(
@NonNull GenericType elementType) {
TypeToken> token =
new TypeToken>() {}.where(new TypeParameter() {}, elementType.token);
return new GenericType<>(token);
}
@NonNull
public static GenericType