|
| 1 | +/* |
| 2 | + * Copyright (C) 2017-2017 DataStax Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.datastax.oss.driver.api.types.reflect; |
| 17 | + |
| 18 | +import com.google.common.reflect.TypeToken; |
| 19 | + |
| 20 | +/** |
| 21 | + * Runtime representation of a generic Java type. |
| 22 | + * |
| 23 | + * <p>This is used by type codecs to indicate which Java types they accept, and by generic getters |
| 24 | + * and setters in the driver's query API. |
| 25 | + * |
| 26 | + * <p>To create an instance, use one of the static factory methods, or create an anonymous class: |
| 27 | + * |
| 28 | + * <pre>{@code |
| 29 | + * GenericType<Foo<Bar>> fooBarType = new GenericType<Foo<Bar>>(){}; |
| 30 | + * }</pre> |
| 31 | + * |
| 32 | + * You are encouraged to store and reuse these objects. |
| 33 | + */ |
| 34 | +public abstract class GenericType<T> { |
| 35 | + |
| 36 | + /** Creates a new instance representing a raw Java class. */ |
| 37 | + public static <T> GenericType<T> of(Class<T> type) { |
| 38 | + return new SimpleGenericType<>(type); |
| 39 | + } |
| 40 | + |
| 41 | + // This wraps -- and delegates most of the work to -- a Guava type token. The reason we don't |
| 42 | + // expose that type directly is because we shade Guava. |
| 43 | + private final TypeToken<T> token; |
| 44 | + |
| 45 | + private GenericType(TypeToken<T> token) { |
| 46 | + this.token = token; |
| 47 | + } |
| 48 | + |
| 49 | + protected GenericType() { |
| 50 | + this.token = new TypeToken<T>(getClass()) {}; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * This method is for internal use, <b>DO NOT use it from client code</b>. |
| 55 | + * |
| 56 | + * <p>It leaks a shaded type. This should be part of the internal API, but due to internal |
| 57 | + * implementation details it has to be exposed here. |
| 58 | + */ |
| 59 | + public TypeToken<T> __getToken() { |
| 60 | + return token; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public boolean equals(Object other) { |
| 65 | + if (other == this) { |
| 66 | + return true; |
| 67 | + } else if (other instanceof GenericType) { |
| 68 | + GenericType that = (GenericType) other; |
| 69 | + return this.token.equals(that.token); |
| 70 | + } else { |
| 71 | + return false; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public int hashCode() { |
| 77 | + return token.hashCode(); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String toString() { |
| 82 | + return token.toString(); |
| 83 | + } |
| 84 | + |
| 85 | + private static class SimpleGenericType<T> extends GenericType<T> { |
| 86 | + SimpleGenericType(Class<T> type) { |
| 87 | + super(TypeToken.of(type)); |
| 88 | + } |
| 89 | + } |
| 90 | +} |
0 commit comments