kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 66b1492994327e28d0ab66a4fe6f4916dd54cde245407ec877636e3003371eec |
|---|---|
| Author | olim7t <omichallat+github@gmail.com> · 2015-12-09 19:46 +01:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2015-12-09 20:10 +01:00 |
| Parents | d457d3dbaa2b |
10 files changed
modified driver-core/src/main/java/com/datastax/driver/core/TypeTokens.java +22 −24
| @@ -39,9 +39,9 @@ | ||
| 39 | 39 | * are of the given type. |
| 40 | 40 | */ |
| 41 | 41 | public static <T> TypeToken<List<T>> listOf(Class<T> eltType) { |
| 42 | - return new TypeToken<List<T>>() { | |
| 43 | - }.where(new TypeParameter<T>() { | |
| 44 | - }, eltType); | |
| 42 | + // @formatter:off | |
| 43 | + return new TypeToken<List<T>>(){}.where(new TypeParameter<T>(){}, eltType); | |
| 44 | + // @formatter:on | |
| 45 | 45 | } |
| 46 | 46 | |
| 47 | 47 | /** |
| @@ -54,9 +54,9 @@ | ||
| 54 | 54 | * are of the given type. |
| 55 | 55 | */ |
| 56 | 56 | public static <T> TypeToken<List<T>> listOf(TypeToken<T> eltType) { |
| 57 | - return new TypeToken<List<T>>() { | |
| 58 | - }.where(new TypeParameter<T>() { | |
| 59 | - }, eltType); | |
| 57 | + // @formatter:off | |
| 58 | + return new TypeToken<List<T>>(){}.where(new TypeParameter<T>(){}, eltType); | |
| 59 | + // @formatter:on | |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | 62 | /** |
| @@ -69,9 +69,9 @@ | ||
| 69 | 69 | * are of the given type. |
| 70 | 70 | */ |
| 71 | 71 | public static <T> TypeToken<Set<T>> setOf(Class<T> eltType) { |
| 72 | - return new TypeToken<Set<T>>() { | |
| 73 | - }.where(new TypeParameter<T>() { | |
| 74 | - }, eltType); | |
| 72 | + // @formatter:off | |
| 73 | + return new TypeToken<Set<T>>(){}.where(new TypeParameter<T>(){}, eltType); | |
| 74 | + // @formatter:on | |
| 75 | 75 | } |
| 76 | 76 | |
| 77 | 77 | /** |
| @@ -84,9 +84,9 @@ | ||
| 84 | 84 | * are of the given type. |
| 85 | 85 | */ |
| 86 | 86 | public static <T> TypeToken<Set<T>> setOf(TypeToken<T> eltType) { |
| 87 | - return new TypeToken<Set<T>>() { | |
| 88 | - }.where(new TypeParameter<T>() { | |
| 89 | - }, eltType); | |
| 87 | + // @formatter:off | |
| 88 | + return new TypeToken<Set<T>>(){}.where(new TypeParameter<T>(){}, eltType); | |
| 89 | + // @formatter:on | |
| 90 | 90 | } |
| 91 | 91 | |
| 92 | 92 | /** |
| @@ -101,12 +101,11 @@ | ||
| 101 | 101 | * and values are of the given key and value types |
| 102 | 102 | */ |
| 103 | 103 | public static <K, V> TypeToken<Map<K, V>> mapOf(Class<K> keyType, Class<V> valueType) { |
| 104 | - return new TypeToken<Map<K, V>>() { | |
| 105 | - } | |
| 106 | - .where(new TypeParameter<K>() { | |
| 107 | - }, keyType) | |
| 108 | - .where(new TypeParameter<V>() { | |
| 109 | - }, valueType); | |
| 104 | + // @formatter:off | |
| 105 | + return new TypeToken<Map<K, V>>(){} | |
| 106 | + .where(new TypeParameter<K>(){}, keyType) | |
| 107 | + .where(new TypeParameter<V>(){}, valueType); | |
| 108 | + // @formatter:on | |
| 110 | 109 | } |
| 111 | 110 | |
| 112 | 111 | /** |
| @@ -121,11 +120,10 @@ | ||
| 121 | 120 | * and values are of the given key and value types |
| 122 | 121 | */ |
| 123 | 122 | public static <K, V> TypeToken<Map<K, V>> mapOf(TypeToken<K> keyType, TypeToken<V> valueType) { |
| 124 | - return new TypeToken<Map<K, V>>() { | |
| 125 | - } | |
| 126 | - .where(new TypeParameter<K>() { | |
| 127 | - }, keyType) | |
| 128 | - .where(new TypeParameter<V>() { | |
| 129 | - }, valueType); | |
| 123 | + // @formatter:off | |
| 124 | + return new TypeToken<Map<K, V>>(){} | |
| 125 | + .where(new TypeParameter<K>(){}, keyType) | |
| 126 | + .where(new TypeParameter<V>(){}, valueType); | |
| 127 | + // @formatter:on | |
| 130 | 128 | } |
| 131 | 129 | } |
modified driver-core/src/test/java/com/datastax/driver/core/CodecRegistryTest.java +1 −2
| @@ -27,8 +27,7 @@ | ||
| 27 | 27 | import static org.mockito.Mockito.when; |
| 28 | 28 | |
| 29 | 29 | public class CodecRegistryTest { |
| 30 | - TypeToken<List<Integer>> LIST_OF_INT_TOKEN = new TypeToken<List<Integer>>() { | |
| 31 | - }; | |
| 30 | + TypeToken<List<Integer>> LIST_OF_INT_TOKEN = TypeTokens.listOf(Integer.class); | |
| 32 | 31 | |
| 33 | 32 | @Test(groups = "unit") |
| 34 | 33 | public void should_ignore_codec_colliding_with_already_registered_codec() { |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecEncapsulationIntegrationTest.java +36 −49
| @@ -35,6 +35,15 @@ | ||
| 35 | 35 | |
| 36 | 36 | public class TypeCodecEncapsulationIntegrationTest extends CCMBridge.PerClassSingleNodeCluster { |
| 37 | 37 | |
| 38 | + // @formatter:off | |
| 39 | + private static final TypeToken<NumberBox<Integer>> NUMBERBOX_OF_INTEGER_TOKEN = new TypeToken<NumberBox<Integer>>() {}; | |
| 40 | + private static final TypeToken<NumberBox<Long>> NUMBERBOX_OF_LONG_TOKEN = new TypeToken<NumberBox<Long>>() {}; | |
| 41 | + private static final TypeToken<NumberBox<Float>> NUMBERBOX_OF_FLOAT_TOKEN = new TypeToken<NumberBox<Float>>() {}; | |
| 42 | + private static final TypeToken<NumberBox<Double>> NUMBERBOX_OF_DOUBLE_TOKEN = new TypeToken<NumberBox<Double>>() {}; | |
| 43 | + private static final TypeToken<NumberBox<BigInteger>> NUMBERBOX_OF_BIGINTEGER_TOKEN = new TypeToken<NumberBox<BigInteger>>() {}; | |
| 44 | + private static final TypeToken<NumberBox<BigDecimal>> NUMBERBOX_OF_BIGDECIMAL_TOKEN = new TypeToken<NumberBox<BigDecimal>>() {}; | |
| 45 | + // @formatter:on | |
| 46 | + | |
| 38 | 47 | private final String insertQuery = "INSERT INTO \"myTable\" (c_int, c_bigint, c_float, c_double, c_varint, c_decimal) VALUES (?, ?, ?, ?, ?, ?)"; |
| 39 | 48 | |
| 40 | 49 | private final String selectQuery = "SELECT c_int, c_bigint, c_float, c_double, c_varint, c_decimal FROM \"myTable\" WHERE c_int = ? and c_bigint = ?"; |
| @@ -127,25 +136,17 @@ | ||
| 127 | 136 | @Test(groups = "short") |
| 128 | 137 | public void should_use_custom_codecs_with_prepared_statements_2() { |
| 129 | 138 | session.execute(session.prepare(insertQuery).bind() |
| 130 | - .set(0, new NumberBox<Integer>(n_int), new TypeToken<NumberBox<Integer>>() { | |
| 131 | - }) | |
| 132 | - .set(1, new NumberBox<Long>(n_bigint), new TypeToken<NumberBox<Long>>() { | |
| 133 | - }) | |
| 134 | - .set(2, new NumberBox<Float>(n_float), new TypeToken<NumberBox<Float>>() { | |
| 135 | - }) | |
| 136 | - .set(3, new NumberBox<Double>(n_double), new TypeToken<NumberBox<Double>>() { | |
| 137 | - }) | |
| 138 | - .set(4, new NumberBox<BigInteger>(n_varint), new TypeToken<NumberBox<BigInteger>>() { | |
| 139 | - }) | |
| 140 | - .set(5, new NumberBox<BigDecimal>(n_decimal), new TypeToken<NumberBox<BigDecimal>>() { | |
| 141 | - }) | |
| 139 | + .set(0, new NumberBox<Integer>(n_int), NUMBERBOX_OF_INTEGER_TOKEN) | |
| 140 | + .set(1, new NumberBox<Long>(n_bigint), NUMBERBOX_OF_LONG_TOKEN) | |
| 141 | + .set(2, new NumberBox<Float>(n_float), NUMBERBOX_OF_FLOAT_TOKEN) | |
| 142 | + .set(3, new NumberBox<Double>(n_double), NUMBERBOX_OF_DOUBLE_TOKEN) | |
| 143 | + .set(4, new NumberBox<BigInteger>(n_varint), NUMBERBOX_OF_BIGINTEGER_TOKEN) | |
| 144 | + .set(5, new NumberBox<BigDecimal>(n_decimal), NUMBERBOX_OF_BIGDECIMAL_TOKEN) | |
| 142 | 145 | ); |
| 143 | 146 | PreparedStatement ps = session.prepare(selectQuery); |
| 144 | 147 | ResultSet rows = session.execute(ps.bind() |
| 145 | - .set(0, new NumberBox<Integer>(n_int), new TypeToken<NumberBox<Integer>>() { | |
| 146 | - }) | |
| 147 | - .set(1, new NumberBox<Long>(n_bigint), new TypeToken<NumberBox<Long>>() { | |
| 148 | - }) | |
| 148 | + .set(0, new NumberBox<Integer>(n_int), NUMBERBOX_OF_INTEGER_TOKEN) | |
| 149 | + .set(1, new NumberBox<Long>(n_bigint), NUMBERBOX_OF_LONG_TOKEN) | |
| 149 | 150 | ); |
| 150 | 151 | Row row = rows.one(); |
| 151 | 152 | assertRow(row); |
| @@ -169,26 +170,17 @@ | ||
| 169 | 170 | @Test(groups = "short") |
| 170 | 171 | public void should_use_custom_codecs_with_built_statements_2() { |
| 171 | 172 | session.execute(session.prepare(insertStmt).bind() |
| 172 | - .set(0, new NumberBox<Integer>(n_int), new TypeToken<NumberBox<Integer>>() { | |
| 173 | - }) | |
| 174 | - .set(1, new NumberBox<Long>(n_bigint), new TypeToken<NumberBox<Long>>() { | |
| 175 | - }) | |
| 176 | - .set(2, new NumberBox<Float>(n_float), new TypeToken<NumberBox<Float>>() { | |
| 177 | - }) | |
| 178 | - .set(3, new NumberBox<Double>(n_double), new TypeToken<NumberBox<Double>>() { | |
| 179 | - }) | |
| 180 | - .set(4, new NumberBox<BigInteger>(n_varint), new TypeToken<NumberBox<BigInteger>>() { | |
| 181 | - }) | |
| 182 | - .set(5, new NumberBox<BigDecimal>(n_decimal), new TypeToken<NumberBox<BigDecimal>>() { | |
| 183 | - }) | |
| 184 | - ); | |
| 173 | + .set(0, new NumberBox<Integer>(n_int), NUMBERBOX_OF_INTEGER_TOKEN) | |
| 174 | + .set(1, new NumberBox<Long>(n_bigint), NUMBERBOX_OF_LONG_TOKEN) | |
| 175 | + .set(2, new NumberBox<Float>(n_float), NUMBERBOX_OF_FLOAT_TOKEN) | |
| 176 | + .set(3, new NumberBox<Double>(n_double), NUMBERBOX_OF_DOUBLE_TOKEN) | |
| 177 | + .set(4, new NumberBox<BigInteger>(n_varint), NUMBERBOX_OF_BIGINTEGER_TOKEN) | |
| 178 | + .set(5, new NumberBox<BigDecimal>(n_decimal), NUMBERBOX_OF_BIGDECIMAL_TOKEN)); | |
| 185 | 179 | PreparedStatement ps = session.prepare(selectStmt); |
| 186 | 180 | ResultSet rows = session.execute(ps.bind() |
| 187 | - .set(0, new NumberBox<Integer>(n_int), new TypeToken<NumberBox<Integer>>() { | |
| 188 | - }) | |
| 189 | - .set(1, new NumberBox<Long>(n_bigint), new TypeToken<NumberBox<Long>>() { | |
| 190 | - }) | |
| 191 | - ); | |
| 181 | + .set(0, new NumberBox<Integer>(n_int), NUMBERBOX_OF_INTEGER_TOKEN) | |
| 182 | + .set(1, new NumberBox<Long>(n_bigint), NUMBERBOX_OF_LONG_TOKEN)); | |
| 183 | + | |
| 192 | 184 | Row row = rows.one(); |
| 193 | 185 | assertRow(row); |
| 194 | 186 | } |
| @@ -219,18 +211,12 @@ | ||
| 219 | 211 | assertThat(row.get(5, BigDecimal.class)).isEqualTo(n_decimal); |
| 220 | 212 | // with get + type, but enforcing NumberBox types |
| 221 | 213 | // we get the NumberBox codecs instead |
| 222 | - assertThat(row.get(0, new TypeToken<NumberBox<Integer>>() { | |
| 223 | - })).isEqualTo(new NumberBox<Integer>(n_int)); | |
| 224 | - assertThat(row.get(1, new TypeToken<NumberBox<Long>>() { | |
| 225 | - })).isEqualTo(new NumberBox<Long>(n_bigint)); | |
| 226 | - assertThat(row.get(2, new TypeToken<NumberBox<Float>>() { | |
| 227 | - })).isEqualTo(new NumberBox<Float>(n_float)); | |
| 228 | - assertThat(row.get(3, new TypeToken<NumberBox<Double>>() { | |
| 229 | - })).isEqualTo(new NumberBox<Double>(n_double)); | |
| 230 | - assertThat(row.get(4, new TypeToken<NumberBox<BigInteger>>() { | |
| 231 | - })).isEqualTo(new NumberBox<BigInteger>(n_varint)); | |
| 232 | - assertThat(row.get(5, new TypeToken<NumberBox<BigDecimal>>() { | |
| 233 | - })).isEqualTo(new NumberBox<BigDecimal>(n_decimal)); | |
| 214 | + assertThat(row.get(0, NUMBERBOX_OF_INTEGER_TOKEN)).isEqualTo(new NumberBox<Integer>(n_int)); | |
| 215 | + assertThat(row.get(1, NUMBERBOX_OF_LONG_TOKEN)).isEqualTo(new NumberBox<Long>(n_bigint)); | |
| 216 | + assertThat(row.get(2, NUMBERBOX_OF_FLOAT_TOKEN)).isEqualTo(new NumberBox<Float>(n_float)); | |
| 217 | + assertThat(row.get(3, NUMBERBOX_OF_DOUBLE_TOKEN)).isEqualTo(new NumberBox<Double>(n_double)); | |
| 218 | + assertThat(row.get(4, NUMBERBOX_OF_BIGINTEGER_TOKEN)).isEqualTo(new NumberBox<BigInteger>(n_varint)); | |
| 219 | + assertThat(row.get(5, NUMBERBOX_OF_BIGDECIMAL_TOKEN)).isEqualTo(new NumberBox<BigDecimal>(n_decimal)); | |
| 234 | 220 | } |
| 235 | 221 | |
| 236 | 222 | private class NumberBoxCodec<T extends Number> extends TypeCodec<NumberBox<T>> { |
| @@ -238,9 +224,10 @@ | ||
| 238 | 224 | private final TypeCodec<T> numberCodec; |
| 239 | 225 | |
| 240 | 226 | protected NumberBoxCodec(TypeCodec<T> numberCodec) { |
| 241 | - super(numberCodec.getCqlType(), new TypeToken<NumberBox<T>>() { | |
| 242 | - }.where(new TypeParameter<T>() { | |
| 243 | - }, numberCodec.getJavaType())); | |
| 227 | + // @formatter:off | |
| 228 | + super(numberCodec.getCqlType(), | |
| 229 | + new TypeToken<NumberBox<T>>() {}.where(new TypeParameter<T>() {}, numberCodec.getJavaType())); | |
| 230 | + // @formatter:on | |
| 244 | 231 | this.numberCodec = numberCodec; |
| 245 | 232 | } |
| 246 | 233 |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecNestedCollectionsIntegrationTest.java +4 −4
| @@ -49,10 +49,10 @@ | ||
| 49 | 49 | private int pk = 42; |
| 50 | 50 | private List<Set<Map<MyInt, String>>> v; |
| 51 | 51 | |
| 52 | - private TypeToken<List<Set<Map<MyInt, String>>>> listType = new TypeToken<List<Set<Map<MyInt, String>>>>() { | |
| 53 | - }; | |
| 54 | - private TypeToken<Set<Map<MyInt, String>>> elementsType = new TypeToken<Set<Map<MyInt, String>>>() { | |
| 55 | - }; | |
| 52 | + // @formatter:off | |
| 53 | + private TypeToken<List<Set<Map<MyInt, String>>>> listType = new TypeToken<List<Set<Map<MyInt, String>>>>() {}; | |
| 54 | + private TypeToken<Set<Map<MyInt, String>>> elementsType = new TypeToken<Set<Map<MyInt, String>>>() {}; | |
| 55 | + // @formatter:on | |
| 56 | 56 | |
| 57 | 57 | @Override |
| 58 | 58 | protected Collection<String> getTableDefinitions() { |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecOverlappingJavaTypeIntegrationTest.java +2 −6
| @@ -16,12 +16,10 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 19 | -import com.google.common.reflect.TypeToken; | |
| 20 | 19 | import org.testng.annotations.Test; |
| 21 | 20 | |
| 22 | 21 | import java.nio.ByteBuffer; |
| 23 | 22 | import java.util.Collection; |
| 24 | -import java.util.List; | |
| 25 | 23 | |
| 26 | 24 | import static com.google.common.collect.Lists.newArrayList; |
| 27 | 25 | import static org.assertj.core.api.Assertions.assertThat; |
| @@ -84,10 +82,8 @@ | ||
| 84 | 82 | assertThat(row.getList(1, Integer.class)).isEqualTo(newArrayList(42)); |
| 85 | 83 | assertThat(row.getList(1, String.class)).isEqualTo(newArrayList("42")); |
| 86 | 84 | assertThat(row.getObject(1)).isEqualTo(newArrayList(42)); // uses the default codec |
| 87 | - assertThat(row.get(1, new TypeToken<List<Integer>>() { | |
| 88 | - })).isEqualTo(newArrayList(42)); | |
| 89 | - assertThat(row.get(1, new TypeToken<List<String>>() { | |
| 90 | - })).isEqualTo(newArrayList("42")); | |
| 85 | + assertThat(row.get(1, TypeTokens.listOf(Integer.class))).isEqualTo(newArrayList(42)); | |
| 86 | + assertThat(row.get(1, TypeTokens.listOf(String.class))).isEqualTo(newArrayList("42")); | |
| 91 | 87 | } |
| 92 | 88 | |
| 93 | 89 | private class IntToStringCodec extends TypeCodec<String> { |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecTest.java +9 −8
| @@ -38,6 +38,11 @@ | ||
| 38 | 38 | |
| 39 | 39 | public static final DataType CUSTOM_FOO = DataType.custom("com.example.FooBar"); |
| 40 | 40 | |
| 41 | + // @formatter:off | |
| 42 | + public static final TypeToken<List<A>> LIST_OF_A_TOKEN = new TypeToken<List<A>>() {}; | |
| 43 | + public static final TypeToken<List<B>> LIST_OF_B_TOKEN = new TypeToken<List<B>>() {}; | |
| 44 | + // @formatter:on | |
| 45 | + | |
| 41 | 46 | private CodecRegistry codecRegistry = new CodecRegistry(); |
| 42 | 47 | |
| 43 | 48 | @Test(groups = "unit") |
| @@ -167,14 +172,12 @@ | ||
| 167 | 172 | //ok |
| 168 | 173 | } |
| 169 | 174 | TypeCodec<List<A>> expected = TypeCodec.list(aCodec); |
| 170 | - TypeCodec<List<A>> actual = codecRegistry.codecFor(list(cint()), new TypeToken<List<A>>() { | |
| 171 | - }); | |
| 175 | + TypeCodec<List<A>> actual = codecRegistry.codecFor(list(cint()), LIST_OF_A_TOKEN); | |
| 172 | 176 | assertThat(actual.getCqlType()).isEqualTo(expected.getCqlType()); |
| 173 | 177 | assertThat(actual.getJavaType()).isEqualTo(expected.getJavaType()); |
| 174 | 178 | // cannot work: List<B> is not assignable to List<A> |
| 175 | 179 | try { |
| 176 | - codecRegistry.codecFor(list(cint()), new TypeToken<List<B>>() { | |
| 177 | - }); | |
| 180 | + codecRegistry.codecFor(list(cint()), LIST_OF_B_TOKEN); | |
| 178 | 181 | fail(); |
| 179 | 182 | } catch (CodecNotFoundException e) { |
| 180 | 183 | //ok |
| @@ -190,15 +193,13 @@ | ||
| 190 | 193 | } |
| 191 | 194 | assertThat(codecRegistry.codecFor(cint(), B.class)).isNotNull().isSameAs(bCodec); |
| 192 | 195 | try { |
| 193 | - codecRegistry.codecFor(list(cint()), new TypeToken<List<A>>() { | |
| 194 | - }); | |
| 196 | + codecRegistry.codecFor(list(cint()), LIST_OF_A_TOKEN); | |
| 195 | 197 | fail(); |
| 196 | 198 | } catch (CodecNotFoundException e) { |
| 197 | 199 | // ok |
| 198 | 200 | } |
| 199 | 201 | TypeCodec<List<B>> expectedB = TypeCodec.list(bCodec); |
| 200 | - TypeCodec<List<B>> actualB = codecRegistry.codecFor(list(cint()), new TypeToken<List<B>>() { | |
| 201 | - }); | |
| 202 | + TypeCodec<List<B>> actualB = codecRegistry.codecFor(list(cint()), LIST_OF_B_TOKEN); | |
| 202 | 203 | assertThat(actualB.getCqlType()).isEqualTo(expectedB.getCqlType()); |
| 203 | 204 | assertThat(actualB.getJavaType()).isEqualTo(expectedB.getJavaType()); |
| 204 | 205 | } |
modified driver-extras/src/main/java/com/datastax/driver/extras/codecs/guava/OptionalCodec.java +3 −3
| @@ -46,9 +46,9 @@ | ||
| 46 | 46 | } |
| 47 | 47 | |
| 48 | 48 | public OptionalCodec(TypeCodec<T> codec, Predicate<T> isAbsent) { |
| 49 | - super(codec, new TypeToken<Optional<T>>() { | |
| 50 | - }.where(new TypeParameter<T>() { | |
| 51 | - }, codec.getJavaType())); | |
| 49 | + // @formatter:off | |
| 50 | + super(codec, new TypeToken<Optional<T>>() {}.where(new TypeParameter<T>() {}, codec.getJavaType())); | |
| 51 | + // @formatter:on | |
| 52 | 52 | this.isAbsent = isAbsent; |
| 53 | 53 | } |
| 54 | 54 |
modified driver-extras/src/main/java/com/datastax/driver/extras/codecs/jdk8/OptionalCodec.java +3 −3
| @@ -47,9 +47,9 @@ | ||
| 47 | 47 | } |
| 48 | 48 | |
| 49 | 49 | public OptionalCodec(TypeCodec<T> codec, Predicate<T> isAbsent) { |
| 50 | - super(codec, new TypeToken<Optional<T>>() { | |
| 51 | - }.where(new TypeParameter<T>() { | |
| 52 | - }, codec.getJavaType())); | |
| 50 | + // @formatter:off | |
| 51 | + super(codec, new TypeToken<Optional<T>>() {}.where(new TypeParameter<T>() {}, codec.getJavaType())); | |
| 52 | + // @formatter:on | |
| 53 | 53 | this.isAbsent = isAbsent; |
| 54 | 54 | } |
| 55 | 55 |
modified driver-extras/src/test/java/com/datastax/driver/extras/codecs/enums/EnumCodecsTest.java +9 −19
| @@ -21,7 +21,6 @@ | ||
| 21 | 21 | import com.datastax.driver.mapping.MappingManager; |
| 22 | 22 | import com.datastax.driver.mapping.annotations.*; |
| 23 | 23 | import com.google.common.collect.ImmutableMap; |
| 24 | -import com.google.common.reflect.TypeToken; | |
| 25 | 24 | import org.testng.annotations.BeforeMethod; |
| 26 | 25 | import org.testng.annotations.Test; |
| 27 | 26 | |
| @@ -124,8 +123,7 @@ | ||
| 124 | 123 | .set(1, FOO_1, Foo.class) |
| 125 | 124 | .setList(2, foos, Foo.class) |
| 126 | 125 | .set(3, BAR_1, Bar.class) |
| 127 | - .set(4, bars, new TypeToken<Set<Bar>>() { | |
| 128 | - }) | |
| 126 | + .set(4, bars, TypeTokens.setOf(Bar.class)) | |
| 129 | 127 | .setMap(5, foobars, Foo.class, Bar.class) |
| 130 | 128 | .setTupleValue(6, tupleValue) |
| 131 | 129 | .setUDTValue(7, udtValue) |
| @@ -199,10 +197,8 @@ | ||
| 199 | 197 | assertThat(row.getObject(2)).isEqualTo(newArrayList(FOO_1.ordinal(), FOO_2.ordinal())); // uses the built-in ListCodec(IntCodec) because CQL type is list<int> |
| 200 | 198 | assertThat(row.getList(2, Integer.class)).isEqualTo(newArrayList(FOO_1.ordinal(), FOO_2.ordinal())); |
| 201 | 199 | assertThat(row.getList("foos", Foo.class)).isEqualTo(newArrayList(FOO_1, FOO_2)); |
| 202 | - assertThat(row.get(2, new TypeToken<List<Integer>>() { | |
| 203 | - })).isEqualTo(newArrayList(FOO_1.ordinal(), FOO_2.ordinal())); | |
| 204 | - assertThat(row.get("foos", new TypeToken<List<Foo>>() { | |
| 205 | - })).isEqualTo(newArrayList(FOO_1, FOO_2)); | |
| 200 | + assertThat(row.get(2, TypeTokens.listOf(Integer.class))).isEqualTo(newArrayList(FOO_1.ordinal(), FOO_2.ordinal())); | |
| 201 | + assertThat(row.get("foos", TypeTokens.listOf(Foo.class))).isEqualTo(newArrayList(FOO_1, FOO_2)); | |
| 206 | 202 | |
| 207 | 203 | assertThat(row.getObject(3)).isEqualTo(BAR_1.name()); // uses the built-in VarcharCodec because CQL type is varchar |
| 208 | 204 | assertThat(row.getString("bar")).isEqualTo(BAR_1.name()); // forces VarcharCodec |
| @@ -212,10 +208,8 @@ | ||
| 212 | 208 | assertThat(row.getObject(4)).isEqualTo(newHashSet(BAR_1.name(), BAR_2.name())); // uses the built-in SetCodec(VarcharCodec) because CQL type is set<varchar> |
| 213 | 209 | assertThat(row.getSet(4, String.class)).isEqualTo(newHashSet(BAR_1.name(), BAR_2.name())); |
| 214 | 210 | assertThat(row.getSet("bars", Bar.class)).isEqualTo(newHashSet(BAR_1, BAR_2)); |
| 215 | - assertThat(row.get(4, new TypeToken<Set<String>>() { | |
| 216 | - })).isEqualTo(newHashSet(BAR_1.name(), BAR_2.name())); | |
| 217 | - assertThat(row.get("bars", new TypeToken<Set<Bar>>() { | |
| 218 | - })).isEqualTo(newHashSet(BAR_1, BAR_2)); | |
| 211 | + assertThat(row.get(4, TypeTokens.setOf(String.class))).isEqualTo(newHashSet(BAR_1.name(), BAR_2.name())); | |
| 212 | + assertThat(row.get("bars", TypeTokens.setOf(Bar.class))).isEqualTo(newHashSet(BAR_1, BAR_2)); | |
| 219 | 213 | |
| 220 | 214 | // uses default built-in codec MapCodec(IntCodec, VarcharCodec) because CQL type is map<int, varchar> |
| 221 | 215 | assertThat(row.getObject(5)).isEqualTo(ImmutableMap.of(FOO_1.ordinal(), BAR_1.name(), FOO_2.ordinal(), BAR_2.name())); |
| @@ -227,14 +221,10 @@ | ||
| 227 | 221 | assertThat(row.getMap(5, Foo.class, Bar.class)).isEqualTo(ImmutableMap.of(FOO_1, BAR_1, FOO_2, BAR_2)); |
| 228 | 222 | |
| 229 | 223 | // get + TypeToken with combinations of built-in or EnumX codecs |
| 230 | - assertThat(row.get("foobars", new TypeToken<Map<Integer, String>>() { | |
| 231 | - })).isEqualTo(ImmutableMap.of(FOO_1.ordinal(), BAR_1.name(), FOO_2.ordinal(), BAR_2.name())); | |
| 232 | - assertThat(row.get("foobars", new TypeToken<Map<Foo, String>>() { | |
| 233 | - })).isEqualTo(ImmutableMap.of(FOO_1, BAR_1.name(), FOO_2, BAR_2.name())); | |
| 234 | - assertThat(row.get("foobars", new TypeToken<Map<Integer, Bar>>() { | |
| 235 | - })).isEqualTo(ImmutableMap.of(FOO_1.ordinal(), BAR_1, FOO_2.ordinal(), BAR_2)); | |
| 236 | - assertThat(row.get("foobars", new TypeToken<Map<Foo, Bar>>() { | |
| 237 | - })).isEqualTo(ImmutableMap.of(FOO_1, BAR_1, FOO_2, BAR_2)); | |
| 224 | + assertThat(row.get("foobars", TypeTokens.mapOf(Integer.class, String.class))).isEqualTo(ImmutableMap.of(FOO_1.ordinal(), BAR_1.name(), FOO_2.ordinal(), BAR_2.name())); | |
| 225 | + assertThat(row.get("foobars", TypeTokens.mapOf(Foo.class, String.class))).isEqualTo(ImmutableMap.of(FOO_1, BAR_1.name(), FOO_2, BAR_2.name())); | |
| 226 | + assertThat(row.get("foobars", TypeTokens.mapOf(Integer.class, Bar.class))).isEqualTo(ImmutableMap.of(FOO_1.ordinal(), BAR_1, FOO_2.ordinal(), BAR_2)); | |
| 227 | + assertThat(row.get("foobars", TypeTokens.mapOf(Foo.class, Bar.class))).isEqualTo(ImmutableMap.of(FOO_1, BAR_1, FOO_2, BAR_2)); | |
| 238 | 228 | |
| 239 | 229 | assertThat(row.getTupleValue(6).getInt(0)).isEqualTo(FOO_1.ordinal()); |
| 240 | 230 | assertThat(row.get(6, TupleValue.class).get(0, Foo.class)).isEqualTo(FOO_1); |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperCustomCodecTest.java +4 −3
| @@ -567,9 +567,10 @@ | ||
| 567 | 567 | private final Predicate<T> isAbsent; |
| 568 | 568 | |
| 569 | 569 | public OptionalCodec(TypeCodec<T> codec) { |
| 570 | - super(codec, new TypeToken<Optional<T>>() { | |
| 571 | - }.where(new TypeParameter<T>() { | |
| 572 | - }, codec.getJavaType())); | |
| 570 | + // @formatter:off | |
| 571 | + super(codec, | |
| 572 | + new TypeToken<Optional<T>>() {}.where(new TypeParameter<T>() {}, codec.getJavaType())); | |
| 573 | + // @formatter:on | |
| 573 | 574 | this.isAbsent = new Predicate<T>() { |
| 574 | 575 | @Override |
| 575 | 576 | public boolean apply(T input) { |