All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.datastax.oss.driver.api.core.type.reflect.GenericType Maven / Gradle / Ivy

The newest version!
/*
 * 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> mapOf( @NonNull Class keyType, @NonNull Class valueType) { TypeToken> token = new TypeToken>() {}.where(new TypeParameter() {}, TypeToken.of(keyType)) .where(new TypeParameter() {}, TypeToken.of(valueType)); return new GenericType<>(token); } @NonNull public static GenericType> mapOf( @NonNull GenericType keyType, @NonNull GenericType valueType) { TypeToken> token = new TypeToken>() {}.where(new TypeParameter() {}, keyType.token) .where(new TypeParameter() {}, valueType.token); return new GenericType<>(token); } @NonNull public static GenericType arrayOf(@NonNull Class componentType) { TypeToken token = new TypeToken() {}.where(new TypeParameter() {}, TypeToken.of(componentType)); return new GenericType<>(token); } @NonNull public static GenericType arrayOf(@NonNull GenericType componentType) { TypeToken token = new TypeToken() {}.where(new TypeParameter() {}, componentType.token); return new GenericType<>(token); } @NonNull public static GenericType> optionalOf(@NonNull Class componentType) { TypeToken> token = new TypeToken>() {}.where( new TypeParameter() {}, TypeToken.of(componentType)); return new GenericType<>(token); } @NonNull public static GenericType> optionalOf(@NonNull GenericType componentType) { TypeToken> token = new TypeToken>() {}.where(new TypeParameter() {}, componentType.token); return new GenericType<>(token); } private final TypeToken token; private GenericType(TypeToken token) { this.token = token; } protected GenericType() { this.token = new TypeToken(getClass()) {}; } /** * Returns true if this type is a supertype of the given {@code type}. "Supertype" is defined * according to the rules for type * arguments introduced with Java generics. */ public final boolean isSupertypeOf(@NonNull GenericType type) { return token.isSupertypeOf(type.token); } /** * Returns true if this type is a subtype of the given {@code type}. "Subtype" is defined * according to the rules for type * arguments introduced with Java generics. */ public final boolean isSubtypeOf(@NonNull GenericType type) { return token.isSubtypeOf(type.token); } /** * Returns true if this type is known to be an array type, such as {@code int[]}, {@code T[]}, * {@code []>} etc. */ public final boolean isArray() { return token.isArray(); } /** Returns true if this type is one of the nine primitive types (including {@code void}). */ public final boolean isPrimitive() { return token.isPrimitive(); } /** * Returns the corresponding wrapper type if this is a primitive type; otherwise returns {@code * this} itself. Idempotent. */ @NonNull public final GenericType wrap() { if (isPrimitive()) { return new GenericType<>(token.wrap()); } return this; } /** * Returns the corresponding primitive type if this is a wrapper type; otherwise returns {@code * this} itself. Idempotent. */ @NonNull public final GenericType unwrap() { if (Primitives.allWrapperTypes().contains(token.getRawType())) { return new GenericType<>(token.unwrap()); } return this; } /** * Substitutes a free type variable with an actual type. See {@link GenericType this class's * javadoc} for an example. */ @NonNull public final GenericType where( @NonNull GenericTypeParameter freeVariable, @NonNull GenericType actualType) { TypeResolver resolver = new TypeResolver().where(freeVariable.getTypeVariable(), actualType.__getToken().getType()); Type resolvedType = resolver.resolveType(this.token.getType()); @SuppressWarnings("unchecked") TypeToken resolvedToken = (TypeToken) TypeToken.of(resolvedType); return new GenericType<>(resolvedToken); } /** * Substitutes a free type variable with an actual type. See {@link GenericType this class's * javadoc} for an example. */ @NonNull public final GenericType where( @NonNull GenericTypeParameter freeVariable, @NonNull Class actualType) { return where(freeVariable, GenericType.of(actualType)); } /** * Returns the array component type if this type represents an array ({@code int[]}, {@code T[]}, * {@code []>} etc.), or else {@code null} is returned. */ @Nullable @SuppressWarnings("unchecked") public final GenericType getComponentType() { TypeToken componentTypeToken = token.getComponentType(); return (componentTypeToken == null) ? null : new GenericType(componentTypeToken); } /** * Returns the raw type of {@code T}. Formally speaking, if {@code T} is returned by {@link * java.lang.reflect.Method#getGenericReturnType}, the raw type is what's returned by {@link * java.lang.reflect.Method#getReturnType} of the same method object. Specifically: * *

    *
  • If {@code T} is a {@code Class} itself, {@code T} itself is returned. *
  • If {@code T} is a parameterized type, the raw type of the parameterized type is returned. *
  • If {@code T} is an array type , the returned type is the corresponding array class. For * example: {@code List[] => List[]}. *
  • If {@code T} is a type variable or a wildcard type, the raw type of the first upper bound * is returned. For example: {@code => Foo}. *
*/ @NonNull public Class getRawType() { return token.getRawType(); } /** * Returns the generic form of {@code superclass}. For example, if this is {@code * ArrayList}, {@code Iterable} is returned given the input {@code * Iterable.class}. */ @SuppressWarnings("unchecked") @NonNull public final GenericType getSupertype(@NonNull Class superclass) { return new GenericType(token.getSupertype(superclass)); } /** * Returns subtype of {@code this} with {@code subclass} as the raw class. For example, if this is * {@code Iterable} and {@code subclass} is {@code List}, {@code List} is * returned. */ @SuppressWarnings("unchecked") @NonNull public final GenericType getSubtype(@NonNull Class subclass) { return new GenericType(token.getSubtype(subclass)); } /** Returns the represented type. */ @NonNull public final Type getType() { return token.getType(); } /** * This method is for internal use, DO NOT use it from client code. * *

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. * * @leaks-private-api */ @NonNull public TypeToken __getToken() { return token; } @Override public boolean equals(Object other) { if (other == this) { return true; } else if (other instanceof GenericType) { GenericType that = (GenericType) other; return this.token.equals(that.token); } else { return false; } } @Override public int hashCode() { return token.hashCode(); } @Override public String toString() { return token.toString(); } private static class SimpleGenericType extends GenericType { SimpleGenericType(Class type) { super(TypeToken.of(type)); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy