kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 7f3ccb3b2b46b9ddffd92dd350318dc9359104363bdac1b5011195dac6111684 |
|---|---|
| Author | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-10-14 22:38 +02:00 |
| Committer | Olivier Michallat <olim7t@users.noreply.github.com> · 2015-10-14 22:38 +02:00 |
| Parents | 16505233fda0, 2764f2e0c54f |
51 files changed
modified changelog/README.md +4 −0
| @@ -11,6 +11,10 @@ | ||
| 11 | 11 | - [new feature] Add BoundStatement#unset (JAVA-930) |
| 12 | 12 | - [bug] Make table metadata options class visible (JAVA-946) |
| 13 | 13 | - [bug] Add crcCheckChance to TableOptionsMetadata#equals/hashCode (JAVA-939) |
| 14 | +- [bug] Make TypeCodec return mutable collections (JAVA-922) | |
| 15 | +- [improvement] Limit visibility of codec internals (JAVA-932) | |
| 16 | +- [improvement] Warn if a custom codec collides with an existing one (JAVA-934) | |
| 17 | +- [improvement] Allow typed getters/setters to target any CQL type (JAVA-940) | |
| 14 | 18 | |
| 15 | 19 | |
| 16 | 20 | ### 3.0.0-alpha3 |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractAddressableByIndexData.java +3 −58
| @@ -27,7 +27,6 @@ | ||
| 27 | 27 | import static com.datastax.driver.core.CodecUtils.listOf; |
| 28 | 28 | import static com.datastax.driver.core.CodecUtils.mapOf; |
| 29 | 29 | import static com.datastax.driver.core.CodecUtils.setOf; |
| 30 | -import static com.datastax.driver.core.DataType.Name.*; | |
| 31 | 30 | |
| 32 | 31 | abstract class AbstractAddressableByIndexData<T extends SettableByIndexData<T>> extends AbstractGettableByIndexData implements SettableByIndexData<T> { |
| 33 | 32 | |
| @@ -49,7 +48,6 @@ | ||
| 49 | 48 | } |
| 50 | 49 | |
| 51 | 50 | public T setBool(int i, boolean v) { |
| 52 | - checkType(i, BOOLEAN); | |
| 53 | 51 | TypeCodec<Boolean> codec = codecFor(i, Boolean.class); |
| 54 | 52 | ByteBuffer bb; |
| 55 | 53 | if(codec instanceof TypeCodec.PrimitiveBooleanCodec) |
| @@ -60,7 +58,6 @@ | ||
| 60 | 58 | } |
| 61 | 59 | |
| 62 | 60 | public T setByte(int i, byte v) { |
| 63 | - checkType(i, TINYINT); | |
| 64 | 61 | TypeCodec<Byte> codec = codecFor(i, Byte.class); |
| 65 | 62 | ByteBuffer bb; |
| 66 | 63 | if(codec instanceof TypeCodec.PrimitiveByteCodec) |
| @@ -71,7 +68,6 @@ | ||
| 71 | 68 | } |
| 72 | 69 | |
| 73 | 70 | public T setShort(int i, short v) { |
| 74 | - checkType(i, SMALLINT); | |
| 75 | 71 | TypeCodec<Short> codec = codecFor(i, Short.class); |
| 76 | 72 | ByteBuffer bb; |
| 77 | 73 | if(codec instanceof TypeCodec.PrimitiveShortCodec) |
| @@ -82,7 +78,6 @@ | ||
| 82 | 78 | } |
| 83 | 79 | |
| 84 | 80 | public T setInt(int i, int v) { |
| 85 | - checkType(i, INT); | |
| 86 | 81 | TypeCodec<Integer> codec = codecFor(i, Integer.class); |
| 87 | 82 | ByteBuffer bb; |
| 88 | 83 | if(codec instanceof TypeCodec.PrimitiveIntCodec) |
| @@ -93,7 +88,6 @@ | ||
| 93 | 88 | } |
| 94 | 89 | |
| 95 | 90 | public T setLong(int i, long v) { |
| 96 | - checkType(i, BIGINT, COUNTER); | |
| 97 | 91 | TypeCodec<Long> codec = codecFor(i, Long.class); |
| 98 | 92 | ByteBuffer bb; |
| 99 | 93 | if(codec instanceof TypeCodec.PrimitiveLongCodec) |
| @@ -104,17 +98,14 @@ | ||
| 104 | 98 | } |
| 105 | 99 | |
| 106 | 100 | public T setTimestamp(int i, Date v) { |
| 107 | - checkType(i, TIMESTAMP); | |
| 108 | 101 | return setValue(i, codecFor(i, Date.class).serialize(v, protocolVersion)); |
| 109 | 102 | } |
| 110 | 103 | |
| 111 | 104 | public T setDate(int i, LocalDate v) { |
| 112 | - checkType(i, DATE); | |
| 113 | 105 | return setValue(i, codecFor(i, LocalDate.class).serialize(v, protocolVersion)); |
| 114 | 106 | } |
| 115 | 107 | |
| 116 | 108 | public T setTime(int i, long v) { |
| 117 | - checkType(i, TIME); | |
| 118 | 109 | TypeCodec<Long> codec = codecFor(i, Long.class); |
| 119 | 110 | ByteBuffer bb; |
| 120 | 111 | if(codec instanceof TypeCodec.PrimitiveLongCodec) |
| @@ -125,7 +116,6 @@ | ||
| 125 | 116 | } |
| 126 | 117 | |
| 127 | 118 | public T setFloat(int i, float v) { |
| 128 | - checkType(i, FLOAT); | |
| 129 | 119 | TypeCodec<Float> codec = codecFor(i, Float.class); |
| 130 | 120 | ByteBuffer bb; |
| 131 | 121 | if(codec instanceof TypeCodec.PrimitiveFloatCodec) |
| @@ -136,7 +126,6 @@ | ||
| 136 | 126 | } |
| 137 | 127 | |
| 138 | 128 | public T setDouble(int i, double v) { |
| 139 | - checkType(i, DOUBLE); | |
| 140 | 129 | TypeCodec<Double> codec = codecFor(i, Double.class); |
| 141 | 130 | ByteBuffer bb; |
| 142 | 131 | if(codec instanceof TypeCodec.PrimitiveDoubleCodec) |
| @@ -147,12 +136,10 @@ | ||
| 147 | 136 | } |
| 148 | 137 | |
| 149 | 138 | public T setString(int i, String v) { |
| 150 | - checkType(i, VARCHAR, TEXT, ASCII); | |
| 151 | 139 | return setValue(i, codecFor(i, String.class).serialize(v, protocolVersion)); |
| 152 | 140 | } |
| 153 | 141 | |
| 154 | 142 | public T setBytes(int i, ByteBuffer v) { |
| 155 | - checkType(i, BLOB); | |
| 156 | 143 | return setValue(i, codecFor(i, ByteBuffer.class).serialize(v, protocolVersion)); |
| 157 | 144 | } |
| 158 | 145 | |
| @@ -161,37 +148,24 @@ | ||
| 161 | 148 | } |
| 162 | 149 | |
| 163 | 150 | public T setVarint(int i, BigInteger v) { |
| 164 | - checkType(i, VARINT); | |
| 165 | 151 | return setValue(i, codecFor(i, BigInteger.class).serialize(v, protocolVersion)); |
| 166 | 152 | } |
| 167 | 153 | |
| 168 | 154 | public T setDecimal(int i, BigDecimal v) { |
| 169 | - checkType(i, DECIMAL); | |
| 170 | 155 | return setValue(i, codecFor(i, BigDecimal.class).serialize(v, protocolVersion)); |
| 171 | 156 | } |
| 172 | 157 | |
| 173 | 158 | public T setUUID(int i, UUID v) { |
| 174 | - checkType(i, UUID, TIMEUUID); | |
| 175 | 159 | return setValue(i, codecFor(i, UUID.class).serialize(v, protocolVersion)); |
| 176 | 160 | } |
| 177 | 161 | |
| 178 | 162 | public T setInet(int i, InetAddress v) { |
| 179 | - checkType(i, INET); | |
| 180 | 163 | return setValue(i, codecFor(i, InetAddress.class).serialize(v, protocolVersion)); |
| 181 | 164 | } |
| 182 | 165 | |
| 183 | 166 | @SuppressWarnings("unchecked") |
| 184 | 167 | public <E> T setList(int i, List<E> v) { |
| 185 | - checkType(i, LIST); | |
| 186 | - if(v == null || v.isEmpty()) { | |
| 187 | - // no runtime inspection possible, rely on the underlying metadata | |
| 188 | - return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 189 | - } else { | |
| 190 | - // inspect the first element and locate a codec that accepts both the underlying CQL type and the actual Java type | |
| 191 | - DataType eltCqlType = getType(i).getTypeArguments().get(0); | |
| 192 | - TypeToken<E> eltJavaType = getCodecRegistry().codecFor(eltCqlType, v.iterator().next()).getJavaType(); | |
| 193 | - return setValue(i, codecFor(i, listOf(eltJavaType)).serialize(v, protocolVersion)); | |
| 194 | - } | |
| 168 | + return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 195 | 169 | } |
| 196 | 170 | |
| 197 | 171 | @Override |
| @@ -206,19 +180,7 @@ | ||
| 206 | 180 | |
| 207 | 181 | @SuppressWarnings("unchecked") |
| 208 | 182 | public <K, V> T setMap(int i, Map<K, V> v) { |
| 209 | - checkType(i, MAP); | |
| 210 | - if(v == null || v.isEmpty()) { | |
| 211 | - // no runtime inspection possible, rely on the underlying metadata | |
| 212 | - return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 213 | - } else { | |
| 214 | - // inspect the first element and locate a codec that accepts both the underlying CQL type and the actual Java types for keys and values | |
| 215 | - DataType keysCqlType = getType(i).getTypeArguments().get(0); | |
| 216 | - DataType valuesCqlType = getType(i).getTypeArguments().get(1); | |
| 217 | - Map.Entry<K, V> entry = v.entrySet().iterator().next(); | |
| 218 | - TypeToken<K> keysType = getCodecRegistry().codecFor(keysCqlType, entry.getKey()).getJavaType(); | |
| 219 | - TypeToken<V> valuesType = getCodecRegistry().codecFor(valuesCqlType, entry.getValue()).getJavaType(); | |
| 220 | - return setValue(i, codecFor(i, mapOf(keysType, valuesType)).serialize(v, protocolVersion)); | |
| 221 | - } | |
| 183 | + return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 222 | 184 | } |
| 223 | 185 | |
| 224 | 186 | @Override |
| @@ -233,16 +195,7 @@ | ||
| 233 | 195 | |
| 234 | 196 | @SuppressWarnings("unchecked") |
| 235 | 197 | public <E> T setSet(int i, Set<E> v) { |
| 236 | - checkType(i, SET); | |
| 237 | - if(v == null || v.isEmpty()) { | |
| 238 | - // no runtime inspection possible, rely on the underlying metadata | |
| 239 | - return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 240 | - } else { | |
| 241 | - // inspect the first element and locate a codec that accepts both the underlying CQL type and the actual Java type | |
| 242 | - DataType eltCqlType = getType(i).getTypeArguments().get(0); | |
| 243 | - TypeToken<E> eltJavaType = getCodecRegistry().codecFor(eltCqlType, v.iterator().next()).getJavaType(); | |
| 244 | - return setValue(i, codecFor(i, setOf(eltJavaType)).serialize(v, protocolVersion)); | |
| 245 | - } | |
| 198 | + return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 246 | 199 | } |
| 247 | 200 | |
| 248 | 201 | @Override |
| @@ -256,21 +209,13 @@ | ||
| 256 | 209 | } |
| 257 | 210 | |
| 258 | 211 | public T setUDTValue(int i, UDTValue v) { |
| 259 | - checkType(i, UDT); | |
| 260 | 212 | return setValue(i, codecFor(i, UDTValue.class).serialize(v, protocolVersion)); |
| 261 | 213 | } |
| 262 | 214 | |
| 263 | 215 | public T setTupleValue(int i, TupleValue v) { |
| 264 | - checkType(i, TUPLE); | |
| 265 | 216 | return setValue(i, codecFor(i, TupleValue.class).serialize(v, protocolVersion)); |
| 266 | 217 | } |
| 267 | 218 | |
| 268 | - @Override | |
| 269 | - public <V> T setObject(int i, V v) { | |
| 270 | - TypeCodec<V> codec = v == null ? this.<V>codecFor(i) : codecFor(i, v); | |
| 271 | - return set(i, v, codec); | |
| 272 | - } | |
| 273 | - | |
| 274 | 219 | @Override |
| 275 | 220 | public <V> T set(int i, V v, Class<V> targetClass) { |
| 276 | 221 | return set(i, v, codecFor(i, targetClass)); |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractData.java +45 −66
| @@ -27,7 +27,6 @@ | ||
| 27 | 27 | import static com.datastax.driver.core.CodecUtils.listOf; |
| 28 | 28 | import static com.datastax.driver.core.CodecUtils.mapOf; |
| 29 | 29 | import static com.datastax.driver.core.CodecUtils.setOf; |
| 30 | -import static com.datastax.driver.core.DataType.Name.*; | |
| 31 | 30 | |
| 32 | 31 | // We don't want to expose this one: it's less useful externally and it's a bit ugly to expose anyway (but it's convenient). |
| 33 | 32 | abstract class AbstractData<T extends SettableData<T>> extends AbstractGettableData implements SettableData<T> { |
| @@ -61,16 +60,18 @@ | ||
| 61 | 60 | return wrapped; |
| 62 | 61 | } |
| 63 | 62 | |
| 63 | + @Override | |
| 64 | 64 | protected ByteBuffer getValue(int i) { |
| 65 | 65 | return values[i]; |
| 66 | 66 | } |
| 67 | 67 | |
| 68 | + @Override | |
| 68 | 69 | protected int getIndexOf(String name) { |
| 69 | 70 | return getAllIndexesOf(name)[0]; |
| 70 | 71 | } |
| 71 | 72 | |
| 73 | + @Override | |
| 72 | 74 | public T setBool(int i, boolean v) { |
| 73 | - checkType(i, BOOLEAN); | |
| 74 | 75 | TypeCodec<Boolean> codec = codecFor(i, Boolean.class); |
| 75 | 76 | ByteBuffer bb; |
| 76 | 77 | if(codec instanceof TypeCodec.PrimitiveBooleanCodec) |
| @@ -80,6 +81,7 @@ | ||
| 80 | 81 | return setValue(i, bb); |
| 81 | 82 | } |
| 82 | 83 | |
| 84 | + @Override | |
| 83 | 85 | public T setBool(String name, boolean v) { |
| 84 | 86 | for (int i : getAllIndexesOf(name)) { |
| 85 | 87 | setBool(i, v); |
| @@ -87,8 +89,8 @@ | ||
| 87 | 89 | return wrapped; |
| 88 | 90 | } |
| 89 | 91 | |
| 92 | + @Override | |
| 90 | 93 | public T setByte(int i, byte v) { |
| 91 | - checkType(i, TINYINT); | |
| 92 | 94 | TypeCodec<Byte> codec = codecFor(i, Byte.class); |
| 93 | 95 | ByteBuffer bb; |
| 94 | 96 | if(codec instanceof TypeCodec.PrimitiveByteCodec) |
| @@ -98,6 +100,7 @@ | ||
| 98 | 100 | return setValue(i, bb); |
| 99 | 101 | } |
| 100 | 102 | |
| 103 | + @Override | |
| 101 | 104 | public T setByte(String name, byte v) { |
| 102 | 105 | for (int i : getAllIndexesOf(name)) { |
| 103 | 106 | setByte(i, v); |
| @@ -105,8 +108,8 @@ | ||
| 105 | 108 | return wrapped; |
| 106 | 109 | } |
| 107 | 110 | |
| 111 | + @Override | |
| 108 | 112 | public T setShort(int i, short v) { |
| 109 | - checkType(i, SMALLINT); | |
| 110 | 113 | TypeCodec<Short> codec = codecFor(i, Short.class); |
| 111 | 114 | ByteBuffer bb; |
| 112 | 115 | if(codec instanceof TypeCodec.PrimitiveShortCodec) |
| @@ -116,6 +119,7 @@ | ||
| 116 | 119 | return setValue(i, bb); |
| 117 | 120 | } |
| 118 | 121 | |
| 122 | + @Override | |
| 119 | 123 | public T setShort(String name, short v) { |
| 120 | 124 | for (int i : getAllIndexesOf(name)) { |
| 121 | 125 | setShort(i, v); |
| @@ -123,8 +127,8 @@ | ||
| 123 | 127 | return wrapped; |
| 124 | 128 | } |
| 125 | 129 | |
| 130 | + @Override | |
| 126 | 131 | public T setInt(int i, int v) { |
| 127 | - checkType(i, INT); | |
| 128 | 132 | TypeCodec<Integer> codec = codecFor(i, Integer.class); |
| 129 | 133 | ByteBuffer bb; |
| 130 | 134 | if(codec instanceof TypeCodec.PrimitiveIntCodec) |
| @@ -134,6 +138,7 @@ | ||
| 134 | 138 | return setValue(i, bb); |
| 135 | 139 | } |
| 136 | 140 | |
| 141 | + @Override | |
| 137 | 142 | public T setInt(String name, int v) { |
| 138 | 143 | for (int i : getAllIndexesOf(name)) { |
| 139 | 144 | setInt(i, v); |
| @@ -141,8 +146,8 @@ | ||
| 141 | 146 | return wrapped; |
| 142 | 147 | } |
| 143 | 148 | |
| 149 | + @Override | |
| 144 | 150 | public T setLong(int i, long v) { |
| 145 | - checkType(i, BIGINT, COUNTER); | |
| 146 | 151 | TypeCodec<Long> codec = codecFor(i, Long.class); |
| 147 | 152 | ByteBuffer bb; |
| 148 | 153 | if(codec instanceof TypeCodec.PrimitiveLongCodec) |
| @@ -152,6 +157,7 @@ | ||
| 152 | 157 | return setValue(i, bb); |
| 153 | 158 | } |
| 154 | 159 | |
| 160 | + @Override | |
| 155 | 161 | public T setLong(String name, long v) { |
| 156 | 162 | for (int i : getAllIndexesOf(name)) { |
| 157 | 163 | setLong(i, v); |
| @@ -159,11 +165,12 @@ | ||
| 159 | 165 | return wrapped; |
| 160 | 166 | } |
| 161 | 167 | |
| 168 | + @Override | |
| 162 | 169 | public T setTimestamp(int i, Date v) { |
| 163 | - checkType(i, TIMESTAMP); | |
| 164 | 170 | return setValue(i, codecFor(i, Date.class).serialize(v, protocolVersion)); |
| 165 | 171 | } |
| 166 | 172 | |
| 173 | + @Override | |
| 167 | 174 | public T setTimestamp(String name, Date v) { |
| 168 | 175 | for (int i : getAllIndexesOf(name)) { |
| 169 | 176 | setTimestamp(i, v); |
| @@ -171,11 +178,12 @@ | ||
| 171 | 178 | return wrapped; |
| 172 | 179 | } |
| 173 | 180 | |
| 181 | + @Override | |
| 174 | 182 | public T setDate(int i, LocalDate v) { |
| 175 | - checkType(i, DATE); | |
| 176 | 183 | return setValue(i, codecFor(i, LocalDate.class).serialize(v, protocolVersion)); |
| 177 | 184 | } |
| 178 | 185 | |
| 186 | + @Override | |
| 179 | 187 | public T setDate(String name, LocalDate v) { |
| 180 | 188 | for (int i : getAllIndexesOf(name)) { |
| 181 | 189 | setDate(i, v); |
| @@ -183,8 +191,8 @@ | ||
| 183 | 191 | return wrapped; |
| 184 | 192 | } |
| 185 | 193 | |
| 194 | + @Override | |
| 186 | 195 | public T setTime(int i, long v) { |
| 187 | - checkType(i, TIME); | |
| 188 | 196 | TypeCodec<Long> codec = codecFor(i, Long.class); |
| 189 | 197 | ByteBuffer bb; |
| 190 | 198 | if(codec instanceof TypeCodec.PrimitiveLongCodec) |
| @@ -194,6 +202,7 @@ | ||
| 194 | 202 | return setValue(i, bb); |
| 195 | 203 | } |
| 196 | 204 | |
| 205 | + @Override | |
| 197 | 206 | public T setTime(String name, long v) { |
| 198 | 207 | for (int i : getAllIndexesOf(name)) { |
| 199 | 208 | setTime(i, v); |
| @@ -201,8 +210,8 @@ | ||
| 201 | 210 | return wrapped; |
| 202 | 211 | } |
| 203 | 212 | |
| 213 | + @Override | |
| 204 | 214 | public T setFloat(int i, float v) { |
| 205 | - checkType(i, FLOAT); | |
| 206 | 215 | TypeCodec<Float> codec = codecFor(i, Float.class); |
| 207 | 216 | ByteBuffer bb; |
| 208 | 217 | if(codec instanceof TypeCodec.PrimitiveFloatCodec) |
| @@ -212,6 +221,7 @@ | ||
| 212 | 221 | return setValue(i, bb); |
| 213 | 222 | } |
| 214 | 223 | |
| 224 | + @Override | |
| 215 | 225 | public T setFloat(String name, float v) { |
| 216 | 226 | for (int i : getAllIndexesOf(name)) { |
| 217 | 227 | setFloat(i, v); |
| @@ -219,8 +229,8 @@ | ||
| 219 | 229 | return wrapped; |
| 220 | 230 | } |
| 221 | 231 | |
| 232 | + @Override | |
| 222 | 233 | public T setDouble(int i, double v) { |
| 223 | - checkType(i, DOUBLE); | |
| 224 | 234 | TypeCodec<Double> codec = codecFor(i, Double.class); |
| 225 | 235 | ByteBuffer bb; |
| 226 | 236 | if(codec instanceof TypeCodec.PrimitiveDoubleCodec) |
| @@ -230,6 +240,7 @@ | ||
| 230 | 240 | return setValue(i, bb); |
| 231 | 241 | } |
| 232 | 242 | |
| 243 | + @Override | |
| 233 | 244 | public T setDouble(String name, double v) { |
| 234 | 245 | for (int i : getAllIndexesOf(name)) { |
| 235 | 246 | setDouble(i, v); |
| @@ -237,11 +248,12 @@ | ||
| 237 | 248 | return wrapped; |
| 238 | 249 | } |
| 239 | 250 | |
| 251 | + @Override | |
| 240 | 252 | public T setString(int i, String v) { |
| 241 | - checkType(i, VARCHAR, TEXT, ASCII); | |
| 242 | 253 | return setValue(i, codecFor(i, String.class).serialize(v, protocolVersion)); |
| 243 | 254 | } |
| 244 | 255 | |
| 256 | + @Override | |
| 245 | 257 | public T setString(String name, String v) { |
| 246 | 258 | for (int i : getAllIndexesOf(name)) { |
| 247 | 259 | setString(i, v); |
| @@ -249,11 +261,12 @@ | ||
| 249 | 261 | return wrapped; |
| 250 | 262 | } |
| 251 | 263 | |
| 264 | + @Override | |
| 252 | 265 | public T setBytes(int i, ByteBuffer v) { |
| 253 | - checkType(i, BLOB); | |
| 254 | 266 | return setValue(i, codecFor(i, ByteBuffer.class).serialize(v, protocolVersion)); |
| 255 | 267 | } |
| 256 | 268 | |
| 269 | + @Override | |
| 257 | 270 | public T setBytes(String name, ByteBuffer v) { |
| 258 | 271 | for (int i : getAllIndexesOf(name)) { |
| 259 | 272 | setBytes(i, v); |
| @@ -261,10 +274,12 @@ | ||
| 261 | 274 | return wrapped; |
| 262 | 275 | } |
| 263 | 276 | |
| 277 | + @Override | |
| 264 | 278 | public T setBytesUnsafe(int i, ByteBuffer v) { |
| 265 | 279 | return setValue(i, v == null ? null : v.duplicate()); |
| 266 | 280 | } |
| 267 | 281 | |
| 282 | + @Override | |
| 268 | 283 | public T setBytesUnsafe(String name, ByteBuffer v) { |
| 269 | 284 | ByteBuffer value = v == null ? null : v.duplicate(); |
| 270 | 285 | for (int i : getAllIndexesOf(name)) { |
| @@ -273,11 +288,12 @@ | ||
| 273 | 288 | return wrapped; |
| 274 | 289 | } |
| 275 | 290 | |
| 291 | + @Override | |
| 276 | 292 | public T setVarint(int i, BigInteger v) { |
| 277 | - checkType(i, VARINT); | |
| 278 | 293 | return setValue(i, codecFor(i, BigInteger.class).serialize(v, protocolVersion)); |
| 279 | 294 | } |
| 280 | 295 | |
| 296 | + @Override | |
| 281 | 297 | public T setVarint(String name, BigInteger v) { |
| 282 | 298 | for (int i : getAllIndexesOf(name)) { |
| 283 | 299 | setVarint(i, v); |
| @@ -285,11 +301,12 @@ | ||
| 285 | 301 | return wrapped; |
| 286 | 302 | } |
| 287 | 303 | |
| 304 | + @Override | |
| 288 | 305 | public T setDecimal(int i, BigDecimal v) { |
| 289 | - checkType(i, DECIMAL); | |
| 290 | 306 | return setValue(i, codecFor(i, BigDecimal.class).serialize(v, protocolVersion)); |
| 291 | 307 | } |
| 292 | 308 | |
| 309 | + @Override | |
| 293 | 310 | public T setDecimal(String name, BigDecimal v) { |
| 294 | 311 | for (int i : getAllIndexesOf(name)) { |
| 295 | 312 | setDecimal(i, v); |
| @@ -297,11 +314,12 @@ | ||
| 297 | 314 | return wrapped; |
| 298 | 315 | } |
| 299 | 316 | |
| 317 | + @Override | |
| 300 | 318 | public T setUUID(int i, UUID v) { |
| 301 | - checkType(i, UUID, TIMEUUID); | |
| 302 | 319 | return setValue(i, codecFor(i, UUID.class).serialize(v, protocolVersion)); |
| 303 | 320 | } |
| 304 | 321 | |
| 322 | + @Override | |
| 305 | 323 | public T setUUID(String name, UUID v) { |
| 306 | 324 | for (int i : getAllIndexesOf(name)) { |
| 307 | 325 | setUUID(i, v); |
| @@ -309,11 +327,12 @@ | ||
| 309 | 327 | return wrapped; |
| 310 | 328 | } |
| 311 | 329 | |
| 330 | + @Override | |
| 312 | 331 | public T setInet(int i, InetAddress v) { |
| 313 | - checkType(i, INET); | |
| 314 | 332 | return setValue(i, codecFor(i, InetAddress.class).serialize(v, protocolVersion)); |
| 315 | 333 | } |
| 316 | 334 | |
| 335 | + @Override | |
| 317 | 336 | public T setInet(String name, InetAddress v) { |
| 318 | 337 | for (int i : getAllIndexesOf(name)) { |
| 319 | 338 | setInet(i, v); |
| @@ -337,18 +356,10 @@ | ||
| 337 | 356 | return wrapped; |
| 338 | 357 | } |
| 339 | 358 | |
| 359 | + @Override | |
| 340 | 360 | @SuppressWarnings("unchecked") |
| 341 | 361 | public <E> T setList(int i, List<E> v) { |
| 342 | - checkType(i, LIST); | |
| 343 | - if(v == null || v.isEmpty()) { | |
| 344 | - // no runtime inspection possible, rely on the underlying metadata | |
| 345 | - return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 346 | - } else { | |
| 347 | - // inspect the first element and locate a codec that accepts both the underlying CQL type and the actual Java type | |
| 348 | - DataType eltCqlType = getType(i).getTypeArguments().get(0); | |
| 349 | - TypeToken<E> eltJavaType = getCodecRegistry().codecFor(eltCqlType, v.iterator().next()).getJavaType(); | |
| 350 | - return setValue(i, codecFor(i, listOf(eltJavaType)).serialize(v, protocolVersion)); | |
| 351 | - } | |
| 362 | + return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 352 | 363 | } |
| 353 | 364 | |
| 354 | 365 | @Override |
| @@ -388,19 +399,7 @@ | ||
| 388 | 399 | @SuppressWarnings("unchecked") |
| 389 | 400 | @Override |
| 390 | 401 | public <K, V> T setMap(int i, Map<K, V> v) { |
| 391 | - checkType(i, MAP); | |
| 392 | - if(v == null || v.isEmpty()) { | |
| 393 | - // no runtime inspection possible, rely on the underlying metadata | |
| 394 | - return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 395 | - } else { | |
| 396 | - // inspect the first element and locate a codec that accepts both the underlying CQL type and the actual Java types for keys and values | |
| 397 | - DataType keysCqlType = getType(i).getTypeArguments().get(0); | |
| 398 | - DataType valuesCqlType = getType(i).getTypeArguments().get(1); | |
| 399 | - Map.Entry<K, V> entry = v.entrySet().iterator().next(); | |
| 400 | - TypeToken<K> keysType = getCodecRegistry().codecFor(keysCqlType, entry.getKey()).getJavaType(); | |
| 401 | - TypeToken<V> valuesType = getCodecRegistry().codecFor(valuesCqlType, entry.getValue()).getJavaType(); | |
| 402 | - return setValue(i, codecFor(i, mapOf(keysType, valuesType)).serialize(v, protocolVersion)); | |
| 403 | - } | |
| 402 | + return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 404 | 403 | } |
| 405 | 404 | |
| 406 | 405 | @Override |
| @@ -437,18 +436,10 @@ | ||
| 437 | 436 | return wrapped; |
| 438 | 437 | } |
| 439 | 438 | |
| 439 | + @Override | |
| 440 | 440 | @SuppressWarnings("unchecked") |
| 441 | 441 | public <E> T setSet(int i, Set<E> v) { |
| 442 | - checkType(i, SET); | |
| 443 | - if(v == null || v.isEmpty()) { | |
| 444 | - // no runtime inspection possible, rely on the underlying metadata | |
| 445 | - return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 446 | - } else { | |
| 447 | - // inspect the first element and locate a codec that accepts both the underlying CQL type and the actual Java type | |
| 448 | - DataType eltCqlType = getType(i).getTypeArguments().get(0); | |
| 449 | - TypeToken<E> eltJavaType = getCodecRegistry().codecFor(eltCqlType, v.iterator().next()).getJavaType(); | |
| 450 | - return setValue(i, codecFor(i, setOf(eltJavaType)).serialize(v, protocolVersion)); | |
| 451 | - } | |
| 442 | + return setValue(i, codecFor(i).serialize(v, protocolVersion)); | |
| 452 | 443 | } |
| 453 | 444 | |
| 454 | 445 | @Override |
| @@ -485,11 +476,12 @@ | ||
| 485 | 476 | return wrapped; |
| 486 | 477 | } |
| 487 | 478 | |
| 479 | + @Override | |
| 488 | 480 | public T setUDTValue(int i, UDTValue v) { |
| 489 | - checkType(i, UDT); | |
| 490 | 481 | return setValue(i, codecFor(i, UDTValue.class).serialize(v, protocolVersion)); |
| 491 | 482 | } |
| 492 | 483 | |
| 484 | + @Override | |
| 493 | 485 | public T setUDTValue(String name, UDTValue v) { |
| 494 | 486 | for (int i : getAllIndexesOf(name)) { |
| 495 | 487 | setUDTValue(i, v); |
| @@ -497,11 +489,12 @@ | ||
| 497 | 489 | return wrapped; |
| 498 | 490 | } |
| 499 | 491 | |
| 492 | + @Override | |
| 500 | 493 | public T setTupleValue(int i, TupleValue v) { |
| 501 | - checkType(i, TUPLE); | |
| 502 | 494 | return setValue(i, codecFor(i, TupleValue.class).serialize(v, protocolVersion)); |
| 503 | 495 | } |
| 504 | 496 | |
| 497 | + @Override | |
| 505 | 498 | public T setTupleValue(String name, TupleValue v) { |
| 506 | 499 | for (int i : getAllIndexesOf(name)) { |
| 507 | 500 | setTupleValue(i, v); |
| @@ -509,20 +502,6 @@ | ||
| 509 | 502 | return wrapped; |
| 510 | 503 | } |
| 511 | 504 | |
| 512 | - @Override | |
| 513 | - public <V> T setObject(int i, V v) { | |
| 514 | - TypeCodec<V> codec = v == null ? this.<V>codecFor(i) : codecFor(i, v); | |
| 515 | - return set(i, v, codec); | |
| 516 | - } | |
| 517 | - | |
| 518 | - @Override | |
| 519 | - public <V> T setObject(String name, V v) { | |
| 520 | - for (int i : getAllIndexesOf(name)) { | |
| 521 | - setObject(i, v); | |
| 522 | - } | |
| 523 | - return wrapped; | |
| 524 | - } | |
| 525 | - | |
| 526 | 505 | @Override |
| 527 | 506 | public <V> T set(int i, V v, Class<V> targetClass) { |
| 528 | 507 | return set(i, v, codecFor(i, targetClass)); |
modified driver-core/src/main/java/com/datastax/driver/core/AbstractGettableByIndexData.java +1 −47
| @@ -19,23 +19,15 @@ | ||
| 19 | 19 | import java.math.BigInteger; |
| 20 | 20 | import java.net.InetAddress; |
| 21 | 21 | import java.nio.ByteBuffer; |
| 22 | -import java.util.Date; | |
| 23 | -import java.util.List; | |
| 24 | -import java.util.Map; | |
| 25 | -import java.util.Set; | |
| 26 | -import java.util.UUID; | |
| 22 | +import java.util.*; | |
| 27 | 23 | |
| 28 | 24 | import com.google.common.reflect.TypeToken; |
| 29 | 25 | |
| 30 | -import static com.google.common.collect.Sets.newHashSet; | |
| 31 | -import static com.google.common.collect.Sets.newLinkedHashSet; | |
| 32 | - | |
| 33 | 26 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 34 | 27 | |
| 35 | 28 | import static com.datastax.driver.core.CodecUtils.listOf; |
| 36 | 29 | import static com.datastax.driver.core.CodecUtils.mapOf; |
| 37 | 30 | import static com.datastax.driver.core.CodecUtils.setOf; |
| 38 | -import static com.datastax.driver.core.DataType.Name.*; | |
| 39 | 31 | |
| 40 | 32 | abstract class AbstractGettableByIndexData implements GettableByIndexData { |
| 41 | 33 | |
| @@ -93,25 +85,12 @@ | ||
| 93 | 85 | return getCodecRegistry().codecFor(getType(i), value); |
| 94 | 86 | } |
| 95 | 87 | |
| 96 | - // Note: we avoid having a vararg method to avoid the array allocation that comes with it. | |
| 97 | 88 | protected void checkType(int i, DataType.Name actual) { |
| 98 | 89 | DataType.Name expected = getType(i).getName(); |
| 99 | 90 | if (!actual.isCompatibleWith(expected)) |
| 100 | 91 | throw new InvalidTypeException(String.format("Value %s is of type %s, not %s", getName(i), expected, actual)); |
| 101 | 92 | } |
| 102 | 93 | |
| 103 | - protected void checkType(int i, DataType.Name actual1, DataType.Name actual2) { | |
| 104 | - DataType.Name expected = getType(i).getName(); | |
| 105 | - if (!actual1.isCompatibleWith(expected) && !actual2.isCompatibleWith(expected)) | |
| 106 | - throw new InvalidTypeException(String.format("Value %s is of type %s, not any of [%s, %s]", getName(i), expected, actual1, actual2)); | |
| 107 | - } | |
| 108 | - | |
| 109 | - protected void checkType(int i, DataType.Name actual1, DataType.Name actual2, DataType.Name actual3) { | |
| 110 | - DataType.Name expected = getType(i).getName(); | |
| 111 | - if (!actual1.isCompatibleWith(expected) && !actual2.isCompatibleWith(expected) && !actual3.isCompatibleWith(expected)) | |
| 112 | - throw new InvalidTypeException(String.format("Value %s is of type %s, not any of [%s, %s, %s]", getName(i), expected, actual1, actual2, actual3)); | |
| 113 | - } | |
| 114 | - | |
| 115 | 94 | /** |
| 116 | 95 | * {@inheritDoc} |
| 117 | 96 | */ |
| @@ -125,12 +104,7 @@ | ||
| 125 | 104 | */ |
| 126 | 105 | @Override |
| 127 | 106 | public boolean getBool(int i) { |
| 128 | - checkType(i, BOOLEAN); | |
| 129 | - | |
| 130 | 107 | ByteBuffer value = getValue(i); |
| 131 | - if (value == null || value.remaining() == 0) | |
| 132 | - return false; | |
| 133 | - | |
| 134 | 108 | TypeCodec<Boolean> codec = codecFor(i, Boolean.class); |
| 135 | 109 | if(codec instanceof TypeCodec.PrimitiveBooleanCodec) |
| 136 | 110 | return ((TypeCodec.PrimitiveBooleanCodec) codec).deserializeNoBoxing(value, protocolVersion); |
| @@ -143,7 +117,6 @@ | ||
| 143 | 117 | */ |
| 144 | 118 | @Override |
| 145 | 119 | public byte getByte(int i) { |
| 146 | - checkType(i, TINYINT); | |
| 147 | 120 | ByteBuffer value = getValue(i); |
| 148 | 121 | TypeCodec<Byte> codec = codecFor(i, Byte.class); |
| 149 | 122 | if(codec instanceof TypeCodec.PrimitiveByteCodec) |
| @@ -157,7 +130,6 @@ | ||
| 157 | 130 | */ |
| 158 | 131 | @Override |
| 159 | 132 | public short getShort(int i) { |
| 160 | - checkType(i, SMALLINT); | |
| 161 | 133 | ByteBuffer value = getValue(i); |
| 162 | 134 | TypeCodec<Short> codec = codecFor(i, Short.class); |
| 163 | 135 | if(codec instanceof TypeCodec.PrimitiveShortCodec) |
| @@ -171,7 +143,6 @@ | ||
| 171 | 143 | */ |
| 172 | 144 | @Override |
| 173 | 145 | public int getInt(int i) { |
| 174 | - checkType(i, INT); | |
| 175 | 146 | ByteBuffer value = getValue(i); |
| 176 | 147 | TypeCodec<Integer> codec = codecFor(i, Integer.class); |
| 177 | 148 | if(codec instanceof TypeCodec.PrimitiveIntCodec) |
| @@ -185,7 +156,6 @@ | ||
| 185 | 156 | */ |
| 186 | 157 | @Override |
| 187 | 158 | public long getLong(int i) { |
| 188 | - checkType(i, BIGINT, COUNTER); | |
| 189 | 159 | ByteBuffer value = getValue(i); |
| 190 | 160 | TypeCodec<Long> codec = codecFor(i, Long.class); |
| 191 | 161 | if(codec instanceof TypeCodec.PrimitiveLongCodec) |
| @@ -199,7 +169,6 @@ | ||
| 199 | 169 | */ |
| 200 | 170 | @Override |
| 201 | 171 | public Date getTimestamp(int i) { |
| 202 | - checkType(i, TIMESTAMP); | |
| 203 | 172 | ByteBuffer value = getValue(i); |
| 204 | 173 | return codecFor(i, Date.class).deserialize(value, protocolVersion); |
| 205 | 174 | } |
| @@ -209,7 +178,6 @@ | ||
| 209 | 178 | */ |
| 210 | 179 | @Override |
| 211 | 180 | public LocalDate getDate(int i) { |
| 212 | - checkType(i, DATE); | |
| 213 | 181 | ByteBuffer value = getValue(i); |
| 214 | 182 | return codecFor(i, LocalDate.class).deserialize(value, protocolVersion); |
| 215 | 183 | } |
| @@ -219,7 +187,6 @@ | ||
| 219 | 187 | */ |
| 220 | 188 | @Override |
| 221 | 189 | public long getTime(int i) { |
| 222 | - checkType(i, TIME); | |
| 223 | 190 | ByteBuffer value = getValue(i); |
| 224 | 191 | TypeCodec<Long> codec = codecFor(i, Long.class); |
| 225 | 192 | if(codec instanceof TypeCodec.PrimitiveLongCodec) |
| @@ -233,7 +200,6 @@ | ||
| 233 | 200 | */ |
| 234 | 201 | @Override |
| 235 | 202 | public float getFloat(int i) { |
| 236 | - checkType(i, FLOAT); | |
| 237 | 203 | ByteBuffer value = getValue(i); |
| 238 | 204 | TypeCodec<Float> codec = codecFor(i, Float.class); |
| 239 | 205 | if(codec instanceof TypeCodec.PrimitiveFloatCodec) |
| @@ -247,7 +213,6 @@ | ||
| 247 | 213 | */ |
| 248 | 214 | @Override |
| 249 | 215 | public double getDouble(int i) { |
| 250 | - checkType(i, DOUBLE); | |
| 251 | 216 | ByteBuffer value = getValue(i); |
| 252 | 217 | TypeCodec<Double> codec = codecFor(i, Double.class); |
| 253 | 218 | if(codec instanceof TypeCodec.PrimitiveDoubleCodec) |
| @@ -272,7 +237,6 @@ | ||
| 272 | 237 | */ |
| 273 | 238 | @Override |
| 274 | 239 | public ByteBuffer getBytes(int i) { |
| 275 | - checkType(i, BLOB); | |
| 276 | 240 | ByteBuffer value = getValue(i); |
| 277 | 241 | return codecFor(i, ByteBuffer.class).deserialize(value, protocolVersion); |
| 278 | 242 | } |
| @@ -282,7 +246,6 @@ | ||
| 282 | 246 | */ |
| 283 | 247 | @Override |
| 284 | 248 | public String getString(int i) { |
| 285 | - checkType(i, VARCHAR, ASCII); | |
| 286 | 249 | ByteBuffer value = getValue(i); |
| 287 | 250 | return codecFor(i, String.class).deserialize(value, protocolVersion); |
| 288 | 251 | } |
| @@ -292,7 +255,6 @@ | ||
| 292 | 255 | */ |
| 293 | 256 | @Override |
| 294 | 257 | public BigInteger getVarint(int i) { |
| 295 | - checkType(i, VARINT); | |
| 296 | 258 | ByteBuffer value = getValue(i); |
| 297 | 259 | return codecFor(i, BigInteger.class).deserialize(value, protocolVersion); |
| 298 | 260 | } |
| @@ -302,7 +264,6 @@ | ||
| 302 | 264 | */ |
| 303 | 265 | @Override |
| 304 | 266 | public BigDecimal getDecimal(int i) { |
| 305 | - checkType(i, DECIMAL); | |
| 306 | 267 | ByteBuffer value = getValue(i); |
| 307 | 268 | return codecFor(i, BigDecimal.class).deserialize(value, protocolVersion); |
| 308 | 269 | } |
| @@ -312,7 +273,6 @@ | ||
| 312 | 273 | */ |
| 313 | 274 | @Override |
| 314 | 275 | public UUID getUUID(int i) { |
| 315 | - checkType(i, UUID, TIMEUUID); | |
| 316 | 276 | ByteBuffer value = getValue(i); |
| 317 | 277 | return codecFor(i, UUID.class).deserialize(value, protocolVersion); |
| 318 | 278 | } |
| @@ -322,7 +282,6 @@ | ||
| 322 | 282 | */ |
| 323 | 283 | @Override |
| 324 | 284 | public InetAddress getInet(int i) { |
| 325 | - checkType(i, INET); | |
| 326 | 285 | ByteBuffer value = getValue(i); |
| 327 | 286 | return codecFor(i, InetAddress.class).deserialize(value, protocolVersion); |
| 328 | 287 | } |
| @@ -342,7 +301,6 @@ | ||
| 342 | 301 | @Override |
| 343 | 302 | @SuppressWarnings("unchecked") |
| 344 | 303 | public <T> List<T> getList(int i, TypeToken<T> elementsType) { |
| 345 | - checkType(i, LIST); | |
| 346 | 304 | ByteBuffer value = getValue(i); |
| 347 | 305 | TypeToken<List<T>> javaType = listOf(elementsType); |
| 348 | 306 | return codecFor(i, javaType).deserialize(value, protocolVersion); |
| @@ -363,7 +321,6 @@ | ||
| 363 | 321 | @Override |
| 364 | 322 | @SuppressWarnings("unchecked") |
| 365 | 323 | public <T> Set<T> getSet(int i, TypeToken<T> elementsType) { |
| 366 | - checkType(i, SET); | |
| 367 | 324 | ByteBuffer value = getValue(i); |
| 368 | 325 | TypeToken<Set<T>> javaType = setOf(elementsType); |
| 369 | 326 | return codecFor(i, javaType).deserialize(value, protocolVersion); |
| @@ -384,7 +341,6 @@ | ||
| 384 | 341 | @Override |
| 385 | 342 | @SuppressWarnings("unchecked") |
| 386 | 343 | public <K, V> Map<K, V> getMap(int i, TypeToken<K> keysType, TypeToken<V> valuesType) { |
| 387 | - checkType(i, MAP); | |
| 388 | 344 | ByteBuffer value = getValue(i); |
| 389 | 345 | TypeToken<Map<K, V>> javaType = mapOf(keysType, valuesType); |
| 390 | 346 | return codecFor(i, javaType).deserialize(value, protocolVersion); |
| @@ -396,7 +352,6 @@ | ||
| 396 | 352 | @Override |
| 397 | 353 | @SuppressWarnings("unchecked") |
| 398 | 354 | public UDTValue getUDTValue(int i) { |
| 399 | - checkType(i, UDT); | |
| 400 | 355 | ByteBuffer value = getValue(i); |
| 401 | 356 | return codecFor(i, UDTValue.class).deserialize(value, protocolVersion); |
| 402 | 357 | } |
| @@ -407,7 +362,6 @@ | ||
| 407 | 362 | @Override |
| 408 | 363 | @SuppressWarnings("unchecked") |
| 409 | 364 | public TupleValue getTupleValue(int i) { |
| 410 | - checkType(i, TUPLE); | |
| 411 | 365 | ByteBuffer value = getValue(i); |
| 412 | 366 | return codecFor(i, TupleValue.class).deserialize(value, protocolVersion); |
| 413 | 367 | } |
modified driver-core/src/main/java/com/datastax/driver/core/ArrayBackedResultSet.java +1 −1
| @@ -442,6 +442,6 @@ | ||
| 442 | 442 | if (value == null || value.remaining() == 0) |
| 443 | 443 | return false; |
| 444 | 444 | |
| 445 | - return TypeCodec.BooleanCodec.instance.deserializeNoBoxing(value, protocolVersion); | |
| 445 | + return TypeCodec.cboolean().deserializeNoBoxing(value, protocolVersion); | |
| 446 | 446 | } |
| 447 | 447 | } |
modified driver-core/src/main/java/com/datastax/driver/core/BoundStatement.java +0 −16
| @@ -838,22 +838,6 @@ | ||
| 838 | 838 | return wrapper.setTupleValue(name, v); |
| 839 | 839 | } |
| 840 | 840 | |
| 841 | - /** | |
| 842 | - * {@inheritDoc} | |
| 843 | - */ | |
| 844 | - @Override | |
| 845 | - public <V> BoundStatement setObject(int i, V v) { | |
| 846 | - return wrapper.setObject(i, v); | |
| 847 | - } | |
| 848 | - | |
| 849 | - /** | |
| 850 | - * {@inheritDoc} | |
| 851 | - */ | |
| 852 | - @Override | |
| 853 | - public <V> BoundStatement setObject(String name, V v) { | |
| 854 | - return wrapper.setObject(name, v); | |
| 855 | - } | |
| 856 | - | |
| 857 | 841 | /** |
| 858 | 842 | * {@inheritDoc} |
| 859 | 843 | */ |
modified driver-core/src/main/java/com/datastax/driver/core/CassandraTypeParser.java +2 −2
| @@ -105,7 +105,7 @@ | ||
| 105 | 105 | |
| 106 | 106 | String keyspace = parser.readOne(); |
| 107 | 107 | parser.skipBlankAndComma(); |
| 108 | - String typeName = TypeCodec.VarcharCodec.instance.deserialize(Bytes.fromHexString("0x" + parser.readOne()), protocolVersion); | |
| 108 | + String typeName = TypeCodec.varchar().deserialize(Bytes.fromHexString("0x" + parser.readOne()), protocolVersion); | |
| 109 | 109 | parser.skipBlankAndComma(); |
| 110 | 110 | Map<String, String> rawFields = parser.getNameAndTypeParameters(); |
| 111 | 111 | List<UserType.Field> fields = new ArrayList<UserType.Field>(rawFields.size()); |
| @@ -319,7 +319,7 @@ | ||
| 319 | 319 | String bbHex = readNextIdentifier(); |
| 320 | 320 | String name = null; |
| 321 | 321 | try { |
| 322 | - name = TypeCodec.VarcharCodec.instance.deserialize(Bytes.fromHexString("0x" + bbHex), ProtocolVersion.NEWEST_SUPPORTED); | |
| 322 | + name = TypeCodec.varchar().deserialize(Bytes.fromHexString("0x" + bbHex), ProtocolVersion.NEWEST_SUPPORTED); | |
| 323 | 323 | } catch (NumberFormatException e) { |
| 324 | 324 | throwSyntaxError(e.getMessage()); |
| 325 | 325 | } |
modified driver-core/src/main/java/com/datastax/driver/core/CodecRegistry.java +169 −126
| @@ -17,6 +17,7 @@ | ||
| 17 | 17 | |
| 18 | 18 | import java.lang.reflect.ParameterizedType; |
| 19 | 19 | import java.lang.reflect.Type; |
| 20 | +import java.nio.ByteBuffer; | |
| 20 | 21 | import java.util.*; |
| 21 | 22 | import java.util.concurrent.CopyOnWriteArrayList; |
| 22 | 23 | import java.util.concurrent.ExecutionException; |
| @@ -31,29 +32,28 @@ | ||
| 31 | 32 | |
| 32 | 33 | import static com.google.common.base.Preconditions.checkNotNull; |
| 33 | 34 | |
| 34 | -import com.datastax.driver.core.TypeCodec.*; | |
| 35 | 35 | import com.datastax.driver.core.exceptions.CodecNotFoundException; |
| 36 | 36 | |
| 37 | -import static com.datastax.driver.core.DataType.Name.*; | |
| 37 | +import static com.datastax.driver.core.DataType.Name.LIST; | |
| 38 | +import static com.datastax.driver.core.DataType.Name.MAP; | |
| 39 | +import static com.datastax.driver.core.DataType.Name.SET; | |
| 38 | 40 | |
| 39 | 41 | /** |
| 40 | - * A registry for {@link TypeCodec}s. When the driver | |
| 42 | + * A registry for {@link TypeCodec codec}s. When the driver | |
| 41 | 43 | * needs to serialize or deserialize an object, |
| 42 | - * it will lookup in the {@link CodecRegistry} for | |
| 44 | + * it will lookup in the registry for | |
| 43 | 45 | * a suitable codec. |
| 44 | 46 | * |
| 45 | 47 | * <h3>Usage</h3> |
| 46 | 48 | * |
| 47 | 49 | * <p> |
| 48 | - * Most users won't need to manipulate {@link CodecRegistry} instances directly. | |
| 49 | - * Only those willing to register user-defined codecs are required to do so. | |
| 50 | - * By default, the driver uses {@link CodecRegistry#DEFAULT_INSTANCE}, a shareable | |
| 51 | - * instance that initially contains all the built-in codecs required by the driver. | |
| 50 | + * By default, the driver uses the CodecRegistry's {@link CodecRegistry#DEFAULT_INSTANCE}, | |
| 51 | + * a shareable instance that initially contains all the built-in codecs required by the driver. | |
| 52 | 52 | * |
| 53 | 53 | * <p> |
| 54 | - * Users willing to customize their {@link CodecRegistry} can do so by {@code register registering} | |
| 55 | - * new {@link TypeCodec}s either on {@link CodecRegistry#DEFAULT_INSTANCE}, | |
| 56 | - * or on a newly-crated one: | |
| 54 | + * For most users, the default instance is good enough and | |
| 55 | + * they won't need to manipulate {@link CodecRegistry} instances directly. | |
| 56 | + * Only those willing to {@link #register(TypeCodec) register} user-defined codecs are required to do so: | |
| 57 | 57 | * |
| 58 | 58 | * <pre> |
| 59 | 59 | * CodecRegistry myCodecRegistry; |
| @@ -61,20 +61,11 @@ | ||
| 61 | 61 | * myCodecRegistry = CodecRegistry.DEFAULT_INSTANCE; |
| 62 | 62 | * // or alternatively, create a new one |
| 63 | 63 | * myCodecRegistry = new CodecRegistry(); |
| 64 | - * // then | |
| 64 | + * // then register additional codecs | |
| 65 | 65 | * myCodecRegistry.register(myCodec1, myCodec2, myCodec3); |
| 66 | 66 | * </pre> |
| 67 | 67 | * |
| 68 | 68 | * <p> |
| 69 | - * Note that the order in which codecs are registered matters; | |
| 70 | - * if a codec is registered while another codec already handles the same | |
| 71 | - * CQL-to-Java mapping, that codec will never be used (unless | |
| 72 | - * the user forces its usage through e.g. {@link GettableByIndexData#get(int, TypeCodec)}). | |
| 73 | - * In order words, it is not possible to replace an existing codec, | |
| 74 | - * specially the built-in ones; it is only possible to enrich the initial | |
| 75 | - * set of codecs with new ones. | |
| 76 | - * | |
| 77 | - * <p> | |
| 78 | 69 | * To be used by the driver, {@link CodecRegistry} instances must then |
| 79 | 70 | * be associated with a {@link Cluster} instance: |
| 80 | 71 | * |
| @@ -90,20 +81,21 @@ | ||
| 90 | 81 | * CodecRegistry registry = cluster.getConfiguration().getCodecRegistry(); |
| 91 | 82 | * </pre> |
| 92 | 83 | * |
| 93 | - * <p> | |
| 94 | - * By default, {@link Cluster} instances will use {@link CodecRegistry#DEFAULT_INSTANCE}. | |
| 95 | - * | |
| 96 | 84 | * <h3>Example</h3> |
| 97 | 85 | * |
| 98 | 86 | * <p> |
| 99 | - * E.g. let's suppose you want to have all your CQL timestamps | |
| 100 | - * deserialized as {@code java.time.DateTime} instances: | |
| 87 | + * Suppose that one wants to have all CQL timestamps seamlessly | |
| 88 | + * converted to {@code java.time.DateTime} instances and vice versa; the following | |
| 89 | + * code contains the necessary steps to achieve this: | |
| 101 | 90 | * |
| 102 | 91 | * <pre> |
| 92 | + * // 1. Instantiate the custom codec | |
| 103 | 93 | * TypeCodec<DateTime> timestampCodec = ... |
| 104 | - * CodecRegistry myCodecRegistry = new CodecRegistry().register(timestampCodec); | |
| 94 | + * // 2. Register the codec | |
| 95 | + * CodecRegistry.DEFAULT_INSTANCE.register(timestampCodec); | |
| 105 | 96 | * </pre> |
| 106 | 97 | * |
| 98 | + * <p> | |
| 107 | 99 | * Read the |
| 108 | 100 | * <a href="http://datastax.github.io/java-driver/features/custom_codecs">online documentation</a> |
| 109 | 101 | * for more examples. |
| @@ -111,59 +103,72 @@ | ||
| 111 | 103 | * <h3>Notes</h3> |
| 112 | 104 | * |
| 113 | 105 | * <p> |
| 114 | - * When a {@link CodecRegistry} cannot find a suitable codec among all registered codecs, | |
| 115 | - * it will attempt to create a suitable codec. | |
| 106 | + * <strong>Registration order</strong>: the order in which codecs are registered matters: | |
| 107 | + * when looking for a suitable codec, the registry will consider | |
| 108 | + * all registered codecs <em>in the order they were registered</em>, and | |
| 109 | + * will pick the first matching one. | |
| 110 | + * | |
| 111 | + * <p> | |
| 112 | + * If a codec is registered while another codec already handles the <em>exact same</em> | |
| 113 | + * CQL-to-Java mapping, that codec will never be used (unless | |
| 114 | + * the user forces its usage through e.g. {@link GettableByIndexData#get(int, TypeCodec)}). | |
| 115 | + * In order words, it is not possible to replace an existing codec, | |
| 116 | + * specially the built-in ones; it is only possible to enrich the initial | |
| 117 | + * set of codecs with new ones. | |
| 118 | + * | |
| 116 | 119 | * <p> |
| 117 | - * If the creation succeeds, that codec is added to the list of known codecs and is returned; | |
| 120 | + * <strong>On-the-fly codec instantiation</strong>: When a {@link CodecRegistry} | |
| 121 | + * cannot find a suitable codec among all registered codecs, | |
| 122 | + * it will attempt to create such a codec on-the-fly. If the creation succeeds, that codec is returned; | |
| 118 | 123 | * otherwise, a {@link CodecNotFoundException} is thrown. |
| 124 | + * | |
| 119 | 125 | * <p> |
| 120 | 126 | * Note that {@link CodecRegistry} instances can only create codecs in very limited situations: |
| 121 | 127 | * <ol> |
| 122 | - * <li>Codecs for Enums are created on the fly using {@link EnumStringCodec};</li> | |
| 123 | - * <li>Codecs for {@link UserType user types} are created on the fly using {@link UDTCodec};</li> | |
| 124 | - * <li>Codecs for {@link TupleType tuple types} are created on the fly using {@link TupleCodec};</li> | |
| 125 | - * <li>Codecs for collections are created on the fly using {@link ListCodec}, {@link SetCodec} and | |
| 126 | - * {@link MapCodec}, if their element types can be handled by existing codecs (or codecs that can | |
| 128 | + * <li>Codecs for {@link UserType user types} are created on the fly using {@link TypeCodec#userType(UserType)};</li> | |
| 129 | + * <li>Codecs for {@link TupleType tuple types} are created on the fly using {@link TypeCodec#tuple(TupleType)}};</li> | |
| 130 | + * <li>Codecs for collections are created on the fly using {@link TypeCodec#list(TypeCodec)}}, {@link TypeCodec#set(TypeCodec)}} and | |
| 131 | + * {@link TypeCodec#map(TypeCodec, TypeCodec)}}, if their element types can be handled by existing codecs (or codecs that can | |
| 127 | 132 | * themselves be generated).</li> |
| 133 | + * <li>Codecs for {@link com.datastax.driver.core.DataType.CustomType custom types} are created on the fly using {@link TypeCodec#custom(DataType.CustomType)};</li> | |
| 128 | 134 | * </ol> |
| 129 | 135 | * Other combinations of Java and CQL types cannot have their codecs created on the fly; |
| 130 | 136 | * such codecs must be manually registered. |
| 131 | 137 | * <p> |
| 132 | - * Note that the default set of codecs has no support for | |
| 133 | - * <a href="https://github.com/apache/cassandra/blob/trunk/src/java/org/apache/cassandra/db/marshal/AbstractType.java">Cassandra custom types</a>; | |
| 134 | - * to be able to deserialize values of such types, you need to manually register an appropriate codec. | |
| 135 | - * <p> | |
| 136 | - * {@link CodecRegistry} instances are not meant to be shared between two or more {@link Cluster} instances; doing so | |
| 137 | - * could yield unexpected results. | |
| 138 | + * | |
| 139 | + * <strong>Debugging the codec registry</strong>: it is possible to turn on log messages | |
| 140 | + * by setting the {@code com.datastax.driver.core.CodecRegistry} logger level to {@code TRACE}. | |
| 141 | + * Beware that the registry can be very verbose at this log level. | |
| 142 | + * | |
| 138 | 143 | * <p> |
| 139 | - * {@link CodecRegistry} instances are thread-safe. | |
| 144 | + * <strong>Thread-safety</strong>: {@link CodecRegistry} instances are thread-safe. | |
| 140 | 145 | * |
| 141 | 146 | */ |
| 142 | -@SuppressWarnings("all") | |
| 143 | 147 | public final class CodecRegistry { |
| 144 | 148 | |
| 145 | 149 | private static final Logger logger = LoggerFactory.getLogger(CodecRegistry.class); |
| 146 | 150 | |
| 151 | + @SuppressWarnings("unchecked") | |
| 147 | 152 | private static final ImmutableSet<TypeCodec<?>> PRIMITIVE_CODECS = ImmutableSet.of( |
| 148 | - BlobCodec.instance, | |
| 149 | - BooleanCodec.instance, | |
| 150 | - SmallIntCodec.instance, | |
| 151 | - TinyIntCodec.instance, | |
| 152 | - IntCodec.instance, | |
| 153 | - BigintCodec.instance, | |
| 154 | - CounterCodec.instance, | |
| 155 | - DoubleCodec.instance, | |
| 156 | - FloatCodec.instance, | |
| 157 | - VarintCodec.instance, | |
| 158 | - DecimalCodec.instance, | |
| 159 | - VarcharCodec.instance, // must be declared before AsciiCodec so it gets chosen when CQL type not available | |
| 160 | - AsciiCodec.instance, | |
| 161 | - TimestampCodec.instance, | |
| 162 | - DateCodec.instance, | |
| 163 | - TimeCodec.instance, | |
| 164 | - UUIDCodec.instance, // must be declared before TimeUUIDCodec so it gets chosen when CQL type not available | |
| 165 | - TimeUUIDCodec.instance, | |
| 166 | - InetCodec.instance | |
| 153 | + TypeCodec.blob(), | |
| 154 | + TypeCodec.cboolean(), | |
| 155 | + TypeCodec.smallInt(), | |
| 156 | + TypeCodec.tinyInt(), | |
| 157 | + TypeCodec.cint(), | |
| 158 | + TypeCodec.bigint(), | |
| 159 | + TypeCodec.counter(), | |
| 160 | + TypeCodec.cdouble(), | |
| 161 | + TypeCodec.cfloat(), | |
| 162 | + TypeCodec.varint(), | |
| 163 | + TypeCodec.decimal(), | |
| 164 | + TypeCodec.varchar(), // must be declared before AsciiCodec so it gets chosen when CQL type not available | |
| 165 | + TypeCodec.ascii(), | |
| 166 | + TypeCodec.timestamp(), | |
| 167 | + TypeCodec.date(), | |
| 168 | + TypeCodec.time(), | |
| 169 | + TypeCodec.uuid(), // must be declared before TimeUUIDCodec so it gets chosen when CQL type not available | |
| 170 | + TypeCodec.timeUUID(), | |
| 171 | + TypeCodec.inet() | |
| 167 | 172 | ); |
| 168 | 173 | |
| 169 | 174 | /** |
| @@ -174,6 +179,9 @@ | ||
| 174 | 179 | */ |
| 175 | 180 | public static final CodecRegistry DEFAULT_INSTANCE = new CodecRegistry(); |
| 176 | 181 | |
| 182 | + /** | |
| 183 | + * Cache key for the codecs cache. | |
| 184 | + */ | |
| 177 | 185 | private static final class CacheKey { |
| 178 | 186 | |
| 179 | 187 | private final DataType cqlType; |
| @@ -197,12 +205,23 @@ | ||
| 197 | 205 | |
| 198 | 206 | @Override |
| 199 | 207 | public int hashCode() { |
| 200 | - return Objects.hashCode(javaType, cqlType); | |
| 208 | + return Objects.hashCode(cqlType, javaType); | |
| 209 | + } | |
| 210 | + | |
| 211 | + } | |
| 212 | + | |
| 213 | + /** | |
| 214 | + * Cache loader for the codecs cache. | |
| 215 | + */ | |
| 216 | + private class TypeCodecCacheLoader extends CacheLoader<CacheKey, TypeCodec<?>> { | |
| 217 | + @Override | |
| 218 | + public TypeCodec<?> load(CacheKey cacheKey) { | |
| 219 | + return findCodec(cacheKey.cqlType, cacheKey.javaType); | |
| 201 | 220 | } |
| 202 | 221 | } |
| 203 | 222 | |
| 204 | 223 | /** |
| 205 | - * A complexity-based weigher. | |
| 224 | + * A complexity-based weigher for the codecs cache. | |
| 206 | 225 | * Weights are computed mainly according to the CQL type: |
| 207 | 226 | * <ol> |
| 208 | 227 | * <li>Manually-registered codecs always weigh 0; |
| @@ -254,6 +273,10 @@ | ||
| 254 | 273 | } |
| 255 | 274 | } |
| 256 | 275 | |
| 276 | + /** | |
| 277 | + * Simple removal listener for the codec cache (can be used for debugging purposes | |
| 278 | + * by setting the {@code com.datastax.driver.core.CodecRegistry} logger level to {@code TRACE}. | |
| 279 | + */ | |
| 257 | 280 | private class TypeCodecRemovalListener implements RemovalListener<CacheKey, TypeCodec<?>> { |
| 258 | 281 | @Override |
| 259 | 282 | public void onRemoval(RemovalNotification<CacheKey, TypeCodec<?>> notification) { |
| @@ -262,7 +285,7 @@ | ||
| 262 | 285 | } |
| 263 | 286 | |
| 264 | 287 | /** |
| 265 | - * The list of all known codecs. | |
| 288 | + * The list of registered codecs. | |
| 266 | 289 | * This list is initialized with the built-in codecs; |
| 267 | 290 | * User-defined codecs are appended to the list. |
| 268 | 291 | */ |
| @@ -279,32 +302,50 @@ | ||
| 279 | 302 | */ |
| 280 | 303 | public CodecRegistry() { |
| 281 | 304 | this.codecs = new CopyOnWriteArrayList<TypeCodec<?>>(PRIMITIVE_CODECS); |
| 282 | - this.cache = CacheBuilder.newBuilder() | |
| 305 | + this.cache = defaultCacheBuilder().build(new TypeCodecCacheLoader()); | |
| 306 | + } | |
| 307 | + | |
| 308 | + private CacheBuilder<CacheKey, TypeCodec<?>> defaultCacheBuilder() { | |
| 309 | + CacheBuilder<CacheKey, TypeCodec<?>> builder = CacheBuilder.newBuilder() | |
| 283 | 310 | // 19 primitive codecs + collections thereof = 19*3 + 19*19 = 418 codecs, |
| 284 | 311 | // so let's start with roughly 1/4 of that |
| 285 | 312 | .initialCapacity(100) |
| 286 | - .weigher(new TypeCodecWeigher()) | |
| 287 | - // a cache with all 418 "basic" codecs weighs 399 (19*2 + 19*19), | |
| 288 | - // so let's cap at roughly 2.5x this size | |
| 289 | 313 | .maximumWeight(1000) |
| 290 | - .concurrencyLevel(Runtime.getRuntime().availableProcessors() * 4) | |
| 291 | - .removalListener(new TypeCodecRemovalListener()) | |
| 292 | - .build( | |
| 293 | - new CacheLoader<CacheKey, TypeCodec<?>>() { | |
| 294 | - public TypeCodec<?> load(CacheKey cacheKey) { | |
| 295 | - return findCodec(cacheKey.cqlType, cacheKey.javaType); | |
| 296 | - } | |
| 297 | - }); | |
| 314 | + .weigher(new TypeCodecWeigher()); | |
| 315 | + if (logger.isTraceEnabled()) | |
| 316 | + // do not bother adding a listener if it will be ineffective | |
| 317 | + builder = builder.removalListener(new TypeCodecRemovalListener()); | |
| 318 | + return builder; | |
| 298 | 319 | } |
| 299 | 320 | |
| 300 | 321 | /** |
| 301 | 322 | * Register the given codec with this registry. |
| 323 | + * <p> | |
| 324 | + * This method will log a warning and ignore the codec if it collides with a previously registered one. | |
| 325 | + * Note that this check is not done in a completely thread-safe manner; codecs should typically be registered | |
| 326 | + * at application startup, not in a highly concurrent context (if a race condition occurs, the worst outcome | |
| 327 | + * is that no warning gets logged, and the codec gets registered but will never actually be used). | |
| 302 | 328 | * |
| 303 | - * @param codec The codec to add to the registry. | |
| 329 | + * @param newCodec The codec to add to the registry. | |
| 304 | 330 | * @return this CodecRegistry (for method chaining). |
| 305 | 331 | */ |
| 306 | - public CodecRegistry register(TypeCodec<?> codec) { | |
| 307 | - return register(Collections.singleton(codec)); | |
| 332 | + public CodecRegistry register(TypeCodec<?> newCodec) { | |
| 333 | + for (TypeCodec<?> oldCodec : codecs) { | |
| 334 | + if (oldCodec.accepts(newCodec.getCqlType()) && oldCodec.accepts(newCodec.getJavaType())) { | |
| 335 | + logger.warn("Ignoring codec {} because it collides with previously registered codec {}", newCodec, oldCodec); | |
| 336 | + return this; | |
| 337 | + } | |
| 338 | + } | |
| 339 | + | |
| 340 | + CacheKey key = new CacheKey(newCodec.getCqlType(), newCodec.getJavaType()); | |
| 341 | + TypeCodec<?> existing = cache.getIfPresent(key); | |
| 342 | + if (existing != null) { | |
| 343 | + logger.warn("Ignoring codec {} because it collides with previously generated codec {}", newCodec, existing); | |
| 344 | + return this; | |
| 345 | + } | |
| 346 | + | |
| 347 | + this.codecs.add(newCodec); | |
| 348 | + return this; | |
| 308 | 349 | } |
| 309 | 350 | |
| 310 | 351 | /** |
| @@ -312,9 +353,13 @@ | ||
| 312 | 353 | * |
| 313 | 354 | * @param codecs The codecs to add to the registry. |
| 314 | 355 | * @return this Builder (for method chaining). |
| 356 | + * | |
| 357 | + * @see #register(TypeCodec) | |
| 315 | 358 | */ |
| 316 | 359 | public CodecRegistry register(TypeCodec<?>... codecs) { |
| 317 | - return register(Arrays.asList(codecs)); | |
| 360 | + for (TypeCodec<?> codec : codecs) | |
| 361 | + register(codec); | |
| 362 | + return this; | |
| 318 | 363 | } |
| 319 | 364 | |
| 320 | 365 | /** |
| @@ -322,11 +367,12 @@ | ||
| 322 | 367 | * |
| 323 | 368 | * @param codecs The codecs to add to the registry. |
| 324 | 369 | * @return this Builder (for method chaining). |
| 370 | + * | |
| 371 | + * @see #register(TypeCodec) | |
| 325 | 372 | */ |
| 326 | 373 | public CodecRegistry register(Iterable<? extends TypeCodec<?>> codecs) { |
| 327 | - for (TypeCodec<?> codec : codecs) { | |
| 328 | - this.codecs.add(codec); | |
| 329 | - } | |
| 374 | + for (TypeCodec<?> codec : codecs) | |
| 375 | + register(codec); | |
| 330 | 376 | return this; |
| 331 | 377 | } |
| 332 | 378 | |
| @@ -355,9 +401,7 @@ | ||
| 355 | 401 | * @throws CodecNotFoundException if a suitable codec cannot be found. |
| 356 | 402 | */ |
| 357 | 403 | public <T> TypeCodec<T> codecFor(T value) { |
| 358 | - checkNotNull(value, "Parameter value cannot be null"); | |
| 359 | - TypeCodec<T> codec = findCodec(null, value); | |
| 360 | - return codec; | |
| 404 | + return findCodec(null, value); | |
| 361 | 405 | } |
| 362 | 406 | |
| 363 | 407 | /** |
| @@ -394,7 +438,7 @@ | ||
| 394 | 438 | * Codecs returned by this method are cached. |
| 395 | 439 | * |
| 396 | 440 | * @param cqlType The {@link DataType CQL type} the codec should accept; must not be {@code null}. |
| 397 | - * @param javaType The Java type the codec should accept; must not be {@code null}. | |
| 441 | + * @param javaType The Java type the codec should accept; can be {@code null}. | |
| 398 | 442 | * @return A suitable codec. |
| 399 | 443 | * @throws CodecNotFoundException if a suitable codec cannot be found. |
| 400 | 444 | */ |
| @@ -416,13 +460,11 @@ | ||
| 416 | 460 | * Codecs returned by this method are cached. |
| 417 | 461 | * |
| 418 | 462 | * @param cqlType The {@link DataType CQL type} the codec should accept; must not be {@code null}. |
| 419 | - * @param javaType The {@link TypeToken Java type} the codec should accept; must not be {@code null}. | |
| 463 | + * @param javaType The {@link TypeToken Java type} the codec should accept; can be {@code null}. | |
| 420 | 464 | * @return A suitable codec. |
| 421 | 465 | * @throws CodecNotFoundException if a suitable codec cannot be found. |
| 422 | 466 | */ |
| 423 | 467 | public <T> TypeCodec<T> codecFor(DataType cqlType, TypeToken<T> javaType) throws CodecNotFoundException { |
| 424 | - checkNotNull(cqlType, "Parameter cqlType cannot be null"); | |
| 425 | - checkNotNull(javaType, "Parameter javaType cannot be null"); | |
| 426 | 468 | return lookupCodec(cqlType, javaType); |
| 427 | 469 | } |
| 428 | 470 | |
| @@ -438,24 +480,23 @@ | ||
| 438 | 480 | * <p> |
| 439 | 481 | * Codecs returned by this method are <em>NOT</em> cached. |
| 440 | 482 | * |
| 441 | - * @param cqlType The {@link DataType CQL type} the codec should accept; must not be {@code null}. | |
| 483 | + * @param cqlType The {@link DataType CQL type} the codec should accept; can be {@code null}. | |
| 442 | 484 | * @param value The value the codec should accept; must not be {@code null}. |
| 443 | 485 | * @return A suitable codec. |
| 444 | 486 | * @throws CodecNotFoundException if a suitable codec cannot be found. |
| 445 | 487 | */ |
| 446 | 488 | public <T> TypeCodec<T> codecFor(DataType cqlType, T value) { |
| 447 | - checkNotNull(cqlType, "Parameter cqlType cannot be null"); | |
| 448 | - checkNotNull(value, "Parameter value cannot be null"); | |
| 449 | 489 | return findCodec(cqlType, value); |
| 450 | 490 | } |
| 451 | 491 | |
| 492 | + @SuppressWarnings("unchecked") | |
| 452 | 493 | private <T> TypeCodec<T> lookupCodec(DataType cqlType, TypeToken<T> javaType) { |
| 453 | 494 | checkNotNull(cqlType, "Parameter cqlType cannot be null"); |
| 454 | - logger.trace("Querying cache for codec [{} <-> {}]", cqlType, javaType); | |
| 495 | + logger.trace("Querying cache for codec [{} <-> {}]", cqlType, javaType == null ? "ANY" : javaType); | |
| 455 | 496 | CacheKey cacheKey = new CacheKey(cqlType, javaType); |
| 456 | 497 | try { |
| 457 | 498 | TypeCodec<?> codec = cache.get(cacheKey); |
| 458 | - logger.trace("Returning cached codec [{} <-> {}]", cqlType, javaType); | |
| 499 | + logger.trace("Returning cached codec {}", codec); | |
| 459 | 500 | return (TypeCodec<T>)codec; |
| 460 | 501 | } catch (UncheckedExecutionException e) { |
| 461 | 502 | if(e.getCause() instanceof CodecNotFoundException) { |
| @@ -467,13 +508,12 @@ | ||
| 467 | 508 | } |
| 468 | 509 | } |
| 469 | 510 | |
| 511 | + @SuppressWarnings("unchecked") | |
| 470 | 512 | private <T> TypeCodec<T> findCodec(DataType cqlType, TypeToken<T> javaType) { |
| 471 | 513 | checkNotNull(cqlType, "Parameter cqlType cannot be null"); |
| 472 | - logger.trace("Looking for codec [{} <-> {}]", | |
| 473 | - cqlType == null ? "ANY" : cqlType, | |
| 474 | - javaType == null ? "ANY" : javaType); | |
| 514 | + logger.trace("Looking for codec [{} <-> {}]", cqlType, javaType == null ? "ANY" : javaType); | |
| 475 | 515 | for (TypeCodec<?> codec : codecs) { |
| 476 | - if ((cqlType == null || codec.accepts(cqlType)) && (javaType == null || codec.accepts(javaType))) { | |
| 516 | + if (codec.accepts(cqlType) && (javaType == null || codec.accepts(javaType))) { | |
| 477 | 517 | logger.trace("Codec found: {}", codec); |
| 478 | 518 | return (TypeCodec<T>)codec; |
| 479 | 519 | } |
| @@ -481,6 +521,7 @@ | ||
| 481 | 521 | return createCodec(cqlType, javaType); |
| 482 | 522 | } |
| 483 | 523 | |
| 524 | + @SuppressWarnings("unchecked") | |
| 484 | 525 | private <T> TypeCodec<T> findCodec(DataType cqlType, T value) { |
| 485 | 526 | checkNotNull(value, "Parameter value cannot be null"); |
| 486 | 527 | logger.trace("Looking for codec [{} <-> {}]", cqlType == null ? "ANY" : cqlType, value.getClass()); |
| @@ -496,10 +537,10 @@ | ||
| 496 | 537 | private <T> TypeCodec<T> createCodec(DataType cqlType, TypeToken<T> javaType) { |
| 497 | 538 | TypeCodec<T> codec = maybeCreateCodec(cqlType, javaType); |
| 498 | 539 | if (codec == null) |
| 499 | - throw newException(cqlType, javaType); | |
| 540 | + throw notFound(cqlType, javaType); | |
| 500 | 541 | // double-check that the created codec satisfies the initial request |
| 501 | 542 | if (!codec.accepts(cqlType) || (javaType != null && !codec.accepts(javaType))) |
| 502 | - throw newException(cqlType, javaType); | |
| 543 | + throw notFound(cqlType, javaType); | |
| 503 | 544 | logger.trace("Codec created: {}", codec); |
| 504 | 545 | return codec; |
| 505 | 546 | } |
| @@ -507,21 +548,18 @@ | ||
| 507 | 548 | private <T> TypeCodec<T> createCodec(DataType cqlType, T value) { |
| 508 | 549 | TypeCodec<T> codec = maybeCreateCodec(cqlType, value); |
| 509 | 550 | if(codec == null) |
| 510 | - throw newException(cqlType, TypeToken.of(value.getClass())); | |
| 551 | + throw notFound(cqlType, TypeToken.of(value.getClass())); | |
| 511 | 552 | // double-check that the created codec satisfies the initial request |
| 512 | 553 | if((cqlType != null && !codec.accepts(cqlType)) || !codec.accepts(value)) |
| 513 | - throw newException(cqlType, TypeToken.of(value.getClass())); | |
| 554 | + throw notFound(cqlType, TypeToken.of(value.getClass())); | |
| 514 | 555 | logger.trace("Codec created: {}", codec); |
| 515 | 556 | return codec; |
| 516 | 557 | } |
| 517 | 558 | |
| 559 | + @SuppressWarnings("unchecked") | |
| 518 | 560 | private <T> TypeCodec<T> maybeCreateCodec(DataType cqlType, TypeToken<T> javaType) { |
| 519 | 561 | checkNotNull(cqlType); |
| 520 | 562 | |
| 521 | - if ((cqlType.getName() == VARCHAR || cqlType.getName() == TEXT) && javaType != null && Enum.class.isAssignableFrom(javaType.getRawType())) { | |
| 522 | - return new EnumStringCodec(javaType.getRawType()); | |
| 523 | - } | |
| 524 | - | |
| 525 | 563 | if (cqlType.getName() == LIST && (javaType == null || List.class.isAssignableFrom(javaType.getRawType()))) { |
| 526 | 564 | TypeToken<?> elementType = null; |
| 527 | 565 | if (javaType != null && javaType.getType() instanceof ParameterizedType) { |
| @@ -529,7 +567,7 @@ | ||
| 529 | 567 | elementType = TypeToken.of(typeArguments[0]); |
| 530 | 568 | } |
| 531 | 569 | TypeCodec<?> eltCodec = findCodec(cqlType.getTypeArguments().get(0), elementType); |
| 532 | - return new ListCodec(eltCodec); | |
| 570 | + return (TypeCodec<T>)TypeCodec.list(eltCodec); | |
| 533 | 571 | } |
| 534 | 572 | |
| 535 | 573 | if (cqlType.getName() == SET && (javaType == null || Set.class.isAssignableFrom(javaType.getRawType()))) { |
| @@ -539,7 +577,7 @@ | ||
| 539 | 577 | elementType = TypeToken.of(typeArguments[0]); |
| 540 | 578 | } |
| 541 | 579 | TypeCodec<?> eltCodec = findCodec(cqlType.getTypeArguments().get(0), elementType); |
| 542 | - return new SetCodec(eltCodec); | |
| 580 | + return (TypeCodec<T>)TypeCodec.set(eltCodec); | |
| 543 | 581 | } |
| 544 | 582 | |
| 545 | 583 | if (cqlType.getName() == MAP && (javaType == null || Map.class.isAssignableFrom(javaType.getRawType()))) { |
| @@ -552,39 +590,40 @@ | ||
| 552 | 590 | } |
| 553 | 591 | TypeCodec<?> keyCodec = findCodec(cqlType.getTypeArguments().get(0), keyType); |
| 554 | 592 | TypeCodec<?> valueCodec = findCodec(cqlType.getTypeArguments().get(1), valueType); |
| 555 | - return new MapCodec(keyCodec, valueCodec); | |
| 593 | + return (TypeCodec<T>)TypeCodec.map(keyCodec, valueCodec); | |
| 556 | 594 | } |
| 557 | 595 | |
| 558 | 596 | if (cqlType instanceof TupleType && (javaType == null || TupleValue.class.isAssignableFrom(javaType.getRawType()))) { |
| 559 | - return (TypeCodec<T>)new TupleCodec((TupleType)cqlType); | |
| 597 | + return (TypeCodec<T>)TypeCodec.tuple((TupleType)cqlType); | |
| 560 | 598 | } |
| 561 | 599 | |
| 562 | 600 | if (cqlType instanceof UserType && (javaType == null || UDTValue.class.isAssignableFrom(javaType.getRawType()))) { |
| 563 | - return (TypeCodec<T>)new UDTCodec((UserType)cqlType); | |
| 601 | + return (TypeCodec<T>)TypeCodec.userType((UserType)cqlType); | |
| 602 | + } | |
| 603 | + | |
| 604 | + if(cqlType instanceof DataType.CustomType && (javaType == null || ByteBuffer.class.isAssignableFrom(javaType.getRawType()))) { | |
| 605 | + return (TypeCodec<T>)TypeCodec.custom((DataType.CustomType)cqlType); | |
| 564 | 606 | } |
| 565 | 607 | |
| 566 | 608 | return null; |
| 567 | 609 | } |
| 568 | 610 | |
| 611 | + @SuppressWarnings("unchecked") | |
| 569 | 612 | private <T> TypeCodec<T> maybeCreateCodec(DataType cqlType, T value) { |
| 570 | 613 | checkNotNull(value); |
| 571 | 614 | |
| 572 | - if ((cqlType == null || cqlType.getName() == VARCHAR || cqlType.getName() == TEXT) && value instanceof Enum) { | |
| 573 | - return new EnumStringCodec(value.getClass()); | |
| 574 | - } | |
| 575 | - | |
| 576 | 615 | if ((cqlType == null || cqlType.getName() == LIST) && value instanceof List) { |
| 577 | 616 | List list = (List)value; |
| 578 | 617 | if (list.isEmpty()) { |
| 579 | 618 | DataType elementType = (cqlType == null || cqlType.getTypeArguments().isEmpty()) |
| 580 | 619 | ? DataType.blob() |
| 581 | 620 | : cqlType.getTypeArguments().get(0); |
| 582 | - return new ListCodec(findCodec(elementType, (TypeToken)null)); | |
| 621 | + return TypeCodec.list(findCodec(elementType, (TypeToken)null)); | |
| 583 | 622 | } else { |
| 584 | 623 | DataType elementType = (cqlType == null || cqlType.getTypeArguments().isEmpty()) |
| 585 | 624 | ? null |
| 586 | 625 | : cqlType.getTypeArguments().get(0); |
| 587 | - return new ListCodec(findCodec(elementType, list.iterator().next())); | |
| 626 | + return (TypeCodec<T>)TypeCodec.list(findCodec(elementType, list.iterator().next())); | |
| 588 | 627 | } |
| 589 | 628 | } |
| 590 | 629 | |
| @@ -594,12 +633,12 @@ | ||
| 594 | 633 | DataType elementType = (cqlType == null || cqlType.getTypeArguments().isEmpty()) |
| 595 | 634 | ? DataType.blob() |
| 596 | 635 | : cqlType.getTypeArguments().get(0); |
| 597 | - return new SetCodec(findCodec(elementType, (TypeToken)null)); | |
| 636 | + return TypeCodec.set(findCodec(elementType, (TypeToken)null)); | |
| 598 | 637 | } else { |
| 599 | 638 | DataType elementType = (cqlType == null || cqlType.getTypeArguments().isEmpty()) |
| 600 | 639 | ? null |
| 601 | 640 | : cqlType.getTypeArguments().get(0); |
| 602 | - return new SetCodec(findCodec(elementType, set.iterator().next())); | |
| 641 | + return (TypeCodec<T>)TypeCodec.set(findCodec(elementType, set.iterator().next())); | |
| 603 | 642 | } |
| 604 | 643 | } |
| 605 | 644 | |
| @@ -612,7 +651,7 @@ | ||
| 612 | 651 | DataType valueType = (cqlType == null || cqlType.getTypeArguments().size() < 2) |
| 613 | 652 | ? DataType.blob() : |
| 614 | 653 | cqlType.getTypeArguments().get(1); |
| 615 | - return new MapCodec( | |
| 654 | + return TypeCodec.map( | |
| 616 | 655 | findCodec(keyType, (TypeToken)null), |
| 617 | 656 | findCodec(valueType, (TypeToken)null)); |
| 618 | 657 | } else { |
| @@ -623,24 +662,28 @@ | ||
| 623 | 662 | ? null |
| 624 | 663 | : cqlType.getTypeArguments().get(1); |
| 625 | 664 | Map.Entry entry = (Map.Entry)map.entrySet().iterator().next(); |
| 626 | - return new MapCodec( | |
| 665 | + return (TypeCodec<T>)TypeCodec.map( | |
| 627 | 666 | findCodec(keyType, entry.getKey()), |
| 628 | 667 | findCodec(valueType, entry.getValue())); |
| 629 | 668 | } |
| 630 | 669 | } |
| 631 | 670 | |
| 632 | 671 | if ((cqlType == null || cqlType.getName() == DataType.Name.TUPLE) && value instanceof TupleValue) { |
| 633 | - return (TypeCodec<T>)new TupleCodec(cqlType == null ? ((TupleValue)value).getType() : (TupleType) cqlType); | |
| 672 | + return (TypeCodec<T>)TypeCodec.tuple(cqlType == null ? ((TupleValue)value).getType() : (TupleType)cqlType); | |
| 634 | 673 | } |
| 635 | 674 | |
| 636 | 675 | if ((cqlType == null || cqlType.getName() == DataType.Name.UDT) && value instanceof UDTValue) { |
| 637 | - return (TypeCodec<T>)new UDTCodec(cqlType == null ? ((UDTValue)value).getType() : (UserType) cqlType); | |
| 676 | + return (TypeCodec<T>)TypeCodec.userType(cqlType == null ? ((UDTValue)value).getType() : (UserType)cqlType); | |
| 677 | + } | |
| 678 | + | |
| 679 | + if((cqlType != null && cqlType instanceof DataType.CustomType) && value instanceof ByteBuffer) { | |
| 680 | + return (TypeCodec<T>)TypeCodec.custom((DataType.CustomType)cqlType); | |
| 638 | 681 | } |
| 639 | 682 | |
| 640 | 683 | return null; |
| 641 | 684 | } |
| 642 | 685 | |
| 643 | - private static CodecNotFoundException newException(DataType cqlType, TypeToken<?> javaType) { | |
| 686 | + private static CodecNotFoundException notFound(DataType cqlType, TypeToken<?> javaType) { | |
| 644 | 687 | String msg = String.format("Codec not found for requested operation: [%s <-> %s]", |
| 645 | 688 | cqlType == null ? "ANY" : cqlType, |
| 646 | 689 | javaType == null ? "ANY" : javaType); |
modified driver-core/src/main/java/com/datastax/driver/core/ControlConnection.java +1 −1
| @@ -32,7 +32,7 @@ | ||
| 32 | 32 | import com.datastax.driver.core.exceptions.DriverInternalError; |
| 33 | 33 | import com.datastax.driver.core.exceptions.NoHostAvailableException; |
| 34 | 34 | |
| 35 | -import static com.datastax.driver.core.SchemaElement.KEYSPACE; | |
| 35 | +import static com.datastax.driver.core.SchemaElement.*; | |
| 36 | 36 | |
| 37 | 37 | class ControlConnection implements Connection.Owner { |
| 38 | 38 |
modified driver-core/src/main/java/com/datastax/driver/core/DataType.java +4 −2
| @@ -47,6 +47,7 @@ | ||
| 47 | 47 | INET (16), |
| 48 | 48 | INT (9), |
| 49 | 49 | TEXT (10){ |
| 50 | + @Override | |
| 50 | 51 | public boolean isCompatibleWith(Name that) { |
| 51 | 52 | return this == that || that == VARCHAR; |
| 52 | 53 | } |
| @@ -54,6 +55,7 @@ | ||
| 54 | 55 | TIMESTAMP (11), |
| 55 | 56 | UUID (12), |
| 56 | 57 | VARCHAR (13){ |
| 58 | + @Override | |
| 57 | 59 | public boolean isCompatibleWith(Name that) { |
| 58 | 60 | return this == that || that == TEXT; |
| 59 | 61 | } |
| @@ -637,7 +639,7 @@ | ||
| 637 | 639 | |
| 638 | 640 | @Override |
| 639 | 641 | public final int hashCode() { |
| 640 | - return Arrays.hashCode(new Object[]{ name, typeArguments }); | |
| 642 | + return Objects.hashCode(name, typeArguments); | |
| 641 | 643 | } |
| 642 | 644 | |
| 643 | 645 | @Override |
| @@ -682,7 +684,7 @@ | ||
| 682 | 684 | |
| 683 | 685 | @Override |
| 684 | 686 | public final int hashCode() { |
| 685 | - return Arrays.hashCode(new Object[]{ name, customClassName }); | |
| 687 | + return Objects.hashCode(name, customClassName); | |
| 686 | 688 | } |
| 687 | 689 | |
| 688 | 690 | @Override |
modified driver-core/src/main/java/com/datastax/driver/core/GettableByIndexData.java +81 −67
| @@ -49,7 +49,6 @@ | ||
| 49 | 49 | * value is NULL, {@code false} is returned. |
| 50 | 50 | * |
| 51 | 51 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 52 | - * @throws InvalidTypeException if value {@code i} is not of type BOOLEAN. | |
| 53 | 52 | */ |
| 54 | 53 | public boolean getBool(int i); |
| 55 | 54 | |
| @@ -61,7 +60,6 @@ | ||
| 61 | 60 | * value is NULL, {@code 0} is returned. |
| 62 | 61 | * |
| 63 | 62 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 64 | - * @throws InvalidTypeException if value {@code i} is not of type TINYINT. | |
| 65 | 63 | */ |
| 66 | 64 | public byte getByte(int i); |
| 67 | 65 | |
| @@ -73,7 +71,6 @@ | ||
| 73 | 71 | * value is NULL, {@code 0} is returned. |
| 74 | 72 | * |
| 75 | 73 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 76 | - * @throws InvalidTypeException if value {@code i} is not of type SMALLINT. | |
| 77 | 74 | */ |
| 78 | 75 | public short getShort(int i); |
| 79 | 76 | |
| @@ -85,7 +82,6 @@ | ||
| 85 | 82 | * value is NULL, {@code 0} is returned. |
| 86 | 83 | * |
| 87 | 84 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 88 | - * @throws InvalidTypeException if value {@code i} is not of type INT. | |
| 89 | 85 | */ |
| 90 | 86 | public int getInt(int i); |
| 91 | 87 | |
| @@ -97,7 +93,6 @@ | ||
| 97 | 93 | * value is NULL, {@code 0L} is returned. |
| 98 | 94 | * |
| 99 | 95 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 100 | - * @throws InvalidTypeException if value {@code i} is not of type BIGINT or COUNTER. | |
| 101 | 96 | */ |
| 102 | 97 | public long getLong(int i); |
| 103 | 98 | |
| @@ -109,7 +104,6 @@ | ||
| 109 | 104 | * value is NULL, {@code null} is returned. |
| 110 | 105 | * |
| 111 | 106 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 112 | - * @throws InvalidTypeException if value {@code i} is not of type TIMESTAMP. | |
| 113 | 107 | */ |
| 114 | 108 | public Date getTimestamp(int i); |
| 115 | 109 | |
| @@ -121,7 +115,6 @@ | ||
| 121 | 115 | * value is NULL, {@code null} is returned. |
| 122 | 116 | * |
| 123 | 117 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 124 | - * @throws InvalidTypeException if value {@code i} is not of type DATE. | |
| 125 | 118 | */ |
| 126 | 119 | public LocalDate getDate(int i); |
| 127 | 120 | |
| @@ -133,7 +126,6 @@ | ||
| 133 | 126 | * value is NULL, {@code 0L} is returned. |
| 134 | 127 | * |
| 135 | 128 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 136 | - * @throws InvalidTypeException if value {@code i} is not of type TIME. | |
| 137 | 129 | */ |
| 138 | 130 | public long getTime(int i); |
| 139 | 131 | |
| @@ -145,7 +137,6 @@ | ||
| 145 | 137 | * value is NULL, {@code 0.0f} is returned. |
| 146 | 138 | * |
| 147 | 139 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 148 | - * @throws InvalidTypeException if value {@code i} is not of type FLOAT. | |
| 149 | 140 | */ |
| 150 | 141 | public float getFloat(int i); |
| 151 | 142 | |
| @@ -157,7 +148,6 @@ | ||
| 157 | 148 | * value is NULL, {@code 0.0} is returned. |
| 158 | 149 | * |
| 159 | 150 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 160 | - * @throws InvalidTypeException if value {@code i} is not of type DOUBLE. | |
| 161 | 151 | */ |
| 162 | 152 | public double getDouble(int i); |
| 163 | 153 | |
| @@ -188,7 +178,6 @@ | ||
| 188 | 178 | * value is NULL, {@code null} is returned. |
| 189 | 179 | * |
| 190 | 180 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 191 | - * @throws InvalidTypeException if value {@code i} type is not of type BLOB. | |
| 192 | 181 | */ |
| 193 | 182 | public ByteBuffer getBytes(int i); |
| 194 | 183 | |
| @@ -200,8 +189,6 @@ | ||
| 200 | 189 | * value is NULL, {@code null} is returned. |
| 201 | 190 | * |
| 202 | 191 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 203 | - * @throws InvalidTypeException if value {@code i} type is none of: | |
| 204 | - * VARCHAR, TEXT or ASCII. | |
| 205 | 192 | */ |
| 206 | 193 | public String getString(int i); |
| 207 | 194 | |
| @@ -213,7 +200,6 @@ | ||
| 213 | 200 | * length integer. If the value is NULL, {@code null} is returned. |
| 214 | 201 | * |
| 215 | 202 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 216 | - * @throws InvalidTypeException if value {@code i} is not of type VARINT. | |
| 217 | 203 | */ |
| 218 | 204 | public BigInteger getVarint(int i); |
| 219 | 205 | |
| @@ -225,7 +211,6 @@ | ||
| 225 | 211 | * length decimal. If the value is NULL, {@code null} is returned. |
| 226 | 212 | * |
| 227 | 213 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 228 | - * @throws InvalidTypeException if value {@code i} is not of type DECIMAL. | |
| 229 | 214 | */ |
| 230 | 215 | public BigDecimal getDecimal(int i); |
| 231 | 216 | |
| @@ -237,8 +222,6 @@ | ||
| 237 | 222 | * If the value is NULL, {@code null} is returned. |
| 238 | 223 | * |
| 239 | 224 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 240 | - * @throws InvalidTypeException if value {@code i} is not of type UUID | |
| 241 | - * or TIMEUUID. | |
| 242 | 225 | */ |
| 243 | 226 | public UUID getUUID(int i); |
| 244 | 227 | |
| @@ -250,7 +233,6 @@ | ||
| 250 | 233 | * If the value is NULL, {@code null} is returned. |
| 251 | 234 | * |
| 252 | 235 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 253 | - * @throws InvalidTypeException if value {@code i} is not of type INET. | |
| 254 | 236 | */ |
| 255 | 237 | public InetAddress getInet(int i); |
| 256 | 238 | |
| @@ -258,17 +240,21 @@ | ||
| 258 | 240 | * Returns the {@code i}th value as a list. |
| 259 | 241 | * <p> |
| 260 | 242 | * If the type of the elements is generic, use {@link #getList(int, TypeToken)}. |
| 243 | + * <p> | |
| 244 | + * Implementation note: the actual {@link List} implementation will depend | |
| 245 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 246 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 247 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 248 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 249 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 250 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 261 | 251 | * |
| 262 | 252 | * @param i the index ({@code 0 <= i < size()}) to retrieve. |
| 263 | 253 | * @param elementsClass the class for the elements of the list to retrieve. |
| 264 | 254 | * @return the value of the {@code i}th element as a list of |
| 265 | - * {@code T} objects. If the value is NULL, an empty list is | |
| 266 | - * returned (note that Cassandra makes no difference between an empty list | |
| 267 | - * and column of type list that is not set). The returned list is immutable. | |
| 255 | + * {@code T} objects. | |
| 268 | 256 | * |
| 269 | 257 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 270 | - * @throws InvalidTypeException if value {@code i} is not a list or if its | |
| 271 | - * elements are not of class {@code T}. | |
| 272 | 258 | */ |
| 273 | 259 | public <T> List<T> getList(int i, Class<T> elementsClass); |
| 274 | 260 | |
| @@ -279,17 +265,21 @@ | ||
| 279 | 265 | * <pre> |
| 280 | 266 | * {@code List<List<String>> l = row.getList(1, new TypeToken<List<String>>() {});} |
| 281 | 267 | * </pre> |
| 268 | + * <p> | |
| 269 | + * Implementation note: the actual {@link List} implementation will depend | |
| 270 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 271 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 272 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 273 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 274 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 275 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 282 | 276 | * |
| 283 | 277 | * @param i the index ({@code 0 <= i < size()}) to retrieve. |
| 284 | 278 | * @param elementsType the type of the elements of the list to retrieve. |
| 285 | 279 | * @return the value of the {@code i}th element as a list of |
| 286 | - * {@code T} objects. If the value is NULL, an empty list is | |
| 287 | - * returned (note that Cassandra makes no difference between an empty list | |
| 288 | - * and column of type list that is not set). The returned list is immutable. | |
| 280 | + * {@code T} objects. | |
| 289 | 281 | * |
| 290 | 282 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 291 | - * @throws InvalidTypeException if value {@code i} is not a list or if its | |
| 292 | - * elements are not of class {@code T}. | |
| 293 | 283 | */ |
| 294 | 284 | public <T> List<T> getList(int i, TypeToken<T> elementsType); |
| 295 | 285 | |
| @@ -297,17 +287,21 @@ | ||
| 297 | 287 | * Returns the {@code i}th value as a set. |
| 298 | 288 | * <p> |
| 299 | 289 | * If the type of the elements is generic, use {@link #getSet(int, TypeToken)}. |
| 290 | + * <p> | |
| 291 | + * Implementation note: the actual {@link Set} implementation will depend | |
| 292 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 293 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 294 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 295 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 296 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 297 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 300 | 298 | * |
| 301 | 299 | * @param i the index ({@code 0 <= i < size()}) to retrieve. |
| 302 | 300 | * @param elementsClass the class for the elements of the set to retrieve. |
| 303 | 301 | * @return the value of the {@code i}th element as a set of |
| 304 | - * {@code T} objects. If the value is NULL, an empty set is | |
| 305 | - * returned (note that Cassandra makes no difference between an empty set | |
| 306 | - * and column of type set that is not set). The returned set is immutable. | |
| 302 | + * {@code T} objects. | |
| 307 | 303 | * |
| 308 | 304 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 309 | - * @throws InvalidTypeException if value {@code i} is not a set or if its | |
| 310 | - * elements are not of class {@code T}. | |
| 311 | 305 | */ |
| 312 | 306 | public <T> Set<T> getSet(int i, Class<T> elementsClass); |
| 313 | 307 | |
| @@ -318,17 +312,21 @@ | ||
| 318 | 312 | * <pre> |
| 319 | 313 | * {@code Set<List<String>> l = row.getSet(1, new TypeToken<List<String>>() {});} |
| 320 | 314 | * </pre> |
| 315 | + * <p> | |
| 316 | + * Implementation note: the actual {@link Set} implementation will depend | |
| 317 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 318 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 319 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 320 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 321 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 322 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 321 | 323 | * |
| 322 | 324 | * @param i the index ({@code 0 <= i < size()}) to retrieve. |
| 323 | 325 | * @param elementsType the type for the elements of the set to retrieve. |
| 324 | 326 | * @return the value of the {@code i}th element as a set of |
| 325 | - * {@code T} objects. If the value is NULL, an empty set is | |
| 326 | - * returned (note that Cassandra makes no difference between an empty set | |
| 327 | - * and column of type set that is not set). The returned set is immutable. | |
| 327 | + * {@code T} objects. | |
| 328 | 328 | * |
| 329 | 329 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 330 | - * @throws InvalidTypeException if value {@code i} is not a set or if its | |
| 331 | - * elements are not of class {@code T}. | |
| 332 | 330 | */ |
| 333 | 331 | public <T> Set<T> getSet(int i, TypeToken<T> elementsType); |
| 334 | 332 | |
| @@ -336,20 +334,22 @@ | ||
| 336 | 334 | * Returns the {@code i}th value as a map. |
| 337 | 335 | * <p> |
| 338 | 336 | * If the type of the keys and/or values is generic, use {@link #getMap(int, TypeToken, TypeToken)}. |
| 337 | + * <p> | |
| 338 | + * Implementation note: the actual {@link Map} implementation will depend | |
| 339 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 340 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 341 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 342 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 343 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 344 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 339 | 345 | * |
| 340 | 346 | * @param i the index ({@code 0 <= i < size()}) to retrieve. |
| 341 | 347 | * @param keysClass the class for the keys of the map to retrieve. |
| 342 | 348 | * @param valuesClass the class for the values of the map to retrieve. |
| 343 | 349 | * @return the value of the {@code i}th element as a map of |
| 344 | - * {@code K} to {@code V} objects. If the value is NULL, | |
| 345 | - * an empty map is returned (note that Cassandra makes no difference | |
| 346 | - * between an empty map and column of type map that is not set). The | |
| 347 | - * returned map is immutable. | |
| 350 | + * {@code K} to {@code V} objects. | |
| 348 | 351 | * |
| 349 | 352 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 350 | - * @throws InvalidTypeException if value {@code i} is not a map, if its | |
| 351 | - * keys are not of class {@code K} or if its values are not of | |
| 352 | - * class {@code V}. | |
| 353 | 353 | */ |
| 354 | 354 | public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass); |
| 355 | 355 | |
| @@ -361,20 +361,22 @@ | ||
| 361 | 361 | * <pre> |
| 362 | 362 | * {@code Map<Int, List<String>> l = row.getMap(1, TypeToken.of(Integer.class), new TypeToken<List<String>>() {});} |
| 363 | 363 | * </pre> |
| 364 | + * <p> | |
| 365 | + * Implementation note: the actual {@link Map} implementation will depend | |
| 366 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 367 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 368 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 369 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 370 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 371 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 364 | 372 | * |
| 365 | 373 | * @param i the index ({@code 0 <= i < size()}) to retrieve. |
| 366 | 374 | * @param keysType the type for the keys of the map to retrieve. |
| 367 | 375 | * @param valuesType the type for the values of the map to retrieve. |
| 368 | 376 | * @return the value of the {@code i}th element as a map of |
| 369 | - * {@code K} to {@code V} objects. If the value is NULL, | |
| 370 | - * an empty map is returned (note that Cassandra makes no difference | |
| 371 | - * between an empty map and column of type map that is not set). The | |
| 372 | - * returned map is immutable. | |
| 377 | + * {@code K} to {@code V} objects. | |
| 373 | 378 | * |
| 374 | 379 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 375 | - * @throws InvalidTypeException if value {@code i} is not a map, if its | |
| 376 | - * keys are not of class {@code K} or if its values are not of | |
| 377 | - * class {@code V}. | |
| 378 | 380 | */ |
| 379 | 381 | public <K, V> Map<K, V> getMap(int i, TypeToken<K> keysType, TypeToken<V> valuesType); |
| 380 | 382 | |
| @@ -386,7 +388,6 @@ | ||
| 386 | 388 | * then {@code null} will be returned. |
| 387 | 389 | * |
| 388 | 390 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 389 | - * @throws InvalidTypeException if value {@code i} is not a UDT value. | |
| 390 | 391 | */ |
| 391 | 392 | public UDTValue getUDTValue(int i); |
| 392 | 393 | |
| @@ -398,25 +399,21 @@ | ||
| 398 | 399 | * then {@code null} will be returned. |
| 399 | 400 | * |
| 400 | 401 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 401 | - * @throws InvalidTypeException if value {@code i} is not a tuple value. | |
| 402 | 402 | */ |
| 403 | 403 | public TupleValue getTupleValue(int i); |
| 404 | 404 | |
| 405 | 405 | /** |
| 406 | 406 | * Returns the {@code i}th value as the Java type matching its CQL type. |
| 407 | 407 | * <p> |
| 408 | - * This method uses the default codec for the underlying CQL type | |
| 409 | - * to perform deserialization, and is safe to be used | |
| 410 | - * <em>as long as only default codecs are in use</em>. | |
| 411 | - * If a second, custom codec for the same CQL type is registered, which one will | |
| 412 | - * be used is unspecified; in such cases, it is preferable to use | |
| 413 | - * the more deterministic methods {@link #get(int, Class)} or {@link #get(int, TypeToken)} instead. | |
| 408 | + * Note: if two or more codecs are available | |
| 409 | + * for the underlying CQL type, <em>the one that will be used will be | |
| 410 | + * the first one to be registered.</em>. | |
| 411 | + * <p> | |
| 412 | + * For these reasons, it is generally preferable to use the more | |
| 413 | + * deterministic methods {@link #get(int, Class)} or {@link #get(int, TypeToken)} instead. | |
| 414 | 414 | * |
| 415 | 415 | * @param i the index to retrieve. |
| 416 | 416 | * @return the value of the {@code i}th value as the Java type matching its CQL type. |
| 417 | - * If the value is NULL and is a simple type, UDT or tuple, {@code null} is returned. | |
| 418 | - * If it is NULL and is a collection type, an empty (immutable) collection is returned. | |
| 419 | - * | |
| 420 | 417 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 421 | 418 | */ |
| 422 | 419 | public Object getObject(int i); |
| @@ -431,12 +428,17 @@ | ||
| 431 | 428 | * where more than one codec is registered for the same CQL type; specifying the Java class |
| 432 | 429 | * allows the {@link CodecRegistry} to narrow down the search and return only an exactly-matching codec (if any), |
| 433 | 430 | * thus avoiding any risk of ambiguity. |
| 431 | + * <p> | |
| 432 | + * Implementation note: the actual object returned by this method will depend | |
| 433 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 434 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 435 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 436 | + * codec-dependent; by default, a CQL {@code NULL} value translates to {@code null} for | |
| 437 | + * simple CQL types, UDTs and tuples, and to empty collections for all CQL collection types. | |
| 434 | 438 | * |
| 435 | 439 | * @param i the index to retrieve. |
| 436 | 440 | * @param targetClass The Java type the value should be converted to. |
| 437 | 441 | * @return the value of the {@code i}th value converted to the given Java type. |
| 438 | - * If the CQL value is {@code NULL}, this method will return {@code null} | |
| 439 | - * for a simple type, UDT or tuple, and an empty (immutable) collection for collection types. | |
| 440 | 442 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 441 | 443 | * @throws com.datastax.driver.core.exceptions.CodecNotFoundException |
| 442 | 444 | * if no {@link TypeCodec} instance for {@code targetClass} could be found |
| @@ -454,12 +456,17 @@ | ||
| 454 | 456 | * where more than one codec is registered for the same CQL type; specifying the Java class |
| 455 | 457 | * allows the {@link CodecRegistry} to narrow down the search and return only an exactly-matching codec (if any), |
| 456 | 458 | * thus avoiding any risk of ambiguity. |
| 459 | + * <p> | |
| 460 | + * Implementation note: the actual object returned by this method will depend | |
| 461 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 462 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 463 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 464 | + * codec-dependent; by default, a CQL {@code NULL} value translates to {@code null} for | |
| 465 | + * simple CQL types, UDTs and tuples, and to empty collections for all CQL collection types. | |
| 457 | 466 | * |
| 458 | 467 | * @param i the index to retrieve. |
| 459 | 468 | * @param targetType The Java type the value should be converted to. |
| 460 | 469 | * @return the value of the {@code i}th value converted to the given Java type. |
| 461 | - * If the CQL value is {@code NULL}, the default set of codecs will return {@code null} | |
| 462 | - * for a simple type, UDT or tuple, and an empty (immutable) collection for collection types. | |
| 463 | 470 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 464 | 471 | * @throws com.datastax.driver.core.exceptions.CodecNotFoundException |
| 465 | 472 | * if no {@link TypeCodec} instance for {@code targetType} could be found |
| @@ -475,6 +482,13 @@ | ||
| 475 | 482 | * <p> |
| 476 | 483 | * It is the caller's responsibility to ensure that the given codec {@link TypeCodec#accepts(DataType) accepts} |
| 477 | 484 | * the underlying CQL type; failing to do so may result in {@link InvalidTypeException}s being thrown. |
| 485 | + * <p> | |
| 486 | + * Implementation note: the actual object returned by this method will depend | |
| 487 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 488 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 489 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 490 | + * codec-dependent; by default, a CQL {@code NULL} value translates to {@code null} for | |
| 491 | + * simple CQL types, UDTs and tuples, and to empty collections for all CQL collection types. | |
| 478 | 492 | * |
| 479 | 493 | * @param i the index to retrieve. |
| 480 | 494 | * @param codec The {@link TypeCodec} to use to deserialize the value; may not be {@code null}. |
modified driver-core/src/main/java/com/datastax/driver/core/GettableByNameData.java +82 −64
| @@ -48,7 +48,6 @@ | ||
| 48 | 48 | * {@code false} is returned. |
| 49 | 49 | * |
| 50 | 50 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 51 | - * @throws InvalidTypeException if value {@code name} is not of type BOOLEAN. | |
| 52 | 51 | */ |
| 53 | 52 | public boolean getBool(String name); |
| 54 | 53 | |
| @@ -60,7 +59,6 @@ | ||
| 60 | 59 | * {@code 0} is returned. |
| 61 | 60 | * |
| 62 | 61 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 63 | - * @throws InvalidTypeException if value {@code name} is not of type TINYINT. | |
| 64 | 62 | */ |
| 65 | 63 | public byte getByte(String name); |
| 66 | 64 | |
| @@ -72,7 +70,6 @@ | ||
| 72 | 70 | * {@code 0} is returned. |
| 73 | 71 | * |
| 74 | 72 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 75 | - * @throws InvalidTypeException if value {@code name} is not of type SMALLINT. | |
| 76 | 73 | */ |
| 77 | 74 | public short getShort(String name); |
| 78 | 75 | |
| @@ -84,7 +81,6 @@ | ||
| 84 | 81 | * {@code 0} is returned. |
| 85 | 82 | * |
| 86 | 83 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 87 | - * @throws InvalidTypeException if value {@code name} is not of type INT. | |
| 88 | 84 | */ |
| 89 | 85 | public int getInt(String name); |
| 90 | 86 | |
| @@ -96,7 +92,6 @@ | ||
| 96 | 92 | * {@code 0L} is returned. |
| 97 | 93 | * |
| 98 | 94 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 99 | - * @throws InvalidTypeException if value {@code i} is not of type BIGINT or COUNTER. | |
| 100 | 95 | */ |
| 101 | 96 | public long getLong(String name); |
| 102 | 97 | |
| @@ -108,7 +103,6 @@ | ||
| 108 | 103 | * {@code null} is returned. |
| 109 | 104 | * |
| 110 | 105 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 111 | - * @throws InvalidTypeException if value {@code name} is not of type TIMESTAMP. | |
| 112 | 106 | */ |
| 113 | 107 | public Date getTimestamp(String name); |
| 114 | 108 | |
| @@ -120,7 +114,6 @@ | ||
| 120 | 114 | * {@code null} is returned. |
| 121 | 115 | * |
| 122 | 116 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 123 | - * @throws InvalidTypeException if value {@code name} is not of type DATE. | |
| 124 | 117 | */ |
| 125 | 118 | public LocalDate getDate(String name); |
| 126 | 119 | |
| @@ -132,7 +125,6 @@ | ||
| 132 | 125 | * {@code 0L} is returned. |
| 133 | 126 | * |
| 134 | 127 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 135 | - * @throws InvalidTypeException if value {@code name} is not of type TIME. | |
| 136 | 128 | */ |
| 137 | 129 | public long getTime(String name); |
| 138 | 130 | |
| @@ -144,7 +136,6 @@ | ||
| 144 | 136 | * {@code 0.0f} is returned. |
| 145 | 137 | * |
| 146 | 138 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 147 | - * @throws InvalidTypeException if value {@code name} is not of type FLOAT. | |
| 148 | 139 | */ |
| 149 | 140 | public float getFloat(String name); |
| 150 | 141 | |
| @@ -156,7 +147,6 @@ | ||
| 156 | 147 | * {@code 0.0} is returned. |
| 157 | 148 | * |
| 158 | 149 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 159 | - * @throws InvalidTypeException if value {@code name} is not of type DOUBLE. | |
| 160 | 150 | */ |
| 161 | 151 | public double getDouble(String name); |
| 162 | 152 | |
| @@ -187,7 +177,6 @@ | ||
| 187 | 177 | * {@code null} is returned. |
| 188 | 178 | * |
| 189 | 179 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 190 | - * @throws InvalidTypeException if value {@code i} type is not of type BLOB. | |
| 191 | 180 | */ |
| 192 | 181 | public ByteBuffer getBytes(String name); |
| 193 | 182 | |
| @@ -199,8 +188,6 @@ | ||
| 199 | 188 | * {@code null} is returned. |
| 200 | 189 | * |
| 201 | 190 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 202 | - * @throws InvalidTypeException if value {@code name} type is none of: | |
| 203 | - * VARCHAR, TEXT or ASCII. | |
| 204 | 191 | */ |
| 205 | 192 | public String getString(String name); |
| 206 | 193 | |
| @@ -212,7 +199,6 @@ | ||
| 212 | 199 | * If the value is NULL, {@code null} is returned. |
| 213 | 200 | * |
| 214 | 201 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 215 | - * @throws InvalidTypeException if value {@code name} is not of type VARINT. | |
| 216 | 202 | */ |
| 217 | 203 | public BigInteger getVarint(String name); |
| 218 | 204 | |
| @@ -224,7 +210,6 @@ | ||
| 224 | 210 | * If the value is NULL, {@code null} is returned. |
| 225 | 211 | * |
| 226 | 212 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 227 | - * @throws InvalidTypeException if value {@code name} is not of type DECIMAL. | |
| 228 | 213 | */ |
| 229 | 214 | public BigDecimal getDecimal(String name); |
| 230 | 215 | |
| @@ -236,8 +221,6 @@ | ||
| 236 | 221 | * If the value is NULL, {@code null} is returned. |
| 237 | 222 | * |
| 238 | 223 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 239 | - * @throws InvalidTypeException if value {@code name} is not of type | |
| 240 | - * UUID or TIMEUUID. | |
| 241 | 224 | */ |
| 242 | 225 | public UUID getUUID(String name); |
| 243 | 226 | |
| @@ -249,8 +232,6 @@ | ||
| 249 | 232 | * If the value is NULL, {@code null} is returned. |
| 250 | 233 | * |
| 251 | 234 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 252 | - * @throws InvalidTypeException if value {@code name} is not of type | |
| 253 | - * INET. | |
| 254 | 235 | */ |
| 255 | 236 | public InetAddress getInet(String name); |
| 256 | 237 | |
| @@ -258,17 +239,21 @@ | ||
| 258 | 239 | * Returns the value for {@code name} as a list. |
| 259 | 240 | * <p> |
| 260 | 241 | * If the type of the elements is generic, use {@link #getList(String, TypeToken)}. |
| 242 | + * <p> | |
| 243 | + * Implementation note: the actual {@link List} implementation will depend | |
| 244 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 245 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 246 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 247 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 248 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 249 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 261 | 250 | * |
| 262 | 251 | * @param name the name to retrieve. |
| 263 | 252 | * @param elementsClass the class for the elements of the list to retrieve. |
| 264 | 253 | * @return the value of the {@code i}th element as a list of |
| 265 | - * {@code T} objects. If the value is NULL, an empty list is | |
| 266 | - * returned (note that Cassandra makes no difference between an empty list | |
| 267 | - * and column of type list that is not set). The returned list is immutable. | |
| 254 | + * {@code T} objects. | |
| 268 | 255 | * |
| 269 | 256 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 270 | - * @throws InvalidTypeException if value {@code name} is not a list or if its | |
| 271 | - * elements are not of class {@code T}. | |
| 272 | 257 | */ |
| 273 | 258 | public <T> List<T> getList(String name, Class<T> elementsClass); |
| 274 | 259 | |
| @@ -279,17 +264,21 @@ | ||
| 279 | 264 | * <pre> |
| 280 | 265 | * {@code List<List<String>> l = row.getList("theColumn", new TypeToken<List<String>>() {});} |
| 281 | 266 | * </pre> |
| 267 | + * <p> | |
| 268 | + * Implementation note: the actual {@link List} implementation will depend | |
| 269 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 270 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 271 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 272 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 273 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 274 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 282 | 275 | * |
| 283 | 276 | * @param name the name to retrieve. |
| 284 | 277 | * @param elementsType the type for the elements of the list to retrieve. |
| 285 | 278 | * @return the value of the {@code i}th element as a list of |
| 286 | - * {@code T} objects. If the value is NULL, an empty list is | |
| 287 | - * returned (note that Cassandra makes no difference between an empty list | |
| 288 | - * and column of type list that is not set). The returned list is immutable. | |
| 279 | + * {@code T} objects. | |
| 289 | 280 | * |
| 290 | 281 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 291 | - * @throws InvalidTypeException if value {@code name} is not a list or if its | |
| 292 | - * elements are not of class {@code T}. | |
| 293 | 282 | */ |
| 294 | 283 | public <T> List<T> getList(String name, TypeToken<T> elementsType); |
| 295 | 284 | |
| @@ -297,17 +286,21 @@ | ||
| 297 | 286 | * Returns the value for {@code name} as a set. |
| 298 | 287 | * <p> |
| 299 | 288 | * If the type of the elements is generic, use {@link #getSet(String, TypeToken)}. |
| 289 | + * <p> | |
| 290 | + * Implementation note: the actual {@link Set} implementation will depend | |
| 291 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 292 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 293 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 294 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 295 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 296 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 300 | 297 | * |
| 301 | 298 | * @param name the name to retrieve. |
| 302 | 299 | * @param elementsClass the class for the elements of the set to retrieve. |
| 303 | 300 | * @return the value of the {@code i}th element as a set of |
| 304 | - * {@code T} objects. If the value is NULL, an empty set is | |
| 305 | - * returned (note that Cassandra makes no difference between an empty set | |
| 306 | - * and column of type set that is not set). The returned set is immutable. | |
| 301 | + * {@code T} objects. | |
| 307 | 302 | * |
| 308 | 303 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 309 | - * @throws InvalidTypeException if value {@code name} is not a set or if its | |
| 310 | - * elements are not of class {@code T}. | |
| 311 | 304 | */ |
| 312 | 305 | public <T> Set<T> getSet(String name, Class<T> elementsClass); |
| 313 | 306 | |
| @@ -318,17 +311,21 @@ | ||
| 318 | 311 | * <pre> |
| 319 | 312 | * {@code Set<List<String>> l = row.getSet("theColumn", new TypeToken<List<String>>() {});} |
| 320 | 313 | * </pre> |
| 314 | + * <p> | |
| 315 | + * Implementation note: the actual {@link Set} implementation will depend | |
| 316 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 317 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 318 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 319 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 320 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 321 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 321 | 322 | * |
| 322 | 323 | * @param name the name to retrieve. |
| 323 | 324 | * @param elementsType the type for the elements of the set to retrieve. |
| 324 | 325 | * @return the value of the {@code i}th element as a set of |
| 325 | - * {@code T} objects. If the value is NULL, an empty set is | |
| 326 | - * returned (note that Cassandra makes no difference between an empty set | |
| 327 | - * and column of type set that is not set). The returned set is immutable. | |
| 326 | + * {@code T} objects. | |
| 328 | 327 | * |
| 329 | 328 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 330 | - * @throws InvalidTypeException if value {@code name} is not a set or if its | |
| 331 | - * elements are not of class {@code T}. | |
| 332 | 329 | */ |
| 333 | 330 | public <T> Set<T> getSet(String name, TypeToken<T> elementsType); |
| 334 | 331 | |
| @@ -336,20 +333,22 @@ | ||
| 336 | 333 | * Returns the value for {@code name} as a map. |
| 337 | 334 | * <p> |
| 338 | 335 | * If the type of the keys and/or values is generic, use {@link #getMap(String, TypeToken, TypeToken)}. |
| 336 | + * <p> | |
| 337 | + * Implementation note: the actual {@link Map} implementation will depend | |
| 338 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 339 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 340 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 341 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 342 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 343 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 339 | 344 | * |
| 340 | 345 | * @param name the name to retrieve. |
| 341 | 346 | * @param keysClass the class for the keys of the map to retrieve. |
| 342 | 347 | * @param valuesClass the class for the values of the map to retrieve. |
| 343 | 348 | * @return the value of {@code name} as a map of |
| 344 | - * {@code K} to {@code V} objects. If the value is NULL, | |
| 345 | - * an empty map is returned (note that Cassandra makes no difference | |
| 346 | - * between an empty map and column of type map that is not set). The | |
| 347 | - * returned map is immutable. | |
| 349 | + * {@code K} to {@code V} objects. | |
| 348 | 350 | * |
| 349 | 351 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 350 | - * @throws InvalidTypeException if value {@code name} is not a map, if its | |
| 351 | - * keys are not of class {@code K} or if its values are not of | |
| 352 | - * class {@code V}. | |
| 353 | 352 | */ |
| 354 | 353 | public <K, V> Map<K, V> getMap(String name, Class<K> keysClass, Class<V> valuesClass); |
| 355 | 354 | |
| @@ -360,20 +359,22 @@ | ||
| 360 | 359 | * <pre> |
| 361 | 360 | * {@code Map<Int, List<String>> l = row.getMap("theColumn", TypeToken.of(Integer.class), new TypeToken<List<String>>() {});} |
| 362 | 361 | * </pre> |
| 362 | + * <p> | |
| 363 | + * Implementation note: the actual {@link Map} implementation will depend | |
| 364 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 365 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 366 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 367 | + * codec-dependent. By default, the driver will return mutable instances, and | |
| 368 | + * a CQL {@code NULL} will mapped to an empty collection (note that Cassandra | |
| 369 | + * makes no distinction between {@code NULL} and an empty collection). | |
| 363 | 370 | * |
| 364 | 371 | * @param name the name to retrieve. |
| 365 | 372 | * @param keysType the class for the keys of the map to retrieve. |
| 366 | 373 | * @param valuesType the class for the values of the map to retrieve. |
| 367 | 374 | * @return the value of {@code name} as a map of |
| 368 | - * {@code K} to {@code V} objects. If the value is NULL, | |
| 369 | - * an empty map is returned (note that Cassandra makes no difference | |
| 370 | - * between an empty map and column of type map that is not set). The | |
| 371 | - * returned map is immutable. | |
| 375 | + * {@code K} to {@code V} objects. | |
| 372 | 376 | * |
| 373 | 377 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 374 | - * @throws InvalidTypeException if value {@code name} is not a map, if its | |
| 375 | - * keys are not of class {@code K} or if its values are not of | |
| 376 | - * class {@code V}. | |
| 377 | 378 | */ |
| 378 | 379 | public <K, V> Map<K, V> getMap(String name, TypeToken<K> keysType, TypeToken<V> valuesType); |
| 379 | 380 | |
| @@ -385,7 +386,6 @@ | ||
| 385 | 386 | * then {@code null} will be returned. |
| 386 | 387 | * |
| 387 | 388 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 388 | - * @throws InvalidTypeException if value {@code i} is not a UDT value. | |
| 389 | 389 | */ |
| 390 | 390 | public UDTValue getUDTValue(String name); |
| 391 | 391 | |
| @@ -397,22 +397,21 @@ | ||
| 397 | 397 | * then {@code null} will be returned. |
| 398 | 398 | * |
| 399 | 399 | * @throws IllegalArgumentException if {@code name} is not valid name for this object. |
| 400 | - * @throws InvalidTypeException if value {@code i} is not a tuple value. | |
| 401 | 400 | */ |
| 402 | 401 | public TupleValue getTupleValue(String name); |
| 403 | 402 | |
| 404 | 403 | /** |
| 405 | 404 | * Returns the value for {@code name} as the Java type matching its CQL type. |
| 406 | 405 | * <p> |
| 407 | - * This method uses the default codec for the underlying CQL type | |
| 408 | - * to perform deserialization, and is safe to be used | |
| 409 | - * <em>as long as only default codecs are in use</em>. | |
| 410 | - * If a second, custom codec for the same CQL type is registered, which one will | |
| 411 | - * be used is unspecified; in such cases, it is preferable to use | |
| 412 | - * the more deterministic methods {@link #get(String, Class)} or {@link #get(String, TypeToken)} instead. | |
| 406 | + * Note: if two or more codecs are available | |
| 407 | + * for the underlying CQL type, <em>the one that will be used will be | |
| 408 | + * the first one to be registered.</em>. | |
| 409 | + * <p> | |
| 410 | + * For these reasons, it is generally preferable to use the more | |
| 411 | + * deterministic methods {@link #get(String, Class)} or {@link #get(String, TypeToken)} instead. | |
| 413 | 412 | * |
| 414 | 413 | * @param name the name to retrieve. |
| 415 | - * @return the value of the {@code i}th value as the Java type matching its CQL type. | |
| 414 | + * @return the value of {@code name} as the Java type matching its CQL type. | |
| 416 | 415 | * If the value is NULL and is a simple type, UDT or tuple, {@code null} is returned. |
| 417 | 416 | * If it is NULL and is a collection type, an empty (immutable) collection is returned. |
| 418 | 417 | * |
| @@ -430,11 +429,17 @@ | ||
| 430 | 429 | * where more than one codec is registered for the same CQL type; specifying the Java class |
| 431 | 430 | * allows the {@link CodecRegistry} to narrow down the search and return only an exactly-matching codec (if any), |
| 432 | 431 | * thus avoiding any risk of ambiguity. |
| 432 | + * <p> | |
| 433 | + * Implementation note: the actual object returned by this method will depend | |
| 434 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 435 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 436 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 437 | + * codec-dependent; by default, a CQL {@code NULL} value translates to {@code null} for | |
| 438 | + * simple CQL types, UDTs and tuples, and to empty collections for all CQL collection types. | |
| 433 | 439 | * |
| 434 | 440 | * @param name the name to retrieve. |
| 435 | 441 | * @param targetClass The Java type the value should be converted to. |
| 436 | 442 | * @return the value for {@code name} value converted to the given Java type. |
| 437 | - * If the CQL value is {@code NULL}, then {@code null} is returned. | |
| 438 | 443 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 439 | 444 | * @throws com.datastax.driver.core.exceptions.CodecNotFoundException |
| 440 | 445 | * if no {@link TypeCodec} instance for {@code targetClass} could be found |
| @@ -452,11 +457,17 @@ | ||
| 452 | 457 | * where more than one codec is registered for the same CQL type; specifying the Java class |
| 453 | 458 | * allows the {@link CodecRegistry} to narrow down the search and return only an exactly-matching codec (if any), |
| 454 | 459 | * thus avoiding any risk of ambiguity. |
| 460 | + * <p> | |
| 461 | + * Implementation note: the actual object returned by this method will depend | |
| 462 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 463 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 464 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 465 | + * codec-dependent; by default, a CQL {@code NULL} value translates to {@code null} for | |
| 466 | + * simple CQL types, UDTs and tuples, and to empty collections for all CQL collection types. | |
| 455 | 467 | * |
| 456 | 468 | * @param name the name to retrieve. |
| 457 | 469 | * @param targetType The Java type the value should be converted to. |
| 458 | 470 | * @return the value for {@code name} value converted to the given Java type. |
| 459 | - * If the CQL value is {@code NULL}, then {@code null} is returned. | |
| 460 | 471 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 461 | 472 | * @throws com.datastax.driver.core.exceptions.CodecNotFoundException |
| 462 | 473 | * if no {@link TypeCodec} instance for {@code targetType} could be found |
| @@ -472,6 +483,13 @@ | ||
| 472 | 483 | * <p> |
| 473 | 484 | * It is the caller's responsibility to ensure that the given codec {@link TypeCodec#accepts(DataType) accepts} |
| 474 | 485 | * the underlying CQL type; failing to do so may result in {@link InvalidTypeException}s being thrown. |
| 486 | + * <p> | |
| 487 | + * Implementation note: the actual object returned by this method will depend | |
| 488 | + * on the {@link TypeCodec codec} being used; therefore, callers should | |
| 489 | + * make no assumptions concerning its mutability nor its thread-safety. | |
| 490 | + * Furthermore, the behavior of this method in respect to CQL {@code NULL} values is also | |
| 491 | + * codec-dependent; by default, a CQL {@code NULL} value translates to {@code null} for | |
| 492 | + * simple CQL types, UDTs and tuples, and to empty collections for all CQL collection types. | |
| 475 | 493 | * |
| 476 | 494 | * @param name the name to retrieve. |
| 477 | 495 | * @param codec The {@link TypeCodec} to use to deserialize the value; may not be {@code null}. |
modified driver-core/src/main/java/com/datastax/driver/core/Row.java +0 −624
| @@ -15,12 +15,6 @@ | ||
| 15 | 15 | */ |
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | -import java.math.BigDecimal; | |
| 19 | -import java.math.BigInteger; | |
| 20 | -import java.net.InetAddress; | |
| 21 | -import java.nio.ByteBuffer; | |
| 22 | -import java.util.*; | |
| 23 | - | |
| 24 | 18 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 25 | 19 | |
| 26 | 20 | /** |
| @@ -31,7 +25,6 @@ | ||
| 31 | 25 | * rules explained in {@link ColumnDefinitions}. |
| 32 | 26 | */ |
| 33 | 27 | public interface Row extends GettableData { |
| 34 | - // Note that we re-include all the methods of GettableData just for the sake of better javadoc | |
| 35 | 28 | |
| 36 | 29 | /** |
| 37 | 30 | * Returns the columns contained in this Row. |
| @@ -40,505 +33,6 @@ | ||
| 40 | 33 | */ |
| 41 | 34 | public ColumnDefinitions getColumnDefinitions(); |
| 42 | 35 | |
| 43 | - /** | |
| 44 | - * Returns whether the {@code i}th value of this row is NULL. | |
| 45 | - * | |
| 46 | - * @param i the index ({@code 0 <= i < size()}) of the column to check. | |
| 47 | - * @return whether the {@code i}th value of this row is NULL. | |
| 48 | - * | |
| 49 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 50 | - */ | |
| 51 | - @Override | |
| 52 | - public boolean isNull(int i); | |
| 53 | - | |
| 54 | - /** | |
| 55 | - * Returns whether the value for column {@code name} in this row is NULL. | |
| 56 | - * | |
| 57 | - * @param name the name of the column to check. | |
| 58 | - * @return whether the value of column {@code name} is NULL. | |
| 59 | - * | |
| 60 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 61 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 62 | - */ | |
| 63 | - @Override | |
| 64 | - public boolean isNull(String name); | |
| 65 | - | |
| 66 | - /** | |
| 67 | - * Returns the {@code i}th value of this row as a boolean. | |
| 68 | - * | |
| 69 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 70 | - * @return the boolean value of the {@code i}th column in this row. If the | |
| 71 | - * value is NULL, {@code false} is returned. | |
| 72 | - * | |
| 73 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 74 | - * @throws InvalidTypeException if column {@code i} is not of type BOOLEAN. | |
| 75 | - */ | |
| 76 | - @Override | |
| 77 | - public boolean getBool(int i); | |
| 78 | - | |
| 79 | - /** | |
| 80 | - * Returns the value of column {@code name} as a boolean. | |
| 81 | - * | |
| 82 | - * @param name the name of the column to retrieve. | |
| 83 | - * @return the boolean value of column {@code name}. If the value is NULL, | |
| 84 | - * {@code false} is returned. | |
| 85 | - * | |
| 86 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 87 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 88 | - * @throws InvalidTypeException if column {@code name} is not of type BOOLEAN. | |
| 89 | - */ | |
| 90 | - @Override | |
| 91 | - public boolean getBool(String name); | |
| 92 | - | |
| 93 | - /** | |
| 94 | - * Returns the {@code i}th value of this row as a byte. | |
| 95 | - * | |
| 96 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 97 | - * @return the value of the {@code i}th column in this row as a byte. If the | |
| 98 | - * value is NULL, {@code 0} is returned. | |
| 99 | - * | |
| 100 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 101 | - * @throws InvalidTypeException if column {@code i} is not of type TINYINT. | |
| 102 | - */ | |
| 103 | - @Override | |
| 104 | - public byte getByte(int i); | |
| 105 | - | |
| 106 | - /** | |
| 107 | - * Returns the value of column {@code name} as a byte. | |
| 108 | - * | |
| 109 | - * @param name the name of the column to retrieve. | |
| 110 | - * @return the value of column {@code name} as a byte. If the value is NULL, | |
| 111 | - * {@code 0} is returned. | |
| 112 | - * | |
| 113 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 114 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 115 | - * @throws InvalidTypeException if column {@code name} is not of type TINYINT. | |
| 116 | - */ | |
| 117 | - @Override | |
| 118 | - public byte getByte(String name); | |
| 119 | - | |
| 120 | - /** | |
| 121 | - * Returns the {@code i}th value of this row as a short. | |
| 122 | - * | |
| 123 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 124 | - * @return the value of the {@code i}th column in this row as a short. If the | |
| 125 | - * value is NULL, {@code 0} is returned. | |
| 126 | - * | |
| 127 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 128 | - * @throws InvalidTypeException if column {@code i} is not of type SMALLINT. | |
| 129 | - */ | |
| 130 | - @Override | |
| 131 | - public short getShort(int i); | |
| 132 | - | |
| 133 | - /** | |
| 134 | - * Returns the value of column {@code name} as a short. | |
| 135 | - * | |
| 136 | - * @param name the name of the column to retrieve. | |
| 137 | - * @return the value of column {@code name} as a short. If the value is NULL, | |
| 138 | - * {@code 0} is returned. | |
| 139 | - * | |
| 140 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 141 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 142 | - * @throws InvalidTypeException if column {@code name} is not of type SMALLINT. | |
| 143 | - */ | |
| 144 | - @Override | |
| 145 | - public short getShort(String name); | |
| 146 | - | |
| 147 | - /** | |
| 148 | - * Returns the {@code i}th value of this row as an integer. | |
| 149 | - * | |
| 150 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 151 | - * @return the value of the {@code i}th column in this row as an integer. If the | |
| 152 | - * value is NULL, {@code 0} is returned. | |
| 153 | - * | |
| 154 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 155 | - * @throws InvalidTypeException if column {@code i} is not of type INT. | |
| 156 | - */ | |
| 157 | - @Override | |
| 158 | - public int getInt(int i); | |
| 159 | - | |
| 160 | - /** | |
| 161 | - * Returns the value of column {@code name} as an integer. | |
| 162 | - * | |
| 163 | - * @param name the name of the column to retrieve. | |
| 164 | - * @return the value of column {@code name} as an integer. If the value is NULL, | |
| 165 | - * {@code 0} is returned. | |
| 166 | - * | |
| 167 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 168 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 169 | - * @throws InvalidTypeException if column {@code name} is not of type INT. | |
| 170 | - */ | |
| 171 | - @Override | |
| 172 | - public int getInt(String name); | |
| 173 | - | |
| 174 | - /** | |
| 175 | - * Returns the {@code i}th value of this row as a long. | |
| 176 | - * | |
| 177 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 178 | - * @return the value of the {@code i}th column in this row as a long. If the | |
| 179 | - * value is NULL, {@code 0L} is returned. | |
| 180 | - * | |
| 181 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 182 | - * @throws InvalidTypeException if column {@code i} is not of type BIGINT or COUNTER. | |
| 183 | - */ | |
| 184 | - @Override | |
| 185 | - public long getLong(int i); | |
| 186 | - | |
| 187 | - /** | |
| 188 | - * Returns the value of column {@code name} as a long. | |
| 189 | - * | |
| 190 | - * @param name the name of the column to retrieve. | |
| 191 | - * @return the value of column {@code name} as a long. If the value is NULL, | |
| 192 | - * {@code 0L} is returned. | |
| 193 | - * | |
| 194 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 195 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 196 | - * @throws InvalidTypeException if column {@code i} is not of type BIGINT or COUNTER. | |
| 197 | - */ | |
| 198 | - @Override | |
| 199 | - public long getLong(String name); | |
| 200 | - | |
| 201 | - /** | |
| 202 | - * Returns the {@code i}th value of this row as a date. | |
| 203 | - * | |
| 204 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 205 | - * @return the value of the {@code i}th column in this row as a data. If the | |
| 206 | - * value is NULL, {@code null} is returned. | |
| 207 | - * | |
| 208 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 209 | - * @throws InvalidTypeException if column {@code i} is not of type TIMESTAMP. | |
| 210 | - */ | |
| 211 | - @Override | |
| 212 | - public Date getTimestamp(int i); | |
| 213 | - | |
| 214 | - /** | |
| 215 | - * Returns the value of column {@code name} as a date. | |
| 216 | - * | |
| 217 | - * @param name the name of the column to retrieve. | |
| 218 | - * @return the value of column {@code name} as a date. If the value is NULL, | |
| 219 | - * {@code null} is returned. | |
| 220 | - * | |
| 221 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 222 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 223 | - * @throws InvalidTypeException if column {@code name} is not of type TIMESTAMP. | |
| 224 | - */ | |
| 225 | - @Override | |
| 226 | - public Date getTimestamp(String name); | |
| 227 | - | |
| 228 | - /** | |
| 229 | - * Returns the {@code i}th value as a date (without time). | |
| 230 | - * | |
| 231 | - * @param i the index ({@code 0 <= i < size()}) to retrieve. | |
| 232 | - * @return the value of the {@code i}th element as a date. If the | |
| 233 | - * value is NULL, {@code null} is returned. | |
| 234 | - * | |
| 235 | - * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 236 | - * @throws InvalidTypeException if value {@code i} is not of type DATE. | |
| 237 | - */ | |
| 238 | - @Override | |
| 239 | - public LocalDate getDate(int i); | |
| 240 | - | |
| 241 | - /** | |
| 242 | - * Returns the value for {@code name} as a date (without time). | |
| 243 | - * | |
| 244 | - * @param name the name to retrieve. | |
| 245 | - * @return the value for {@code name} as a date. If the value is NULL, | |
| 246 | - * {@code null} is returned. | |
| 247 | - * | |
| 248 | - * @throws IllegalArgumentException if {@code name} is not valid name for this object. | |
| 249 | - * @throws InvalidTypeException if value {@code name} is not of type DATE. | |
| 250 | - */ | |
| 251 | - @Override | |
| 252 | - public LocalDate getDate(String name); | |
| 253 | - | |
| 254 | - /** | |
| 255 | - * Returns the {@code i}th value as a long in nanoseconds since midnight. | |
| 256 | - * | |
| 257 | - * @param i the index ({@code 0 <= i < size()}) to retrieve. | |
| 258 | - * @return the value of the {@code i}th element as a long. If the | |
| 259 | - * value is NULL, {@code 0L} is returned. | |
| 260 | - * | |
| 261 | - * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 262 | - * @throws InvalidTypeException if value {@code i} is not of type TIME. | |
| 263 | - */ | |
| 264 | - @Override | |
| 265 | - public long getTime(int i); | |
| 266 | - | |
| 267 | - /** | |
| 268 | - * Returns the value for {@code name} as a long in nanoseconds since midnight. | |
| 269 | - * | |
| 270 | - * @param name the name to retrieve. | |
| 271 | - * @return the value for {@code name} as a long. If the value is NULL, | |
| 272 | - * {@code 0L} is returned. | |
| 273 | - * | |
| 274 | - * @throws IllegalArgumentException if {@code name} is not valid name for this object. | |
| 275 | - * @throws InvalidTypeException if value {@code name} is not of type TIME. | |
| 276 | - */ | |
| 277 | - @Override | |
| 278 | - public long getTime(String name); | |
| 279 | - | |
| 280 | - /** | |
| 281 | - * Returns the {@code i}th value of this row as a float. | |
| 282 | - * | |
| 283 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 284 | - * @return the value of the {@code i}th column in this row as a float. If the | |
| 285 | - * value is NULL, {@code 0.0f} is returned. | |
| 286 | - * | |
| 287 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 288 | - * @throws InvalidTypeException if column {@code i} is not of type FLOAT. | |
| 289 | - */ | |
| 290 | - @Override | |
| 291 | - public float getFloat(int i); | |
| 292 | - | |
| 293 | - /** | |
| 294 | - * Returns the value of column {@code name} as a float. | |
| 295 | - * | |
| 296 | - * @param name the name of the column to retrieve. | |
| 297 | - * @return the value of column {@code name} as a float. If the value is NULL, | |
| 298 | - * {@code 0.0f} is returned. | |
| 299 | - * | |
| 300 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 301 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 302 | - * @throws InvalidTypeException if column {@code name} is not of type FLOAT. | |
| 303 | - */ | |
| 304 | - @Override | |
| 305 | - public float getFloat(String name); | |
| 306 | - | |
| 307 | - /** | |
| 308 | - * Returns the {@code i}th value of this row as a double. | |
| 309 | - * | |
| 310 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 311 | - * @return the value of the {@code i}th column in this row as a double. If the | |
| 312 | - * value is NULL, {@code 0.0} is returned. | |
| 313 | - * | |
| 314 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 315 | - * @throws InvalidTypeException if column {@code i} is not of type DOUBLE. | |
| 316 | - */ | |
| 317 | - @Override | |
| 318 | - public double getDouble(int i); | |
| 319 | - | |
| 320 | - /** | |
| 321 | - * Returns the value of column {@code name} as a double. | |
| 322 | - * | |
| 323 | - * @param name the name of the column to retrieve. | |
| 324 | - * @return the value of column {@code name} as a double. If the value is NULL, | |
| 325 | - * {@code 0.0} is returned. | |
| 326 | - * | |
| 327 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 328 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 329 | - * @throws InvalidTypeException if column {@code name} is not of type DOUBLE. | |
| 330 | - */ | |
| 331 | - @Override | |
| 332 | - public double getDouble(String name); | |
| 333 | - | |
| 334 | - /** | |
| 335 | - * Returns the {@code i}th value of this row as a ByteBuffer. | |
| 336 | - * | |
| 337 | - * Note: this method always return the bytes composing the value, even if | |
| 338 | - * the column is not of type BLOB. That is, this method never throw an | |
| 339 | - * InvalidTypeException. However, if the type is not BLOB, it is up to the | |
| 340 | - * caller to handle the returned value correctly. | |
| 341 | - * | |
| 342 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 343 | - * @return the value of the {@code i}th column in this row as a ByteBuffer. If the | |
| 344 | - * value is NULL, {@code null} is returned. | |
| 345 | - * | |
| 346 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 347 | - */ | |
| 348 | - @Override | |
| 349 | - public ByteBuffer getBytesUnsafe(int i); | |
| 350 | - | |
| 351 | - /** | |
| 352 | - * Returns the value of column {@code name} as a ByteBuffer. | |
| 353 | - * | |
| 354 | - * Note: this method always return the bytes composing the value, even if | |
| 355 | - * the column is not of type BLOB. That is, this method never throw an | |
| 356 | - * InvalidTypeException. However, if the type is not BLOB, it is up to the | |
| 357 | - * caller to handle the returned value correctly. | |
| 358 | - * | |
| 359 | - * @param name the name of the column to retrieve. | |
| 360 | - * @return the value of column {@code name} as a ByteBuffer. If the value is NULL, | |
| 361 | - * {@code null} is returned. | |
| 362 | - * | |
| 363 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 364 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 365 | - */ | |
| 366 | - @Override | |
| 367 | - public ByteBuffer getBytesUnsafe(String name); | |
| 368 | - | |
| 369 | - /** | |
| 370 | - * Returns the {@code i}th value of this row as a byte array. | |
| 371 | - * <p> | |
| 372 | - * Note that this method validate that the column is of type BLOB. If you want to retrieve | |
| 373 | - * the bytes for any type of columns, use {@link #getBytesUnsafe(int)} instead. | |
| 374 | - * | |
| 375 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 376 | - * @return the value of the {@code i}th column in this row as a byte array. If the | |
| 377 | - * value is NULL, {@code null} is returned. | |
| 378 | - * | |
| 379 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 380 | - * @throws InvalidTypeException if column {@code i} type is not of type BLOB. | |
| 381 | - */ | |
| 382 | - @Override | |
| 383 | - public ByteBuffer getBytes(int i); | |
| 384 | - | |
| 385 | - /** | |
| 386 | - * Returns the value of column {@code name} as a byte array. | |
| 387 | - * <p> | |
| 388 | - * Note that this method validate that the column is of type BLOB. If you want to retrieve | |
| 389 | - * the bytes for any type of columns, use {@link #getBytesUnsafe(String)} instead. | |
| 390 | - * | |
| 391 | - * @param name the name of the column to retrieve. | |
| 392 | - * @return the value of column {@code name} as a byte array. If the value is NULL, | |
| 393 | - * {@code null} is returned. | |
| 394 | - * | |
| 395 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 396 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 397 | - * @throws InvalidTypeException if column {@code i} type is not of type BLOB. | |
| 398 | - */ | |
| 399 | - @Override | |
| 400 | - public ByteBuffer getBytes(String name); | |
| 401 | - | |
| 402 | - /** | |
| 403 | - * Returns the {@code i}th value of this row as a string. | |
| 404 | - * | |
| 405 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 406 | - * @return the value of the {@code i}th column in this row as a string. If the | |
| 407 | - * value is NULL, {@code null} is returned. | |
| 408 | - * | |
| 409 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 410 | - * @throws InvalidTypeException if column {@code i} type is none of: | |
| 411 | - * VARCHAR, TEXT or ASCII. | |
| 412 | - */ | |
| 413 | - @Override | |
| 414 | - public String getString(int i); | |
| 415 | - | |
| 416 | - /** | |
| 417 | - * Returns the value of column {@code name} as a string. | |
| 418 | - * | |
| 419 | - * @param name the name of the column to retrieve. | |
| 420 | - * @return the value of column {@code name} as a string. If the value is NULL, | |
| 421 | - * {@code null} is returned. | |
| 422 | - * | |
| 423 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 424 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 425 | - * @throws InvalidTypeException if column {@code name} type is none of: | |
| 426 | - * VARCHAR, TEXT or ASCII. | |
| 427 | - */ | |
| 428 | - @Override | |
| 429 | - public String getString(String name); | |
| 430 | - | |
| 431 | - /** | |
| 432 | - * Returns the {@code i}th value of this row as a variable length integer. | |
| 433 | - * | |
| 434 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 435 | - * @return the value of the {@code i}th column in this row as a variable | |
| 436 | - * length integer. If the value is NULL, {@code null} is returned. | |
| 437 | - * | |
| 438 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 439 | - * @throws InvalidTypeException if column {@code i} is not of type VARINT. | |
| 440 | - */ | |
| 441 | - @Override | |
| 442 | - public BigInteger getVarint(int i); | |
| 443 | - | |
| 444 | - /** | |
| 445 | - * Returns the value of column {@code name} as a variable length integer. | |
| 446 | - * | |
| 447 | - * @param name the name of the column to retrieve. | |
| 448 | - * @return the value of column {@code name} as a variable length integer. | |
| 449 | - * If the value is NULL, {@code null} is returned. | |
| 450 | - * | |
| 451 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 452 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 453 | - * @throws InvalidTypeException if column {@code name} is not of type VARINT. | |
| 454 | - */ | |
| 455 | - @Override | |
| 456 | - public BigInteger getVarint(String name); | |
| 457 | - | |
| 458 | - /** | |
| 459 | - * Returns the {@code i}th value of this row as a variable length decimal. | |
| 460 | - * | |
| 461 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 462 | - * @return the value of the {@code i}th column in this row as a variable | |
| 463 | - * length decimal. If the value is NULL, {@code null} is returned. | |
| 464 | - * | |
| 465 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 466 | - * @throws InvalidTypeException if column {@code i} is not of type DECIMAL. | |
| 467 | - */ | |
| 468 | - @Override | |
| 469 | - public BigDecimal getDecimal(int i); | |
| 470 | - | |
| 471 | - /** | |
| 472 | - * Returns the value of column {@code name} as a variable length decimal. | |
| 473 | - * | |
| 474 | - * @param name the name of the column to retrieve. | |
| 475 | - * @return the value of column {@code name} as a variable length decimal. | |
| 476 | - * If the value is NULL, {@code null} is returned. | |
| 477 | - * | |
| 478 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 479 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 480 | - * @throws InvalidTypeException if column {@code name} is not of type DECIMAL. | |
| 481 | - */ | |
| 482 | - @Override | |
| 483 | - public BigDecimal getDecimal(String name); | |
| 484 | - | |
| 485 | - /** | |
| 486 | - * Returns the {@code i}th value of this row as a UUID. | |
| 487 | - * | |
| 488 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 489 | - * @return the value of the {@code i}th column in this row as a UUID. | |
| 490 | - * If the value is NULL, {@code null} is returned. | |
| 491 | - * | |
| 492 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 493 | - * @throws InvalidTypeException if column {@code i} is not of type UUID | |
| 494 | - * or TIMEUUID. | |
| 495 | - */ | |
| 496 | - @Override | |
| 497 | - public UUID getUUID(int i); | |
| 498 | - | |
| 499 | - /** | |
| 500 | - * Returns the value of column {@code name} as a UUID. | |
| 501 | - * | |
| 502 | - * @param name the name of the column to retrieve. | |
| 503 | - * @return the value of column {@code name} as a UUID. | |
| 504 | - * If the value is NULL, {@code null} is returned. | |
| 505 | - * | |
| 506 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 507 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 508 | - * @throws InvalidTypeException if column {@code name} is not of type | |
| 509 | - * UUID or TIMEUUID. | |
| 510 | - */ | |
| 511 | - @Override | |
| 512 | - public UUID getUUID(String name); | |
| 513 | - | |
| 514 | - /** | |
| 515 | - * Returns the {@code i}th value of this row as an InetAddress. | |
| 516 | - * | |
| 517 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 518 | - * @return the value of the {@code i}th column in this row as an InetAddress. | |
| 519 | - * If the value is NULL, {@code null} is returned. | |
| 520 | - * | |
| 521 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 522 | - * @throws InvalidTypeException if column {@code i} is not of type INET. | |
| 523 | - */ | |
| 524 | - @Override | |
| 525 | - public InetAddress getInet(int i); | |
| 526 | - | |
| 527 | - /** | |
| 528 | - * Returns the value of column {@code name} as an InetAddress. | |
| 529 | - * | |
| 530 | - * @param name the name of the column to retrieve. | |
| 531 | - * @return the value of column {@code name} as an InetAddress. | |
| 532 | - * If the value is NULL, {@code null} is returned. | |
| 533 | - * | |
| 534 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 535 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 536 | - * @throws InvalidTypeException if column {@code name} is not of type | |
| 537 | - * INET. | |
| 538 | - */ | |
| 539 | - @Override | |
| 540 | - public InetAddress getInet(String name); | |
| 541 | - | |
| 542 | 36 | /** |
| 543 | 37 | * Returns the {@code i}th value of this row as a {@link Token}. |
| 544 | 38 | * <p> |
| @@ -593,122 +87,4 @@ | ||
| 593 | 87 | */ |
| 594 | 88 | public Token getPartitionKeyToken(); |
| 595 | 89 | |
| 596 | - /** | |
| 597 | - * Returns the {@code i}th value of this row as a list. | |
| 598 | - * | |
| 599 | - * @param <T> the type of the elements of the list to return. | |
| 600 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 601 | - * @param elementsClass the class for the elements of the list to retrieve. | |
| 602 | - * @return the value of the {@code i}th column in this row as a list of | |
| 603 | - * {@code elementsClass} objects. If the value is NULL, an empty list is | |
| 604 | - * returned (note that Cassandra makes no difference between an empty list | |
| 605 | - * and column of type list that is not set). The returned list is immutable. | |
| 606 | - * | |
| 607 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 608 | - * @throws InvalidTypeException if column {@code i} is not a list or if its | |
| 609 | - * elements are not of class {@code elementsClass}. | |
| 610 | - */ | |
| 611 | - @Override | |
| 612 | - public <T> List<T> getList(int i, Class<T> elementsClass); | |
| 613 | - | |
| 614 | - /** | |
| 615 | - * Returns the value of column {@code name} as a list. | |
| 616 | - * | |
| 617 | - * @param <T> the type of the elements of the list to return. | |
| 618 | - * @param name the name of the column to retrieve. | |
| 619 | - * @param elementsClass the class for the elements of the list to retrieve. | |
| 620 | - * @return the value of the {@code i}th column in this row as a list of | |
| 621 | - * {@code elementsClass} objects. If the value is NULL, an empty list is | |
| 622 | - * returned (note that Cassandra makes no difference between an empty list | |
| 623 | - * and column of type list that is not set). The returned list is immutable. | |
| 624 | - * | |
| 625 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 626 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 627 | - * @throws InvalidTypeException if column {@code name} is not a list or if its | |
| 628 | - * elements are not of class {@code elementsClass}. | |
| 629 | - */ | |
| 630 | - @Override | |
| 631 | - public <T> List<T> getList(String name, Class<T> elementsClass); | |
| 632 | - | |
| 633 | - /** | |
| 634 | - * Returns the {@code i}th value of this row as a set. | |
| 635 | - * | |
| 636 | - * @param <T> the type of the elements of the set to return. | |
| 637 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 638 | - * @param elementsClass the class for the elements of the set to retrieve. | |
| 639 | - * @return the value of the {@code i}th column in this row as a set of | |
| 640 | - * {@code elementsClass} objects. If the value is NULL, an empty set is | |
| 641 | - * returned (note that Cassandra makes no difference between an empty set | |
| 642 | - * and column of type set that is not set). The returned set is immutable. | |
| 643 | - * | |
| 644 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 645 | - * @throws InvalidTypeException if column {@code i} is not a set or if its | |
| 646 | - * elements are not of class {@code elementsClass}. | |
| 647 | - */ | |
| 648 | - @Override | |
| 649 | - public <T> Set<T> getSet(int i, Class<T> elementsClass); | |
| 650 | - | |
| 651 | - /** | |
| 652 | - * Returns the value of column {@code name} as a set. | |
| 653 | - * | |
| 654 | - * @param <T> the type of the elements of the set to return. | |
| 655 | - * @param name the name of the column to retrieve. | |
| 656 | - * @param elementsClass the class for the elements of the set to retrieve. | |
| 657 | - * @return the value of the {@code i}th column in this row as a set of | |
| 658 | - * {@code elementsClass} objects. If the value is NULL, an empty set is | |
| 659 | - * returned (note that Cassandra makes no difference between an empty set | |
| 660 | - * and column of type set that is not set). The returned set is immutable. | |
| 661 | - * | |
| 662 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 663 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 664 | - * @throws InvalidTypeException if column {@code name} is not a set or if its | |
| 665 | - * elements are not of class {@code elementsClass}. | |
| 666 | - */ | |
| 667 | - @Override | |
| 668 | - public <T> Set<T> getSet(String name, Class<T> elementsClass); | |
| 669 | - | |
| 670 | - /** | |
| 671 | - * Returns the {@code i}th value of this row as a map. | |
| 672 | - * | |
| 673 | - * @param <K> the type of the keys of the map to return. | |
| 674 | - * @param <V> the type of the values of the map to return. | |
| 675 | - * @param i the index ({@code 0 <= i < size()}) of the column to retrieve. | |
| 676 | - * @param keysClass the class for the keys of the map to retrieve. | |
| 677 | - * @param valuesClass the class for the values of the map to retrieve. | |
| 678 | - * @return the value of the {@code i}th column in this row as a map of | |
| 679 | - * {@code keysClass} to {@code valuesClass} objects. If the value is NULL, | |
| 680 | - * an empty map is returned (note that Cassandra makes no difference | |
| 681 | - * between an empty map and column of type map that is not set). The | |
| 682 | - * returned map is immutable. | |
| 683 | - * | |
| 684 | - * @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}. | |
| 685 | - * @throws InvalidTypeException if column {@code i} is not a map, if its | |
| 686 | - * keys are not of class {@code keysClass} or if its values are not of | |
| 687 | - * class {@code valuesClass}. | |
| 688 | - */ | |
| 689 | - @Override | |
| 690 | - public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass); | |
| 691 | - | |
| 692 | - /** | |
| 693 | - * Returns the value of column {@code name} as a map. | |
| 694 | - * | |
| 695 | - * @param <K> the type of the keys of the map to return. | |
| 696 | - * @param <V> the type of the values of the map to return. | |
| 697 | - * @param name the name of the column to retrieve. | |
| 698 | - * @param keysClass the class for the keys of the map to retrieve. | |
| 699 | - * @param valuesClass the class for the values of the map to retrieve. | |
| 700 | - * @return the value of the {@code i}th column in this row as a map of | |
| 701 | - * {@code keysClass} to {@code valuesClass} objects. If the value is NULL, | |
| 702 | - * an empty map is returned (note that Cassandra makes no difference | |
| 703 | - * between an empty map and column of type map that is not set). The | |
| 704 | - * returned map is immutable. | |
| 705 | - * | |
| 706 | - * @throws IllegalArgumentException if {@code name} is not part of the | |
| 707 | - * ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}. | |
| 708 | - * @throws InvalidTypeException if column {@code name} is not a map, if its | |
| 709 | - * keys are not of class {@code keysClass} or if its values are not of | |
| 710 | - * class {@code valuesClass}. | |
| 711 | - */ | |
| 712 | - @Override | |
| 713 | - public <K, V> Map<K, V> getMap(String name, Class<K> keysClass, Class<V> valuesClass); | |
| 714 | 90 | } |
modified driver-core/src/main/java/com/datastax/driver/core/SchemaParser.java +1 −1
| @@ -29,7 +29,7 @@ | ||
| 29 | 29 | |
| 30 | 30 | private static final Logger logger = LoggerFactory.getLogger(SchemaParser.class); |
| 31 | 31 | |
| 32 | - private static final TypeCodec.ListCodec<String> LIST_OF_TEXT_CODEC = new TypeCodec.ListCodec<String>(TypeCodec.VarcharCodec.instance); | |
| 32 | + private static final TypeCodec<List<String>> LIST_OF_TEXT_CODEC = TypeCodec.list(TypeCodec.varchar()); | |
| 33 | 33 | |
| 34 | 34 | private static final SchemaParser V2_PARSER = new V2SchemaParser(); |
| 35 | 35 | private static final SchemaParser V3_PARSER = new V3SchemaParser(); |
modified driver-core/src/main/java/com/datastax/driver/core/SettableByIndexData.java +9 −105
| @@ -38,7 +38,6 @@ | ||
| 38 | 38 | * @return this object. |
| 39 | 39 | * |
| 40 | 40 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 41 | - * @throws InvalidTypeException if value {@code i} is not of type BOOLEAN. | |
| 42 | 41 | */ |
| 43 | 42 | public T setBool(int i, boolean v); |
| 44 | 43 | |
| @@ -50,7 +49,6 @@ | ||
| 50 | 49 | * @return this object. |
| 51 | 50 | * |
| 52 | 51 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 53 | - * @throws InvalidTypeException if value {@code i} is not of type TINYINT. | |
| 54 | 52 | */ |
| 55 | 53 | public T setByte(int i, byte v); |
| 56 | 54 | |
| @@ -62,7 +60,6 @@ | ||
| 62 | 60 | * @return this object. |
| 63 | 61 | * |
| 64 | 62 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 65 | - * @throws InvalidTypeException if value {@code i} is not of type SMALLINT. | |
| 66 | 63 | */ |
| 67 | 64 | public T setShort(int i, short v); |
| 68 | 65 | |
| @@ -74,7 +71,6 @@ | ||
| 74 | 71 | * @return this object. |
| 75 | 72 | * |
| 76 | 73 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 77 | - * @throws InvalidTypeException if value {@code i} is not of type INT. | |
| 78 | 74 | */ |
| 79 | 75 | public T setInt(int i, int v); |
| 80 | 76 | |
| @@ -86,7 +82,6 @@ | ||
| 86 | 82 | * @return this object. |
| 87 | 83 | * |
| 88 | 84 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 89 | - * @throws InvalidTypeException if value {@code i} is not of type BIGINT or COUNTER. | |
| 90 | 85 | */ |
| 91 | 86 | public T setLong(int i, long v); |
| 92 | 87 | |
| @@ -98,7 +93,6 @@ | ||
| 98 | 93 | * @return this object. |
| 99 | 94 | * |
| 100 | 95 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 101 | - * @throws InvalidTypeException if value {@code i} is not of type TIMESTAMP. | |
| 102 | 96 | */ |
| 103 | 97 | public T setTimestamp(int i, Date v); |
| 104 | 98 | |
| @@ -110,7 +104,6 @@ | ||
| 110 | 104 | * @return this object. |
| 111 | 105 | * |
| 112 | 106 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 113 | - * @throws InvalidTypeException if value {@code i} is not of type DATE. | |
| 114 | 107 | */ |
| 115 | 108 | public T setDate(int i, LocalDate v); |
| 116 | 109 | |
| @@ -122,7 +115,6 @@ | ||
| 122 | 115 | * @return this object. |
| 123 | 116 | * |
| 124 | 117 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 125 | - * @throws InvalidTypeException if value {@code i} is not of type TIME. | |
| 126 | 118 | */ |
| 127 | 119 | public T setTime(int i, long v); |
| 128 | 120 | |
| @@ -134,7 +126,6 @@ | ||
| 134 | 126 | * @return this object. |
| 135 | 127 | * |
| 136 | 128 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 137 | - * @throws InvalidTypeException if value {@code i} is not of type FLOAT. | |
| 138 | 129 | */ |
| 139 | 130 | public T setFloat(int i, float v); |
| 140 | 131 | |
| @@ -146,7 +137,6 @@ | ||
| 146 | 137 | * @return this object. |
| 147 | 138 | * |
| 148 | 139 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 149 | - * @throws InvalidTypeException if value {@code i} is not of type DOUBLE. | |
| 150 | 140 | */ |
| 151 | 141 | public T setDouble(int i, double v); |
| 152 | 142 | |
| @@ -158,8 +148,6 @@ | ||
| 158 | 148 | * @return this object. |
| 159 | 149 | * |
| 160 | 150 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 161 | - * @throws InvalidTypeException if value {@code i} is of neither of the | |
| 162 | - * following types: VARCHAR, TEXT or ASCII. | |
| 163 | 151 | */ |
| 164 | 152 | public T setString(int i, String v); |
| 165 | 153 | |
| @@ -175,7 +163,6 @@ | ||
| 175 | 163 | * @return this object. |
| 176 | 164 | * |
| 177 | 165 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 178 | - * @throws InvalidTypeException if value {@code i} is not of type BLOB. | |
| 179 | 166 | */ |
| 180 | 167 | public T setBytes(int i, ByteBuffer v); |
| 181 | 168 | |
| @@ -203,7 +190,6 @@ | ||
| 203 | 190 | * @return this object. |
| 204 | 191 | * |
| 205 | 192 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 206 | - * @throws InvalidTypeException if value {@code i} is not of type VARINT. | |
| 207 | 193 | */ |
| 208 | 194 | public T setVarint(int i, BigInteger v); |
| 209 | 195 | |
| @@ -215,7 +201,6 @@ | ||
| 215 | 201 | * @return this object. |
| 216 | 202 | * |
| 217 | 203 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 218 | - * @throws InvalidTypeException if value {@code i} is not of type DECIMAL. | |
| 219 | 204 | */ |
| 220 | 205 | public T setDecimal(int i, BigDecimal v); |
| 221 | 206 | |
| @@ -227,9 +212,6 @@ | ||
| 227 | 212 | * @return this object. |
| 228 | 213 | * |
| 229 | 214 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 230 | - * @throws InvalidTypeException if value {@code i} is not of type UUID or | |
| 231 | - * TIMEUUID, or if value {@code i} is of type TIMEUUID but {@code v} is | |
| 232 | - * not a type 1 UUID. | |
| 233 | 215 | */ |
| 234 | 216 | public T setUUID(int i, UUID v); |
| 235 | 217 | |
| @@ -241,7 +223,6 @@ | ||
| 241 | 223 | * @return this object. |
| 242 | 224 | * |
| 243 | 225 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 244 | - * @throws InvalidTypeException if value {@code i} is not of type INET. | |
| 245 | 226 | */ |
| 246 | 227 | public T setInet(int i, InetAddress v); |
| 247 | 228 | |
| @@ -250,12 +231,9 @@ | ||
| 250 | 231 | * <p> |
| 251 | 232 | * Please note that {@code null} values inside collections are not supported by CQL. |
| 252 | 233 | * <p> |
| 253 | - * Note about performance: this method must perform a runtime inspection of the provided list, | |
| 254 | - * in order to guess the best codec to serialize the list elements. | |
| 255 | - * <p> | |
| 256 | - * Furthermore, if two or more codecs are available | |
| 257 | - * for the underlying CQL type ({@code list}), <em>which one will be used will depend | |
| 258 | - * on the actual object being serialized</em>. | |
| 234 | + * Note: if two or more codecs are available | |
| 235 | + * for the underlying CQL type, <em>the one that will be used will be | |
| 236 | + * the first one to be registered.</em>. | |
| 259 | 237 | * <p> |
| 260 | 238 | * For these reasons, it is generally preferable to use the more |
| 261 | 239 | * deterministic methods {@link #setList(int, List, Class)} or |
| @@ -266,9 +244,6 @@ | ||
| 266 | 244 | * @return this object. |
| 267 | 245 | * |
| 268 | 246 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 269 | - * @throws InvalidTypeException if value {@code i} is not a list type or | |
| 270 | - * if the elements of {@code v} are not of the type of the elements of | |
| 271 | - * column {@code i}. | |
| 272 | 247 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 273 | 248 | * by CQL. |
| 274 | 249 | */ |
| @@ -290,9 +265,6 @@ | ||
| 290 | 265 | * @return this object. |
| 291 | 266 | * |
| 292 | 267 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 293 | - * @throws InvalidTypeException if value {@code i} is not a list type or | |
| 294 | - * if the elements of {@code v} are not of the type of the elements of | |
| 295 | - * column {@code i}. | |
| 296 | 268 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 297 | 269 | * by CQL. |
| 298 | 270 | */ |
| @@ -314,9 +286,6 @@ | ||
| 314 | 286 | * @return this object. |
| 315 | 287 | * |
| 316 | 288 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 317 | - * @throws InvalidTypeException if value {@code i} is not a list type or | |
| 318 | - * if the elements of {@code v} are not of the type of the elements of | |
| 319 | - * column {@code i}. | |
| 320 | 289 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 321 | 290 | * by CQL. |
| 322 | 291 | */ |
| @@ -327,14 +296,9 @@ | ||
| 327 | 296 | * <p> |
| 328 | 297 | * Please note that {@code null} values are not supported inside collection by CQL. |
| 329 | 298 | * <p> |
| 330 | - * Note about performance: this method must perform a runtime inspection of the provided map, | |
| 331 | - * in order to guess the best codec to serialize the map entries. | |
| 332 | - * The result of such inspection cannot be cached and thus must be performed for each invocation | |
| 333 | - * of this method, which may incur in a performance penalty. | |
| 334 | - * <p> | |
| 335 | - * Furthermore, if two or more codecs are available | |
| 336 | - * for the underlying CQL type ({@code map}), <em>which one will be used will depend | |
| 337 | - * on the actual object being serialized</em>. | |
| 299 | + * Note: if two or more codecs are available | |
| 300 | + * for the underlying CQL type, <em>the one that will be used will be | |
| 301 | + * the first one to be registered.</em>. | |
| 338 | 302 | * <p> |
| 339 | 303 | * For these reasons, it is generally preferable to use the more |
| 340 | 304 | * deterministic methods {@link #setMap(int, Map, Class, Class)} or |
| @@ -345,9 +309,6 @@ | ||
| 345 | 309 | * @return this object. |
| 346 | 310 | * |
| 347 | 311 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 348 | - * @throws InvalidTypeException if value {@code i} is not a map type or | |
| 349 | - * if the elements (keys or values) of {@code v} are not of the type of the | |
| 350 | - * elements of column {@code i}. | |
| 351 | 312 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 352 | 313 | * by CQL. |
| 353 | 314 | */ |
| @@ -370,9 +331,6 @@ | ||
| 370 | 331 | * @return this object. |
| 371 | 332 | * |
| 372 | 333 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 373 | - * @throws InvalidTypeException if value {@code i} is not a map type or | |
| 374 | - * if the elements of {@code v} are not of the type of the elements of | |
| 375 | - * column {@code i}. | |
| 376 | 334 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 377 | 335 | * by CQL. |
| 378 | 336 | */ |
| @@ -395,9 +353,6 @@ | ||
| 395 | 353 | * @return this object. |
| 396 | 354 | * |
| 397 | 355 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 398 | - * @throws InvalidTypeException if value {@code i} is not a map type or | |
| 399 | - * if the elements of {@code v} are not of the type of the elements of | |
| 400 | - * column {@code i}. | |
| 401 | 356 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 402 | 357 | * by CQL. |
| 403 | 358 | */ |
| @@ -408,14 +363,9 @@ | ||
| 408 | 363 | * <p> |
| 409 | 364 | * Please note that {@code null} values are not supported inside collection by CQL. |
| 410 | 365 | * <p> |
| 411 | - * Note about performance: this method must perform a runtime inspection of the provided set, | |
| 412 | - * in order to guess the best codec to serialize the set elements. | |
| 413 | - * The result of such inspection cannot be cached and thus must be performed for each invocation | |
| 414 | - * of this method, which may incur in a performance penalty. | |
| 415 | - * <p> | |
| 416 | - * Furthermore, if two or more codecs are available | |
| 417 | - * for the underlying CQL type ({@code set}), <em>which one will be used will depend | |
| 418 | - * on the actual object being serialized</em>. | |
| 366 | + * Note: if two or more codecs are available | |
| 367 | + * for the underlying CQL type, <em>the one that will be used will be | |
| 368 | + * the first one to be registered.</em>. | |
| 419 | 369 | * <p> |
| 420 | 370 | * For these reasons, it is generally preferable to use the more |
| 421 | 371 | * deterministic methods {@link #setSet(int, Set, Class)} or |
| @@ -426,9 +376,6 @@ | ||
| 426 | 376 | * @return this object. |
| 427 | 377 | * |
| 428 | 378 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 429 | - * @throws InvalidTypeException if value {@code i} is not a set type or | |
| 430 | - * if the elements of {@code v} are not of the type of the elements of | |
| 431 | - * column {@code i}. | |
| 432 | 379 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 433 | 380 | * by CQL. |
| 434 | 381 | */ |
| @@ -450,9 +397,6 @@ | ||
| 450 | 397 | * @return this object. |
| 451 | 398 | * |
| 452 | 399 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 453 | - * @throws InvalidTypeException if value {@code i} is not a set type or | |
| 454 | - * if the elements of {@code v} are not of the type of the elements of | |
| 455 | - * column {@code i}. | |
| 456 | 400 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 457 | 401 | * by CQL. |
| 458 | 402 | */ |
| @@ -474,9 +418,6 @@ | ||
| 474 | 418 | * @return this object. |
| 475 | 419 | * |
| 476 | 420 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 477 | - * @throws InvalidTypeException if value {@code i} is not a set type or | |
| 478 | - * if the elements of {@code v} are not of the type of the elements of | |
| 479 | - * column {@code i}. | |
| 480 | 421 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 481 | 422 | * by CQL. |
| 482 | 423 | */ |
| @@ -490,8 +431,6 @@ | ||
| 490 | 431 | * @return this object. |
| 491 | 432 | * |
| 492 | 433 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 493 | - * @throws InvalidTypeException if value {@code i} is not a UDT value or if its definition | |
| 494 | - * does not correspond to the one of {@code v}. | |
| 495 | 434 | */ |
| 496 | 435 | public T setUDTValue(int i, UDTValue v); |
| 497 | 436 | |
| @@ -503,8 +442,6 @@ | ||
| 503 | 442 | * @return this object. |
| 504 | 443 | * |
| 505 | 444 | * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. |
| 506 | - * @throws InvalidTypeException if value {@code i} is not a tuple value or if its types | |
| 507 | - * do not correspond to the ones of {@code v}. | |
| 508 | 445 | */ |
| 509 | 446 | public T setTupleValue(int i, TupleValue v); |
| 510 | 447 | |
| @@ -519,39 +456,11 @@ | ||
| 519 | 456 | */ |
| 520 | 457 | public T setToNull(int i); |
| 521 | 458 | |
| 522 | - /** | |
| 523 | - * Sets the {@code i}th value to the provided value. | |
| 524 | - * <p> | |
| 525 | - * Note about performance: this method must perform a runtime inspection of the provided object, | |
| 526 | - * in order to guess the best codec to serialize it. | |
| 527 | - * The result of such inspection cannot be cached and thus must be performed for each invocation | |
| 528 | - * of this method, which may incur in a performance penalty. | |
| 529 | - * <p> | |
| 530 | - * Furthermore, if two or more codecs are available | |
| 531 | - * for the underlying CQL type, <em>which one will be used will depend | |
| 532 | - * on the actual object being serialized</em>. | |
| 533 | - * <p> | |
| 534 | - * For these reasons, it is generally preferable to use the more | |
| 535 | - * deterministic methods {@link #set(int, V, Class)} | |
| 536 | - * or {@link #set(int, V, TypeToken)} instead. | |
| 537 | - * | |
| 538 | - * @param i the index of the value to set. | |
| 539 | - * @param v the value to set; may be {@code null}. | |
| 540 | - * @return this object. | |
| 541 | - * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 542 | - */ | |
| 543 | - <V> T setObject(int i, V v); | |
| 544 | - | |
| 545 | 459 | /** |
| 546 | 460 | * Sets the {@code i}th value to the provided value of the provided Java class. |
| 547 | 461 | * <p> |
| 548 | 462 | * A suitable {@link TypeCodec} instance for the underlying CQL type and the provided class must |
| 549 | 463 | * have been previously registered with the {@link CodecRegistry} currently in use. |
| 550 | - * <p> | |
| 551 | - * This method should be used instead of {@link #setObject(int, Object)} in cases | |
| 552 | - * where more than one codec is registered for the same CQL type; specifying the Java class | |
| 553 | - * allows the {@link CodecRegistry} to narrow down the search and return only an exactly-matching codec (if any), | |
| 554 | - * thus avoiding any risk of ambiguity. | |
| 555 | 464 | * |
| 556 | 465 | * @param i the index of the value to set. |
| 557 | 466 | * @param v the value to set; may be {@code null}. |
| @@ -566,11 +475,6 @@ | ||
| 566 | 475 | * <p> |
| 567 | 476 | * A suitable {@link TypeCodec} instance for the underlying CQL type and the provided class must |
| 568 | 477 | * have been previously registered with the {@link CodecRegistry} currently in use. |
| 569 | - * <p> | |
| 570 | - * This method should be used instead of {@link #setObject(int, Object)} in cases | |
| 571 | - * where more than one codec is registered for the same CQL type; specifying the Java class | |
| 572 | - * allows the {@link CodecRegistry} to narrow down the search and return only an exactly-matching codec (if any), | |
| 573 | - * thus avoiding any risk of ambiguity. | |
| 574 | 478 | * |
| 575 | 479 | * @param i the index of the value to set. |
| 576 | 480 | * @param v the value to set; may be {@code null}. |
modified driver-core/src/main/java/com/datastax/driver/core/SettableByNameData.java +14 −125
| @@ -40,7 +40,6 @@ | ||
| 40 | 40 | * @return this object. |
| 41 | 41 | * |
| 42 | 42 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 43 | - * @throws InvalidTypeException if (any one occurrence of) {@code name} is not of type BOOLEAN. | |
| 44 | 43 | */ |
| 45 | 44 | public T setBool(String name, boolean v); |
| 46 | 45 | |
| @@ -54,7 +53,6 @@ | ||
| 54 | 53 | * @return this object. |
| 55 | 54 | * |
| 56 | 55 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 57 | - * @throws InvalidTypeException if (any one occurrence of) {@code name} is not of type TINYINT. | |
| 58 | 56 | */ |
| 59 | 57 | public T setByte(String name, byte v); |
| 60 | 58 | |
| @@ -68,7 +66,6 @@ | ||
| 68 | 66 | * @return this object. |
| 69 | 67 | * |
| 70 | 68 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 71 | - * @throws InvalidTypeException if (any one occurrence of) {@code name} is not of type SMALLINT. | |
| 72 | 69 | */ |
| 73 | 70 | public T setShort(String name, short v); |
| 74 | 71 | |
| @@ -82,7 +79,6 @@ | ||
| 82 | 79 | * @return this object. |
| 83 | 80 | * |
| 84 | 81 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 85 | - * @throws InvalidTypeException if (any one occurrence of) {@code name} is not of type INT. | |
| 86 | 82 | */ |
| 87 | 83 | public T setInt(String name, int v); |
| 88 | 84 | |
| @@ -96,8 +92,6 @@ | ||
| 96 | 92 | * @return this object. |
| 97 | 93 | * |
| 98 | 94 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 99 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 100 | - * not of type BIGINT or COUNTER. | |
| 101 | 95 | */ |
| 102 | 96 | public T setLong(String name, long v); |
| 103 | 97 | |
| @@ -111,8 +105,6 @@ | ||
| 111 | 105 | * @return this object. |
| 112 | 106 | * |
| 113 | 107 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 114 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 115 | - * not of type TIMESTAMP. | |
| 116 | 108 | */ |
| 117 | 109 | public T setTimestamp(String name, Date v); |
| 118 | 110 | |
| @@ -126,8 +118,6 @@ | ||
| 126 | 118 | * @return this object. |
| 127 | 119 | * |
| 128 | 120 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 129 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 130 | - * not of type DATE. | |
| 131 | 121 | */ |
| 132 | 122 | public T setDate(String name, LocalDate v); |
| 133 | 123 | |
| @@ -141,8 +131,6 @@ | ||
| 141 | 131 | * @return this object. |
| 142 | 132 | * |
| 143 | 133 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 144 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 145 | - * not of type TIME. | |
| 146 | 134 | */ |
| 147 | 135 | public T setTime(String name, long v); |
| 148 | 136 | |
| @@ -156,8 +144,6 @@ | ||
| 156 | 144 | * @return this object. |
| 157 | 145 | * |
| 158 | 146 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 159 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 160 | - * not of type FLOAT. | |
| 161 | 147 | */ |
| 162 | 148 | public T setFloat(String name, float v); |
| 163 | 149 | |
| @@ -171,8 +157,6 @@ | ||
| 171 | 157 | * @return this object. |
| 172 | 158 | * |
| 173 | 159 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 174 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 175 | - * not of type DOUBLE. | |
| 176 | 160 | */ |
| 177 | 161 | public T setDouble(String name, double v); |
| 178 | 162 | |
| @@ -186,8 +170,6 @@ | ||
| 186 | 170 | * @return this object. |
| 187 | 171 | * |
| 188 | 172 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 189 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 190 | - * of neither of the following types: VARCHAR, TEXT or ASCII. | |
| 191 | 173 | */ |
| 192 | 174 | public T setString(String name, String v); |
| 193 | 175 | |
| @@ -205,7 +187,6 @@ | ||
| 205 | 187 | * @return this object. |
| 206 | 188 | * |
| 207 | 189 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 208 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is not of type BLOB. | |
| 209 | 190 | */ |
| 210 | 191 | public T setBytes(String name, ByteBuffer v); |
| 211 | 192 | |
| @@ -237,8 +218,6 @@ | ||
| 237 | 218 | * @return this object. |
| 238 | 219 | * |
| 239 | 220 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 240 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 241 | - * not of type VARINT. | |
| 242 | 221 | */ |
| 243 | 222 | public T setVarint(String name, BigInteger v); |
| 244 | 223 | |
| @@ -252,8 +231,6 @@ | ||
| 252 | 231 | * @return this object. |
| 253 | 232 | * |
| 254 | 233 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 255 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 256 | - * not of type DECIMAL. | |
| 257 | 234 | */ |
| 258 | 235 | public T setDecimal(String name, BigDecimal v); |
| 259 | 236 | |
| @@ -267,9 +244,6 @@ | ||
| 267 | 244 | * @return this object. |
| 268 | 245 | * |
| 269 | 246 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 270 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 271 | - * not of type UUID or TIMEUUID, or if value {@code name} is of type | |
| 272 | - * TIMEUUID but {@code v} is not a type 1 UUID. | |
| 273 | 247 | */ |
| 274 | 248 | public T setUUID(String name, UUID v); |
| 275 | 249 | |
| @@ -283,8 +257,6 @@ | ||
| 283 | 257 | * @return this object. |
| 284 | 258 | * |
| 285 | 259 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 286 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 287 | - * not of type INET. | |
| 288 | 260 | */ |
| 289 | 261 | public T setInet(String name, InetAddress v); |
| 290 | 262 | |
| @@ -294,14 +266,9 @@ | ||
| 294 | 266 | * <p> |
| 295 | 267 | * Please note that {@code null} values inside collections are not supported by CQL. |
| 296 | 268 | * <p> |
| 297 | - * Note about performance: this method must perform a runtime inspection of the provided list, | |
| 298 | - * in order to guess the best codec to serialize the list elements. | |
| 299 | - * The result of such inspection cannot be cached and thus must be performed for each invocation | |
| 300 | - * of this method, which may incur in a performance penalty. | |
| 301 | - * <p> | |
| 302 | - * Furthermore, if two or more codecs are available | |
| 303 | - * for the underlying CQL type ({@code list}), <em>which one will be used will depend | |
| 304 | - * on the actual object being serialized</em>. | |
| 269 | + * Note: if two or more codecs are available | |
| 270 | + * for the underlying CQL type, <em>the one that will be used will be | |
| 271 | + * the first one to be registered.</em>. | |
| 305 | 272 | * <p> |
| 306 | 273 | * For these reasons, it is generally preferable to use the more |
| 307 | 274 | * deterministic methods {@link #setList(String, List, Class)} or |
| @@ -313,9 +280,6 @@ | ||
| 313 | 280 | * @return this object. |
| 314 | 281 | * |
| 315 | 282 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 316 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 317 | - * not a list type or if the elements of {@code v} are not of the type of | |
| 318 | - * the elements of column {@code name}. | |
| 319 | 283 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 320 | 284 | * by CQL. |
| 321 | 285 | */ |
| @@ -336,10 +300,7 @@ | ||
| 336 | 300 | * @param elementsClass the class for the elements of the list. |
| 337 | 301 | * @return this object. |
| 338 | 302 | * |
| 339 | - * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 340 | - * @throws InvalidTypeException if value {@code i} is not a list type or | |
| 341 | - * if the elements of {@code v} are not of the type of the elements of | |
| 342 | - * column {@code i}. | |
| 303 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 343 | 304 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 344 | 305 | * by CQL. |
| 345 | 306 | */ |
| @@ -360,10 +321,7 @@ | ||
| 360 | 321 | * @param elementsType the type for the elements of the list. |
| 361 | 322 | * @return this object. |
| 362 | 323 | * |
| 363 | - * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 364 | - * @throws InvalidTypeException if value {@code i} is not a list type or | |
| 365 | - * if the elements of {@code v} are not of the type of the elements of | |
| 366 | - * column {@code i}. | |
| 324 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 367 | 325 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 368 | 326 | * by CQL. |
| 369 | 327 | */ |
| @@ -375,14 +333,10 @@ | ||
| 375 | 333 | * <p> |
| 376 | 334 | * Please note that {@code null} values are not supported inside collection by CQL. |
| 377 | 335 | * <p> |
| 378 | - * Note about performance: this method must perform a runtime inspection of the provided map, | |
| 379 | - * in order to guess the best codec to serialize the map entries. | |
| 380 | - * The result of such inspection cannot be cached and thus must be performed for each invocation | |
| 381 | - * of this method, which may incur in a performance penalty. | |
| 382 | 336 | * <p> |
| 383 | - * Furthermore, if two or more codecs are available | |
| 384 | - * for the underlying CQL type ({@code map}), <em>which one will be used will depend | |
| 385 | - * on the actual object being serialized</em>. | |
| 337 | + * Note: if two or more codecs are available | |
| 338 | + * for the underlying CQL type, <em>the one that will be used will be | |
| 339 | + * the first one to be registered.</em>. | |
| 386 | 340 | * <p> |
| 387 | 341 | * For these reasons, it is generally preferable to use the more |
| 388 | 342 | * deterministic methods {@link #setMap(String, Map, Class, Class)} or |
| @@ -394,9 +348,6 @@ | ||
| 394 | 348 | * @return this object. |
| 395 | 349 | * |
| 396 | 350 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 397 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 398 | - * not a map type or if the elements (keys or values) of {@code v} are not of | |
| 399 | - * the type of the elements of column {@code name}. | |
| 400 | 351 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 401 | 352 | * by CQL. |
| 402 | 353 | */ |
| @@ -420,9 +371,6 @@ | ||
| 420 | 371 | * @return this object. |
| 421 | 372 | * |
| 422 | 373 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 423 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 424 | - * not a map type or if the elements (keys or values) of {@code v} are not of | |
| 425 | - * the type of the elements of column {@code name}. | |
| 426 | 374 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 427 | 375 | * by CQL. |
| 428 | 376 | */ |
| @@ -446,9 +394,6 @@ | ||
| 446 | 394 | * @return this object. |
| 447 | 395 | * |
| 448 | 396 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 449 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 450 | - * not a map type or if the elements (keys or values) of {@code v} are not of | |
| 451 | - * the type of the elements of column {@code name}. | |
| 452 | 397 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 453 | 398 | * by CQL. |
| 454 | 399 | */ |
| @@ -460,14 +405,9 @@ | ||
| 460 | 405 | * <p> |
| 461 | 406 | * Please note that {@code null} values are not supported inside collection by CQL. |
| 462 | 407 | * <p> |
| 463 | - * Note about performance: this method must perform a runtime inspection of the provided map, | |
| 464 | - * in order to guess the best codec to serialize the set elements. | |
| 465 | - * The result of such inspection cannot be cached and thus must be performed for each invocation | |
| 466 | - * of this method, which may incur in a performance penalty. | |
| 467 | - * <p> | |
| 468 | - * Furthermore, if two or more codecs are available | |
| 469 | - * for the underlying CQL type ({@code set}), <em>which one will be used will depend | |
| 470 | - * on the actual object being serialized</em>. | |
| 408 | + * Note: if two or more codecs are available | |
| 409 | + * for the underlying CQL type, <em>the one that will be used will be | |
| 410 | + * the first one to be registered.</em>. | |
| 471 | 411 | * <p> |
| 472 | 412 | * For these reasons, it is generally preferable to use the more |
| 473 | 413 | * deterministic methods {@link #setSet(String, Set, Class)} or |
| @@ -479,9 +419,6 @@ | ||
| 479 | 419 | * @return this object. |
| 480 | 420 | * |
| 481 | 421 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 482 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 483 | - * not a map type or if the elements of {@code v} are not of the type of | |
| 484 | - * the elements of column {@code name}. | |
| 485 | 422 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 486 | 423 | * by CQL. |
| 487 | 424 | */ |
| @@ -503,9 +440,6 @@ | ||
| 503 | 440 | * @return this object. |
| 504 | 441 | * |
| 505 | 442 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 506 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 507 | - * not a map type or if the elements (keys or values) of {@code v} are not of | |
| 508 | - * the type of the elements of column {@code name}. | |
| 509 | 443 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 510 | 444 | * by CQL. |
| 511 | 445 | */ |
| @@ -527,9 +461,6 @@ | ||
| 527 | 461 | * @return this object. |
| 528 | 462 | * |
| 529 | 463 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 530 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 531 | - * not a map type or if the elements (keys or values) of {@code v} are not of | |
| 532 | - * the type of the elements of column {@code name}. | |
| 533 | 464 | * @throws NullPointerException if {@code v} contains null values. Nulls are not supported in collections |
| 534 | 465 | * by CQL. |
| 535 | 466 | */ |
| @@ -545,11 +476,6 @@ | ||
| 545 | 476 | * @return this object. |
| 546 | 477 | * |
| 547 | 478 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 548 | - * @throws InvalidTypeException if value {@code i} is not a UDT value or if its definition | |
| 549 | - * does not correspond to the one of {@code v}. | |
| 550 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 551 | - * not a UDT value or if the definition of column {@code name} does not correspond to | |
| 552 | - * the one of {@code v}. | |
| 553 | 479 | */ |
| 554 | 480 | public T setUDTValue(String name, UDTValue v); |
| 555 | 481 | |
| @@ -563,9 +489,6 @@ | ||
| 563 | 489 | * @return this object. |
| 564 | 490 | * |
| 565 | 491 | * @throws IllegalArgumentException if {@code name} is not a valid name for this object. |
| 566 | - * @throws InvalidTypeException if (any occurrence of) {@code name} is | |
| 567 | - * not a tuple value or if the types of column {@code name} do not correspond to | |
| 568 | - * the ones of {@code v}. | |
| 569 | 492 | */ |
| 570 | 493 | public T setTupleValue(String name, TupleValue v); |
| 571 | 494 | |
| @@ -581,46 +504,18 @@ | ||
| 581 | 504 | */ |
| 582 | 505 | public T setToNull(String name); |
| 583 | 506 | |
| 584 | - /** | |
| 585 | - * Sets the value for (all occurrences of) variable {@code name} to the provided value. | |
| 586 | - * <p> | |
| 587 | - * Note about performance: this method must perform a runtime inspection of the provided object, | |
| 588 | - * in order to guess the best codec to serialize it. | |
| 589 | - * The result of such inspection cannot be cached and thus must be performed for each invocation | |
| 590 | - * of this method, which may incur in a performance penalty. | |
| 591 | - * <p> | |
| 592 | - * Furthermore, if two or more codecs are available | |
| 593 | - * for the underlying CQL type, <em>which one will be used will depend | |
| 594 | - * on the actual object being serialized</em>. | |
| 595 | - * <p> | |
| 596 | - * For these reasons, it is generally preferable to use the more | |
| 597 | - * deterministic methods {@link #set(String, V, Class)} or {@link #set(String, V, TypeToken)} instead. | |
| 598 | - * | |
| 599 | - * @param name the name of the value to set; if {@code name} is present multiple | |
| 600 | - * times, all its values are set. | |
| 601 | - * @param v the value to set; may be {@code null}. | |
| 602 | - * @return this object. | |
| 603 | - * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 604 | - */ | |
| 605 | - <V> T setObject(String name, V v); | |
| 606 | - | |
| 607 | 507 | /** |
| 608 | 508 | * Sets the value for (all occurrences of) variable {@code name} to the provided value of the provided Java class. |
| 609 | 509 | * <p> |
| 610 | 510 | * A suitable {@link TypeCodec} instance for the underlying CQL type and the provided class must |
| 611 | 511 | * have been previously registered with the {@link CodecRegistry} currently in use. |
| 612 | - * <p> | |
| 613 | - * This method should be used instead of {@link #setObject(String, Object)} in cases | |
| 614 | - * where more than one codec is registered for the same CQL type; specifying the Java class | |
| 615 | - * allows the {@link CodecRegistry} to narrow down the search and return only an exactly-matching codec (if any), | |
| 616 | - * thus avoiding any risk of ambiguity. | |
| 617 | 512 | * |
| 618 | 513 | * @param name the name of the value to set; if {@code name} is present multiple |
| 619 | 514 | * times, all its values are set. |
| 620 | 515 | * @param v the value to set; may be {@code null}. |
| 621 | 516 | * @param targetClass The Java class to convert to; must not be {@code null}; |
| 622 | 517 | * @return this object. |
| 623 | - * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 518 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 624 | 519 | */ |
| 625 | 520 | <V> T set(String name, V v, Class<V> targetClass); |
| 626 | 521 | |
| @@ -629,18 +524,13 @@ | ||
| 629 | 524 | * <p> |
| 630 | 525 | * A suitable {@link TypeCodec} instance for the underlying CQL type and the provided class must |
| 631 | 526 | * have been previously registered with the {@link CodecRegistry} currently in use. |
| 632 | - * <p> | |
| 633 | - * This method should be used instead of {@link #setObject(String, Object)} in cases | |
| 634 | - * where more than one codec is registered for the same CQL type; specifying the Java class | |
| 635 | - * allows the {@link CodecRegistry} to narrow down the search and return only an exactly-matching codec (if any), | |
| 636 | - * thus avoiding any risk of ambiguity. | |
| 637 | 527 | * |
| 638 | 528 | * @param name the name of the value to set; if {@code name} is present multiple |
| 639 | 529 | * times, all its values are set. |
| 640 | 530 | * @param v the value to set; may be {@code null}. |
| 641 | 531 | * @param targetType The Java type to convert to; must not be {@code null}; |
| 642 | 532 | * @return this object. |
| 643 | - * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 533 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 644 | 534 | */ |
| 645 | 535 | <V> T set(String name, V v, TypeToken<V> targetType); |
| 646 | 536 | |
| @@ -660,9 +550,8 @@ | ||
| 660 | 550 | * @param codec The {@link TypeCodec} to use to serialize the value; may not be {@code null}. |
| 661 | 551 | * @return this object. |
| 662 | 552 | * @throws InvalidTypeException if the given codec does not {@link TypeCodec#accepts(DataType) accept} the underlying CQL type. |
| 663 | - * @throws IndexOutOfBoundsException if {@code i} is not a valid index for this object. | |
| 553 | + * @throws IllegalArgumentException if {@code name} is not a valid name for this object. | |
| 664 | 554 | */ |
| 665 | 555 | <V> T set(String name, V v, TypeCodec<V> codec); |
| 666 | 556 | |
| 667 | - | |
| 668 | 557 | } |
modified driver-core/src/main/java/com/datastax/driver/core/Token.java +5 −5
| @@ -223,7 +223,7 @@ | ||
| 223 | 223 | |
| 224 | 224 | @Override |
| 225 | 225 | Token deserialize(ByteBuffer buffer, ProtocolVersion protocolVersion) { |
| 226 | - return new M3PToken(TypeCodec.BigintCodec.instance.deserialize(buffer, protocolVersion)); | |
| 226 | + return new M3PToken(TypeCodec.bigint().deserialize(buffer, protocolVersion)); | |
| 227 | 227 | } |
| 228 | 228 | |
| 229 | 229 | @Override |
| @@ -276,7 +276,7 @@ | ||
| 276 | 276 | |
| 277 | 277 | @Override |
| 278 | 278 | public ByteBuffer serialize(ProtocolVersion protocolVersion) { |
| 279 | - return TypeCodec.BigintCodec.instance.serialize(value, protocolVersion); | |
| 279 | + return TypeCodec.bigint().serialize(value, protocolVersion); | |
| 280 | 280 | } |
| 281 | 281 | |
| 282 | 282 | @Override |
| @@ -483,7 +483,7 @@ | ||
| 483 | 483 | |
| 484 | 484 | @Override |
| 485 | 485 | public ByteBuffer serialize(ProtocolVersion protocolVersion) { |
| 486 | - return TypeCodec.BlobCodec.instance.serialize(value, protocolVersion); | |
| 486 | + return TypeCodec.blob().serialize(value, protocolVersion); | |
| 487 | 487 | } |
| 488 | 488 | |
| 489 | 489 | @Override |
| @@ -552,7 +552,7 @@ | ||
| 552 | 552 | |
| 553 | 553 | @Override |
| 554 | 554 | Token deserialize(ByteBuffer buffer, ProtocolVersion protocolVersion) { |
| 555 | - return new RPToken(TypeCodec.VarintCodec.instance.deserialize(buffer, protocolVersion)); | |
| 555 | + return new RPToken(TypeCodec.varint().deserialize(buffer, protocolVersion)); | |
| 556 | 556 | } |
| 557 | 557 | |
| 558 | 558 | @Override |
| @@ -604,7 +604,7 @@ | ||
| 604 | 604 | |
| 605 | 605 | @Override |
| 606 | 606 | public ByteBuffer serialize(ProtocolVersion protocolVersion) { |
| 607 | - return TypeCodec.VarintCodec.instance.serialize(value, protocolVersion); | |
| 607 | + return TypeCodec.varint().serialize(value, protocolVersion); | |
| 608 | 608 | } |
| 609 | 609 | |
| 610 | 610 | @Override |
modified driver-core/src/main/java/com/datastax/driver/core/TypeCodec.java +549 −496
| @@ -29,7 +29,6 @@ | ||
| 29 | 29 | import java.util.concurrent.TimeUnit; |
| 30 | 30 | import java.util.regex.Pattern; |
| 31 | 31 | |
| 32 | -import com.google.common.base.Objects; | |
| 33 | 32 | import com.google.common.collect.ImmutableMap; |
| 34 | 33 | import com.google.common.reflect.TypeToken; |
| 35 | 34 | |
| @@ -53,9 +52,9 @@ | ||
| 53 | 52 | * Two methods handle the serialization and deserialization of Java types into |
| 54 | 53 | * CQL types according to the native protocol specifications: |
| 55 | 54 | * <ol> |
| 56 | - * <li>{@link #serialize(Object,ProtocolVersion)}: used to serialize from the codec's Java type to a | |
| 55 | + * <li>{@link #serialize(Object, ProtocolVersion)}: used to serialize from the codec's Java type to a | |
| 57 | 56 | * {@link ByteBuffer} instance corresponding to the codec's CQL type;</li> |
| 58 | - * <li>{@link #deserialize(ByteBuffer,ProtocolVersion)}: used to deserialize a {@link ByteBuffer} instance | |
| 57 | + * <li>{@link #deserialize(ByteBuffer, ProtocolVersion)}: used to deserialize a {@link ByteBuffer} instance | |
| 59 | 58 | * corresponding to the codec's CQL type to the codec's Java type.</li> |
| 60 | 59 | * </ol> |
| 61 | 60 | * |
| @@ -99,11 +98,12 @@ | ||
| 99 | 98 | * <li>TypeCodec implementations that wish to handle Java primitive types <em>must</em> be instantiated with |
| 100 | 99 | * the wrapper Java class instead, and implement the appropriate interface (see {@link BooleanCodec} |
| 101 | 100 | * for an example).</li> |
| 101 | + * <li>TypeCodec implementations should not consume {@link ByteBuffer} instances by performing read operations | |
| 102 | + * that modify their current position; if necessary, codecs should {@link ByteBuffer#duplicate()} duplicate} them.</li> | |
| 102 | 103 | * </ol> |
| 103 | 104 | * |
| 104 | 105 | * @param <T> The codec's Java type |
| 105 | 106 | */ |
| 106 | -@SuppressWarnings("all") | |
| 107 | 107 | public abstract class TypeCodec<T> { |
| 108 | 108 | |
| 109 | 109 | private static final Map<TypeToken<?>, TypeToken<?>> primitiveToWrapperMap = ImmutableMap.<TypeToken<?>, TypeToken<?>>builder() |
| @@ -118,9 +118,272 @@ | ||
| 118 | 118 | .build(); |
| 119 | 119 | |
| 120 | 120 | /** |
| 121 | - * The CQL keyword {@code NULL}. | |
| 122 | - */ | |
| 123 | - private static final String NULL = "NULL"; | |
| 121 | + * Return the default codec for the CQL type {@code boolean}. | |
| 122 | + * The returned codec maps the CQL type {@code boolean} into the Java type {@link Boolean}. | |
| 123 | + * The returned instance is a singleton. | |
| 124 | + * @return the default codec for CQL type {@code boolean}. | |
| 125 | + **/ | |
| 126 | + public static PrimitiveBooleanCodec cboolean() { | |
| 127 | + return BooleanCodec.instance; | |
| 128 | + } | |
| 129 | + | |
| 130 | + /** | |
| 131 | + * Return the default codec for the CQL type {@code tinyint}. | |
| 132 | + * The returned codec maps the CQL type {@code tinyint} into the Java type {@link Byte}. | |
| 133 | + * The returned instance is a singleton. | |
| 134 | + * @return the default codec for CQL type {@code tinyint}. | |
| 135 | + **/ | |
| 136 | + public static PrimitiveByteCodec tinyInt() { | |
| 137 | + return TinyIntCodec.instance; | |
| 138 | + } | |
| 139 | + | |
| 140 | + /** | |
| 141 | + * Return the default codec for the CQL type {@code smallint}. | |
| 142 | + * The returned codec maps the CQL type {@code smallint} into the Java type {@link Short}. | |
| 143 | + * The returned instance is a singleton. | |
| 144 | + * @return the default codec for CQL type {@code smallint}. | |
| 145 | + **/ | |
| 146 | + public static PrimitiveShortCodec smallInt() { | |
| 147 | + return SmallIntCodec.instance; | |
| 148 | + } | |
| 149 | + | |
| 150 | + /** | |
| 151 | + * Return the default codec for the CQL type {@code int}. | |
| 152 | + * The returned codec maps the CQL type {@code int} into the Java type {@link Integer}. | |
| 153 | + * The returned instance is a singleton. | |
| 154 | + * @return the default codec for CQL type {@code int}. | |
| 155 | + **/ | |
| 156 | + public static PrimitiveIntCodec cint() { | |
| 157 | + return IntCodec.instance; | |
| 158 | + } | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * Return the default codec for the CQL type {@code bigint}. | |
| 162 | + * The returned codec maps the CQL type {@code bigint} into the Java type {@link Long}. | |
| 163 | + * The returned instance is a singleton. | |
| 164 | + * @return the default codec for CQL type {@code bigint}. | |
| 165 | + **/ | |
| 166 | + public static PrimitiveLongCodec bigint() { | |
| 167 | + return BigintCodec.instance; | |
| 168 | + } | |
| 169 | + | |
| 170 | + /** | |
| 171 | + * Return the default codec for the CQL type {@code counter}. | |
| 172 | + * The returned codec maps the CQL type {@code counter} into the Java type {@link Long}. | |
| 173 | + * The returned instance is a singleton. | |
| 174 | + * @return the default codec for CQL type {@code counter}. | |
| 175 | + **/ | |
| 176 | + public static PrimitiveLongCodec counter() { | |
| 177 | + return CounterCodec.instance; | |
| 178 | + } | |
| 179 | + | |
| 180 | + /** | |
| 181 | + * Return the default codec for the CQL type {@code float}. | |
| 182 | + * The returned codec maps the CQL type {@code float} into the Java type {@link Float}. | |
| 183 | + * The returned instance is a singleton. | |
| 184 | + * @return the default codec for CQL type {@code float}. | |
| 185 | + **/ | |
| 186 | + public static PrimitiveFloatCodec cfloat() { | |
| 187 | + return FloatCodec.instance; | |
| 188 | + } | |
| 189 | + | |
| 190 | + /** | |
| 191 | + * Return the default codec for the CQL type {@code double}. | |
| 192 | + * The returned codec maps the CQL type {@code double} into the Java type {@link Double}. | |
| 193 | + * The returned instance is a singleton. | |
| 194 | + * @return the default codec for CQL type {@code double}. | |
| 195 | + **/ | |
| 196 | + public static PrimitiveDoubleCodec cdouble() { | |
| 197 | + return DoubleCodec.instance; | |
| 198 | + } | |
| 199 | + | |
| 200 | + /** | |
| 201 | + * Return the default codec for the CQL type {@code varint}. | |
| 202 | + * The returned codec maps the CQL type {@code varint} into the Java type {@link BigInteger}. | |
| 203 | + * The returned instance is a singleton. | |
| 204 | + * @return the default codec for CQL type {@code varint}. | |
| 205 | + **/ | |
| 206 | + public static TypeCodec<BigInteger> varint() { | |
| 207 | + return VarintCodec.instance; | |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 211 | + * Return the default codec for the CQL type {@code decimal}. | |
| 212 | + * The returned codec maps the CQL type {@code decimal} into the Java type {@link BigDecimal}. | |
| 213 | + * The returned instance is a singleton. | |
| 214 | + * @return the default codec for CQL type {@code decimal}. | |
| 215 | + **/ | |
| 216 | + public static TypeCodec<BigDecimal> decimal() { | |
| 217 | + return DecimalCodec.instance; | |
| 218 | + } | |
| 219 | + | |
| 220 | + /** | |
| 221 | + * Return the default codec for the CQL type {@code ascii}. | |
| 222 | + * The returned codec maps the CQL type {@code ascii} into the Java type {@link String}. | |
| 223 | + * The returned instance is a singleton. | |
| 224 | + * @return the default codec for CQL type {@code ascii}. | |
| 225 | + **/ | |
| 226 | + public static TypeCodec<String> ascii() { | |
| 227 | + return AsciiCodec.instance; | |
| 228 | + } | |
| 229 | + | |
| 230 | + /** | |
| 231 | + * Return the default codec for the CQL type {@code varchar}. | |
| 232 | + * The returned codec maps the CQL type {@code varchar} into the Java type {@link String}. | |
| 233 | + * The returned instance is a singleton. | |
| 234 | + * @return the default codec for CQL type {@code varchar}. | |
| 235 | + **/ | |
| 236 | + public static TypeCodec<String> varchar() { | |
| 237 | + return VarcharCodec.instance; | |
| 238 | + } | |
| 239 | + | |
| 240 | + /** | |
| 241 | + * Return the default codec for the CQL type {@code blob}. | |
| 242 | + * The returned codec maps the CQL type {@code blob} into the Java type {@link ByteBuffer}. | |
| 243 | + * The returned instance is a singleton. | |
| 244 | + * @return the default codec for CQL type {@code blob}. | |
| 245 | + **/ | |
| 246 | + public static TypeCodec<ByteBuffer> blob() { | |
| 247 | + return BlobCodec.instance; | |
| 248 | + } | |
| 249 | + | |
| 250 | + /** | |
| 251 | + * Return the default codec for the CQL type {@code date}. | |
| 252 | + * The returned codec maps the CQL type {@code date} into the Java type {@link LocalDate}. | |
| 253 | + * The returned instance is a singleton. | |
| 254 | + * @return the default codec for CQL type {@code date}. | |
| 255 | + **/ | |
| 256 | + public static TypeCodec<LocalDate> date() { | |
| 257 | + return DateCodec.instance; | |
| 258 | + } | |
| 259 | + | |
| 260 | + /** | |
| 261 | + * Return the default codec for the CQL type {@code time}. | |
| 262 | + * The returned codec maps the CQL type {@code time} into the Java type {@link Long}. | |
| 263 | + * The returned instance is a singleton. | |
| 264 | + * @return the default codec for CQL type {@code time}. | |
| 265 | + **/ | |
| 266 | + public static TypeCodec<Long> time() { | |
| 267 | + return TimeCodec.instance; | |
| 268 | + } | |
| 269 | + | |
| 270 | + /** | |
| 271 | + * Return the default codec for the CQL type {@code timestamp}. | |
| 272 | + * The returned codec maps the CQL type {@code timestamp} into the Java type {@link Date}. | |
| 273 | + * The returned instance is a singleton. | |
| 274 | + * @return the default codec for CQL type {@code timestamp}. | |
| 275 | + **/ | |
| 276 | + public static TypeCodec<Date> timestamp() { | |
| 277 | + return TimestampCodec.instance; | |
| 278 | + } | |
| 279 | + | |
| 280 | + /** | |
| 281 | + * Return the default codec for the CQL type {@code uuid}. | |
| 282 | + * The returned codec maps the CQL type {@code uuid} into the Java type {@link UUID}. | |
| 283 | + * The returned instance is a singleton. | |
| 284 | + * @return the default codec for CQL type {@code uuid}. | |
| 285 | + **/ | |
| 286 | + public static TypeCodec<UUID> uuid() { | |
| 287 | + return UUIDCodec.instance; | |
| 288 | + } | |
| 289 | + | |
| 290 | + /** | |
| 291 | + * Return the default codec for the CQL type {@code timeuuid}. | |
| 292 | + * The returned codec maps the CQL type {@code timeuuid} into the Java type {@link UUID}. | |
| 293 | + * The returned instance is a singleton. | |
| 294 | + * @return the default codec for CQL type {@code timeuuid}. | |
| 295 | + **/ | |
| 296 | + public static TypeCodec<UUID> timeUUID() { | |
| 297 | + return TimeUUIDCodec.instance; | |
| 298 | + } | |
| 299 | + | |
| 300 | + /** | |
| 301 | + * Return the default codec for the CQL type {@code inet}. | |
| 302 | + * The returned codec maps the CQL type {@code inet} into the Java type {@link InetAddress}. | |
| 303 | + * The returned instance is a singleton. | |
| 304 | + * @return the default codec for CQL type {@code inet}. | |
| 305 | + **/ | |
| 306 | + public static TypeCodec<InetAddress> inet() { | |
| 307 | + return InetCodec.instance; | |
| 308 | + } | |
| 309 | + | |
| 310 | + /** | |
| 311 | + * Return a newly-created codec for the CQL type {@code list} whose element type | |
| 312 | + * is determined by the given element codec. | |
| 313 | + * The returned codec maps the CQL type {@code list} into the Java type {@link List}. | |
| 314 | + * This method does not cache returned instances and returns a newly-allocated object | |
| 315 | + * at each invocation. | |
| 316 | + * @param elementCodec the codec that will handle elements of this list. | |
| 317 | + * @return A newly-created codec for CQL type {@code list}. | |
| 318 | + **/ | |
| 319 | + public static <T> TypeCodec<List<T>> list(TypeCodec<T> elementCodec) { | |
| 320 | + return new ListCodec<T>(elementCodec); | |
| 321 | + } | |
| 322 | + | |
| 323 | + /** | |
| 324 | + * Return a newly-created codec for the CQL type {@code set} whose element type | |
| 325 | + * is determined by the given element codec. | |
| 326 | + * The returned codec maps the CQL type {@code set} into the Java type {@link Set}. | |
| 327 | + * This method does not cache returned instances and returns a newly-allocated object | |
| 328 | + * at each invocation. | |
| 329 | + * @param elementCodec the codec that will handle elements of this set. | |
| 330 | + * @return A newly-created codec for CQL type {@code set}. | |
| 331 | + **/ | |
| 332 | + public static <T> TypeCodec<Set<T>> set(TypeCodec<T> elementCodec) { | |
| 333 | + return new SetCodec<T>(elementCodec); | |
| 334 | + } | |
| 335 | + | |
| 336 | + /** | |
| 337 | + * Return a newly-created codec for the CQL type {@code map} whose key type | |
| 338 | + * and value type are determined by the given codecs. | |
| 339 | + * The returned codec maps the CQL type {@code map} into the Java type {@link Map}. | |
| 340 | + * This method does not cache returned instances and returns a newly-allocated object | |
| 341 | + * at each invocation. | |
| 342 | + * @param keyCodec the codec that will handle keys of this map. | |
| 343 | + * @param valueCodec the codec that will handle values of this map. | |
| 344 | + * @return A newly-created codec for CQL type {@code map}. | |
| 345 | + **/ | |
| 346 | + public static <K, V> TypeCodec<Map<K, V>> map(TypeCodec<K> keyCodec, TypeCodec<V> valueCodec) { | |
| 347 | + return new MapCodec<K, V>(keyCodec, valueCodec); | |
| 348 | + } | |
| 349 | + | |
| 350 | + /** | |
| 351 | + * Return a newly-created codec for the given user-defined CQL type. | |
| 352 | + * The returned codec maps the user-defined type into the Java type {@link UDTValue}. | |
| 353 | + * This method does not cache returned instances and returns a newly-allocated object | |
| 354 | + * at each invocation. | |
| 355 | + * @param type the user-defined type this codec should handle. | |
| 356 | + * @return A newly-created codec for the given user-defined CQL type. | |
| 357 | + **/ | |
| 358 | + public static TypeCodec<UDTValue> userType(UserType type) { | |
| 359 | + return new UDTCodec(type); | |
| 360 | + } | |
| 361 | + | |
| 362 | + /** | |
| 363 | + * Return a newly-created codec for the given CQL tuple type. | |
| 364 | + * The returned codec maps the tuple type into the Java type {@link TupleValue}. | |
| 365 | + * This method does not cache returned instances and returns a newly-allocated object | |
| 366 | + * at each invocation. | |
| 367 | + * @param type the tuple type this codec should handle. | |
| 368 | + * @return A newly-created codec for the given CQL tuple type. | |
| 369 | + **/ | |
| 370 | + public static TypeCodec<TupleValue> tuple(TupleType type) { | |
| 371 | + return new TupleCodec(type); | |
| 372 | + } | |
| 373 | + | |
| 374 | + /** | |
| 375 | + * Return a newly-created codec for the given CQL custom type. | |
| 376 | + * The returned codec maps the custom type into the Java type {@link ByteBuffer}. | |
| 377 | + * This method provides a (very lightweight) support for Cassandra | |
| 378 | + * types that do not have a CQL equivalent. | |
| 379 | + * This method does not cache returned instances and returns a newly-allocated object | |
| 380 | + * at each invocation. | |
| 381 | + * @param type the custom type this codec should handle. | |
| 382 | + * @return A newly-created codec for the given CQL custom type. | |
| 383 | + **/ | |
| 384 | + public static TypeCodec<ByteBuffer> custom(CustomType type) { | |
| 385 | + return new CustomCodec(type); | |
| 386 | + } | |
| 124 | 387 | |
| 125 | 388 | protected final TypeToken<T> javaType; |
| 126 | 389 | |
| @@ -192,9 +455,13 @@ | ||
| 192 | 455 | * <li>Null or empty buffers should be gracefully handled and no exception should be raised; |
| 193 | 456 | * these should be considered as the equivalent of a NULL CQL value and, in most cases, should |
| 194 | 457 | * map to {@code null} or a default value for the corresponding Java type, if applicable;</li> |
| 195 | - * <li>Codecs for CQL collection types should return unmodifiable collections;</li> | |
| 458 | + * <li>Codecs for CQL collection types should clearly document whether they return immutable collections or not | |
| 459 | + * (note that the driver's default collection codecs return <em>mutable</em> collections);</li> | |
| 196 | 460 | * <li>Codecs for CQL collection types should avoid returning {@code null}; |
| 197 | - * they should return empty collections instead.</li> | |
| 461 | + * they should return empty collections instead (the driver's default collection codecs all comply with this rule).</li> | |
| 462 | + * <li>The provided {@link ByteBuffer} should never be consumed by read operations that | |
| 463 | + * modify its current position; if necessary, | |
| 464 | + * {@link ByteBuffer#duplicate()} duplicate} it before consuming.</li> | |
| 198 | 465 | * </ol> |
| 199 | 466 | * |
| 200 | 467 | * @param bytes A {@link ByteBuffer} instance containing the serialized form of T; |
| @@ -248,7 +515,7 @@ | ||
| 248 | 515 | */ |
| 249 | 516 | public boolean accepts(TypeToken javaType) { |
| 250 | 517 | checkNotNull(javaType); |
| 251 | - if(javaType.isPrimitive()) { | |
| 518 | + if (javaType.isPrimitive()) { | |
| 252 | 519 | javaType = primitiveToWrapperMap.get(javaType); |
| 253 | 520 | } |
| 254 | 521 | // TODO accept generics type covariance, i.e. a codec for List<Number> should accept List<Integer> |
| @@ -301,119 +568,203 @@ | ||
| 301 | 568 | return accepts(TypeToken.of(value.getClass())); |
| 302 | 569 | } |
| 303 | 570 | |
| 304 | - @Override | |
| 305 | - public boolean equals(Object o) { | |
| 306 | - if (this == o) | |
| 307 | - return true; | |
| 308 | - if (!(o instanceof TypeCodec)) | |
| 309 | - return false; | |
| 310 | - TypeCodec<?> typeCodec = (TypeCodec<?>)o; | |
| 311 | - return Objects.equal(javaType, typeCodec.javaType) && Objects.equal(cqlType, typeCodec.cqlType); | |
| 312 | - } | |
| 313 | - | |
| 314 | - @Override | |
| 315 | - public int hashCode() { | |
| 316 | - return Objects.hashCode(javaType, cqlType); | |
| 317 | - } | |
| 318 | - | |
| 319 | 571 | @Override |
| 320 | 572 | public String toString() { |
| 321 | 573 | return String.format("%s [%s <-> %s]", this.getClass().getSimpleName(), cqlType, javaType); |
| 322 | 574 | } |
| 323 | 575 | |
| 324 | 576 | /** |
| 325 | - * An interface for codecs that are capable of handling primitive booleans, | |
| 577 | + * A codec that is capable of handling primitive booleans, | |
| 326 | 578 | * thus avoiding the overhead of boxing and unboxing such primitives. |
| 327 | 579 | */ |
| 328 | - public interface PrimitiveBooleanCodec { | |
| 580 | + public static abstract class PrimitiveBooleanCodec extends TypeCodec<Boolean> { | |
| 581 | + | |
| 582 | + protected PrimitiveBooleanCodec(DataType cqlType) { | |
| 583 | + super(cqlType, Boolean.class); | |
| 584 | + } | |
| 585 | + | |
| 586 | + public abstract ByteBuffer serializeNoBoxing(boolean v, ProtocolVersion protocolVersion); | |
| 329 | 587 | |
| 330 | - ByteBuffer serializeNoBoxing(boolean v, ProtocolVersion protocolVersion); | |
| 331 | - boolean deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 588 | + public abstract boolean deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 332 | 589 | |
| 590 | + @Override | |
| 591 | + public ByteBuffer serialize(Boolean value, ProtocolVersion protocolVersion) { | |
| 592 | + return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 593 | + } | |
| 594 | + | |
| 595 | + @Override | |
| 596 | + public Boolean deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 597 | + return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 598 | + } | |
| 333 | 599 | } |
| 334 | 600 | |
| 335 | 601 | /** |
| 336 | - * An interface for codecs that are capable of handling primitive bytes, | |
| 602 | + * A codec that is capable of handling primitive bytes, | |
| 337 | 603 | * thus avoiding the overhead of boxing and unboxing such primitives. |
| 338 | 604 | */ |
| 339 | - public interface PrimitiveByteCodec { | |
| 605 | + public static abstract class PrimitiveByteCodec extends TypeCodec<Byte> { | |
| 606 | + | |
| 607 | + protected PrimitiveByteCodec(DataType cqlType) { | |
| 608 | + super(cqlType, Byte.class); | |
| 609 | + } | |
| 340 | 610 | |
| 341 | - ByteBuffer serializeNoBoxing(byte v, ProtocolVersion protocolVersion); | |
| 342 | - byte deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 611 | + public abstract ByteBuffer serializeNoBoxing(byte v, ProtocolVersion protocolVersion); | |
| 612 | + | |
| 613 | + public abstract byte deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 614 | + | |
| 615 | + @Override | |
| 616 | + public ByteBuffer serialize(Byte value, ProtocolVersion protocolVersion) { | |
| 617 | + return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 618 | + } | |
| 343 | 619 | |
| 620 | + @Override | |
| 621 | + public Byte deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 622 | + return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 623 | + } | |
| 344 | 624 | } |
| 345 | 625 | |
| 346 | 626 | /** |
| 347 | - * An interface for codecs that are capable of handling primitive shorts, | |
| 627 | + * A codec that is capable of handling primitive shorts, | |
| 348 | 628 | * thus avoiding the overhead of boxing and unboxing such primitives. |
| 349 | 629 | */ |
| 350 | - public interface PrimitiveShortCodec { | |
| 630 | + public static abstract class PrimitiveShortCodec extends TypeCodec<Short>{ | |
| 631 | + | |
| 632 | + protected PrimitiveShortCodec(DataType cqlType) { | |
| 633 | + super(cqlType, Short.class); | |
| 634 | + } | |
| 635 | + | |
| 636 | + public abstract ByteBuffer serializeNoBoxing(short v, ProtocolVersion protocolVersion); | |
| 637 | + | |
| 638 | + public abstract short deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 351 | 639 | |
| 352 | - ByteBuffer serializeNoBoxing(short v, ProtocolVersion protocolVersion); | |
| 353 | - short deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 640 | + @Override | |
| 641 | + public ByteBuffer serialize(Short value, ProtocolVersion protocolVersion) { | |
| 642 | + return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 643 | + } | |
| 354 | 644 | |
| 645 | + @Override | |
| 646 | + public Short deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 647 | + return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 648 | + } | |
| 355 | 649 | } |
| 356 | 650 | |
| 357 | 651 | /** |
| 358 | - * An interface for codecs that are capable of handling primitive ints, | |
| 652 | + * A codec that is capable of handling primitive ints, | |
| 359 | 653 | * thus avoiding the overhead of boxing and unboxing such primitives. |
| 360 | 654 | */ |
| 361 | - public interface PrimitiveIntCodec { | |
| 655 | + public static abstract class PrimitiveIntCodec extends TypeCodec<Integer> { | |
| 362 | 656 | |
| 363 | - ByteBuffer serializeNoBoxing(int v, ProtocolVersion protocolVersion); | |
| 364 | - int deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 657 | + protected PrimitiveIntCodec(DataType cqlType) { | |
| 658 | + super(cqlType, Integer.class); | |
| 659 | + } | |
| 660 | + | |
| 661 | + public abstract ByteBuffer serializeNoBoxing(int v, ProtocolVersion protocolVersion); | |
| 662 | + | |
| 663 | + public abstract int deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 664 | + | |
| 665 | + @Override | |
| 666 | + public ByteBuffer serialize(Integer value, ProtocolVersion protocolVersion) { | |
| 667 | + return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 668 | + } | |
| 365 | 669 | |
| 670 | + @Override | |
| 671 | + public Integer deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 672 | + return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 673 | + } | |
| 366 | 674 | } |
| 367 | 675 | |
| 368 | 676 | /** |
| 369 | - * An interface for codecs that are capable of handling primitive longs, | |
| 677 | + * A codec that is capable of handling primitive longs, | |
| 370 | 678 | * thus avoiding the overhead of boxing and unboxing such primitives. |
| 371 | 679 | */ |
| 372 | - public interface PrimitiveLongCodec { | |
| 680 | + public static abstract class PrimitiveLongCodec extends TypeCodec<Long> { | |
| 373 | 681 | |
| 374 | - ByteBuffer serializeNoBoxing(long v, ProtocolVersion protocolVersion); | |
| 375 | - long deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 682 | + protected PrimitiveLongCodec(DataType cqlType) { | |
| 683 | + super(cqlType, Long.class); | |
| 684 | + } | |
| 685 | + | |
| 686 | + public abstract ByteBuffer serializeNoBoxing(long v, ProtocolVersion protocolVersion); | |
| 687 | + | |
| 688 | + public abstract long deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 689 | + | |
| 690 | + @Override | |
| 691 | + public ByteBuffer serialize(Long value, ProtocolVersion protocolVersion) { | |
| 692 | + return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 693 | + } | |
| 376 | 694 | |
| 695 | + @Override | |
| 696 | + public Long deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 697 | + return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 698 | + } | |
| 377 | 699 | } |
| 378 | 700 | |
| 379 | 701 | /** |
| 380 | - * An interface for codecs that are capable of handling primitive floats, | |
| 702 | + * A codec that is capable of handling primitive floats, | |
| 381 | 703 | * thus avoiding the overhead of boxing and unboxing such primitives. |
| 382 | 704 | */ |
| 383 | - public interface PrimitiveFloatCodec { | |
| 705 | + public static abstract class PrimitiveFloatCodec extends TypeCodec<Float> { | |
| 706 | + | |
| 707 | + protected PrimitiveFloatCodec(DataType cqlType) { | |
| 708 | + super(cqlType, Float.class); | |
| 709 | + } | |
| 710 | + | |
| 711 | + public abstract ByteBuffer serializeNoBoxing(float v, ProtocolVersion protocolVersion); | |
| 712 | + | |
| 713 | + public abstract float deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 384 | 714 | |
| 385 | - ByteBuffer serializeNoBoxing(float v, ProtocolVersion protocolVersion); | |
| 386 | - float deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 715 | + @Override | |
| 716 | + public ByteBuffer serialize(Float value, ProtocolVersion protocolVersion) { | |
| 717 | + return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 718 | + } | |
| 387 | 719 | |
| 720 | + @Override | |
| 721 | + public Float deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 722 | + return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 723 | + } | |
| 388 | 724 | } |
| 389 | 725 | |
| 390 | 726 | /** |
| 391 | - * An interface for codecs that are capable of handling primitive doubles, | |
| 727 | + * A codec that is capable of handling primitive doubles, | |
| 392 | 728 | * thus avoiding the overhead of boxing and unboxing such primitives. |
| 393 | 729 | */ |
| 394 | - public interface PrimitiveDoubleCodec { | |
| 730 | + public static abstract class PrimitiveDoubleCodec extends TypeCodec<Double> { | |
| 731 | + | |
| 732 | + protected PrimitiveDoubleCodec(DataType cqlType) { | |
| 733 | + super(cqlType, Double.class); | |
| 734 | + } | |
| 735 | + | |
| 736 | + public abstract ByteBuffer serializeNoBoxing(double v, ProtocolVersion protocolVersion); | |
| 395 | 737 | |
| 396 | - ByteBuffer serializeNoBoxing(double v, ProtocolVersion protocolVersion); | |
| 397 | - double deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 738 | + public abstract double deserializeNoBoxing(ByteBuffer v, ProtocolVersion protocolVersion); | |
| 398 | 739 | |
| 740 | + @Override | |
| 741 | + public ByteBuffer serialize(Double value, ProtocolVersion protocolVersion) { | |
| 742 | + return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 743 | + } | |
| 744 | + | |
| 745 | + @Override | |
| 746 | + public Double deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 747 | + return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 748 | + } | |
| 399 | 749 | } |
| 400 | 750 | |
| 401 | 751 | /** |
| 402 | 752 | * Base class for codecs handling CQL string types such as {@link DataType#varchar()}, |
| 403 | 753 | * {@link DataType#text()} or {@link DataType#ascii()}. |
| 404 | 754 | */ |
| 405 | - public static abstract class StringCodec extends TypeCodec<String> { | |
| 755 | + private static abstract class StringCodec extends TypeCodec<String> { | |
| 406 | 756 | |
| 407 | 757 | private final Charset charset; |
| 408 | 758 | |
| 409 | - public StringCodec(DataType cqlType, Charset charset) { | |
| 759 | + private StringCodec(DataType cqlType, Charset charset) { | |
| 410 | 760 | super(cqlType, String.class); |
| 411 | 761 | this.charset = charset; |
| 412 | 762 | } |
| 413 | 763 | |
| 414 | 764 | @Override |
| 415 | 765 | public String parse(String value) { |
| 416 | - if (value == null || value.isEmpty() || value.equals(NULL)) return null; | |
| 766 | + if (value == null || value.isEmpty() || value.equals("NULL")) | |
| 767 | + return null; | |
| 417 | 768 | if (value.charAt(0) != '\'' || value.charAt(value.length() - 1) != '\'') |
| 418 | 769 | throw new InvalidTypeException("text or varchar values must be enclosed by single quotes"); |
| 419 | 770 | |
| @@ -423,13 +774,13 @@ | ||
| 423 | 774 | @Override |
| 424 | 775 | public String format(String value) { |
| 425 | 776 | if (value == null) |
| 426 | - return NULL; | |
| 777 | + return "NULL"; | |
| 427 | 778 | return '\'' + replace(value, '\'', "''") + '\''; |
| 428 | 779 | } |
| 429 | 780 | |
| 430 | 781 | // Simple method to replace a single character. String.replace is a bit too |
| 431 | 782 | // inefficient (see JAVA-67) |
| 432 | - static String replace(String text, char search, String replacement) { | |
| 783 | + private static String replace(String text, char search, String replacement) { | |
| 433 | 784 | if (text == null || text.isEmpty()) |
| 434 | 785 | return text; |
| 435 | 786 | |
| @@ -474,7 +825,7 @@ | ||
| 474 | 825 | public String deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 475 | 826 | if (bytes == null) |
| 476 | 827 | return null; |
| 477 | - if(bytes.remaining() == 0) | |
| 828 | + if (bytes.remaining() == 0) | |
| 478 | 829 | return ""; |
| 479 | 830 | return new String(Bytes.getArray(bytes), charset); |
| 480 | 831 | } |
| @@ -485,12 +836,12 @@ | ||
| 485 | 836 | * Note that this codec also handles {@link DataType#text()}, which is merely |
| 486 | 837 | * an alias for {@link DataType#varchar()}. |
| 487 | 838 | */ |
| 488 | - public static class VarcharCodec extends StringCodec { | |
| 839 | + private static class VarcharCodec extends StringCodec { | |
| 489 | 840 | |
| 490 | - public static final VarcharCodec instance = new VarcharCodec(); | |
| 841 | + private static final VarcharCodec instance = new VarcharCodec(); | |
| 491 | 842 | |
| 492 | 843 | private VarcharCodec() { |
| 493 | - super(varchar(), Charset.forName("UTF-8")); | |
| 844 | + super(DataType.varchar(), Charset.forName("UTF-8")); | |
| 494 | 845 | } |
| 495 | 846 | |
| 496 | 847 | } |
| @@ -498,22 +849,19 @@ | ||
| 498 | 849 | /** |
| 499 | 850 | * This codec maps a CQL {@link DataType#ascii()} to a Java {@link String}. |
| 500 | 851 | */ |
| 501 | - public static class AsciiCodec extends StringCodec { | |
| 852 | + private static class AsciiCodec extends StringCodec { | |
| 502 | 853 | |
| 503 | - public static final AsciiCodec instance = new AsciiCodec(); | |
| 854 | + private static final AsciiCodec instance = new AsciiCodec(); | |
| 504 | 855 | |
| 505 | 856 | private static final Pattern ASCII_PATTERN = Pattern.compile("^\\p{ASCII}*$"); |
| 506 | 857 | |
| 507 | 858 | private AsciiCodec() { |
| 508 | - super(ascii(), Charset.forName("US-ASCII")); | |
| 859 | + super(DataType.ascii(), Charset.forName("US-ASCII")); | |
| 509 | 860 | } |
| 510 | 861 | |
| 511 | 862 | @Override |
| 512 | 863 | public boolean accepts(Object value) { |
| 513 | - if (value instanceof String) { | |
| 514 | - return ASCII_PATTERN.matcher((String)value).matches(); | |
| 515 | - } | |
| 516 | - return false; | |
| 864 | + return value instanceof String && ASCII_PATTERN.matcher((String)value).matches(); | |
| 517 | 865 | } |
| 518 | 866 | |
| 519 | 867 | } |
| @@ -522,16 +870,16 @@ | ||
| 522 | 870 | * Base class for codecs handling CQL 8-byte integer types such as {@link DataType#bigint()}, |
| 523 | 871 | * {@link DataType#counter()} or {@link DataType#time()}. |
| 524 | 872 | */ |
| 525 | - public abstract static class LongCodec extends TypeCodec<Long> implements PrimitiveLongCodec { | |
| 873 | + private abstract static class LongCodec extends PrimitiveLongCodec { | |
| 526 | 874 | |
| 527 | - public LongCodec(DataType cqlType) { | |
| 528 | - super(cqlType, Long.class); | |
| 875 | + private LongCodec(DataType cqlType) { | |
| 876 | + super(cqlType); | |
| 529 | 877 | } |
| 530 | 878 | |
| 531 | 879 | @Override |
| 532 | 880 | public Long parse(String value) { |
| 533 | 881 | try { |
| 534 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : Long.parseLong(value); | |
| 882 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : Long.parseLong(value); | |
| 535 | 883 | } catch (NumberFormatException e) { |
| 536 | 884 | throw new InvalidTypeException(String.format("Cannot parse 64-bits long value from \"%s\"", value)); |
| 537 | 885 | } |
| @@ -540,15 +888,10 @@ | ||
| 540 | 888 | @Override |
| 541 | 889 | public String format(Long value) { |
| 542 | 890 | if (value == null) |
| 543 | - return NULL; | |
| 891 | + return "NULL"; | |
| 544 | 892 | return Long.toString(value); |
| 545 | 893 | } |
| 546 | 894 | |
| 547 | - @Override | |
| 548 | - public ByteBuffer serialize(Long value, ProtocolVersion protocolVersion) { | |
| 549 | - return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 550 | - } | |
| 551 | - | |
| 552 | 895 | @Override |
| 553 | 896 | public ByteBuffer serializeNoBoxing(long value, ProtocolVersion protocolVersion) { |
| 554 | 897 | ByteBuffer bb = ByteBuffer.allocate(8); |
| @@ -556,11 +899,6 @@ | ||
| 556 | 899 | return bb; |
| 557 | 900 | } |
| 558 | 901 | |
| 559 | - @Override | |
| 560 | - public Long deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 561 | - return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 562 | - } | |
| 563 | - | |
| 564 | 902 | @Override |
| 565 | 903 | public long deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 566 | 904 | if (bytes == null || bytes.remaining() == 0) |
| @@ -575,12 +913,12 @@ | ||
| 575 | 913 | /** |
| 576 | 914 | * This codec maps a CQL {@link DataType#bigint()} to a Java {@link Long}. |
| 577 | 915 | */ |
| 578 | - public static class BigintCodec extends LongCodec { | |
| 916 | + private static class BigintCodec extends LongCodec { | |
| 579 | 917 | |
| 580 | - public static final BigintCodec instance = new BigintCodec(); | |
| 918 | + private static final BigintCodec instance = new BigintCodec(); | |
| 581 | 919 | |
| 582 | 920 | private BigintCodec() { |
| 583 | - super(bigint()); | |
| 921 | + super(DataType.bigint()); | |
| 584 | 922 | } |
| 585 | 923 | |
| 586 | 924 | } |
| @@ -588,12 +926,12 @@ | ||
| 588 | 926 | /** |
| 589 | 927 | * This codec maps a CQL {@link DataType#counter()} to a Java {@link Long}. |
| 590 | 928 | */ |
| 591 | - public static class CounterCodec extends LongCodec { | |
| 929 | + private static class CounterCodec extends LongCodec { | |
| 592 | 930 | |
| 593 | - public static final CounterCodec instance = new CounterCodec(); | |
| 931 | + private static final CounterCodec instance = new CounterCodec(); | |
| 594 | 932 | |
| 595 | 933 | private CounterCodec() { |
| 596 | - super(counter()); | |
| 934 | + super(DataType.counter()); | |
| 597 | 935 | } |
| 598 | 936 | |
| 599 | 937 | } |
| @@ -601,23 +939,23 @@ | ||
| 601 | 939 | /** |
| 602 | 940 | * This codec maps a CQL {@link DataType#blob()} to a Java {@link ByteBuffer}. |
| 603 | 941 | */ |
| 604 | - public static class BlobCodec extends TypeCodec<ByteBuffer> { | |
| 942 | + private static class BlobCodec extends TypeCodec<ByteBuffer> { | |
| 605 | 943 | |
| 606 | - public static final BlobCodec instance = new BlobCodec(); | |
| 944 | + private static final BlobCodec instance = new BlobCodec(); | |
| 607 | 945 | |
| 608 | 946 | private BlobCodec() { |
| 609 | - super(blob(), ByteBuffer.class); | |
| 947 | + super(DataType.blob(), ByteBuffer.class); | |
| 610 | 948 | } |
| 611 | 949 | |
| 612 | 950 | @Override |
| 613 | 951 | public ByteBuffer parse(String value) { |
| 614 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : Bytes.fromHexString(value); | |
| 952 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : Bytes.fromHexString(value); | |
| 615 | 953 | } |
| 616 | 954 | |
| 617 | 955 | @Override |
| 618 | 956 | public String format(ByteBuffer value) { |
| 619 | 957 | if (value == null) |
| 620 | - return NULL; | |
| 958 | + return "NULL"; | |
| 621 | 959 | return Bytes.toHexString(value); |
| 622 | 960 | } |
| 623 | 961 | |
| @@ -633,26 +971,26 @@ | ||
| 633 | 971 | } |
| 634 | 972 | |
| 635 | 973 | /** |
| 636 | - * This codec maps a CQL {@link DataType#custom(String)} to a Java {@link ByteBuffer}. | |
| 974 | + * This codec maps a CQL {@link DataType#custom(String) custom} type to a Java {@link ByteBuffer}. | |
| 637 | 975 | * Note that no instance of this codec is part of the default set of codecs used by the Java driver; |
| 638 | 976 | * instances of this codec must be manually registered. |
| 639 | 977 | */ |
| 640 | - public static class CustomCodec extends TypeCodec<ByteBuffer> { | |
| 978 | + private static class CustomCodec extends TypeCodec<ByteBuffer> { | |
| 641 | 979 | |
| 642 | - public CustomCodec(DataType custom) { | |
| 980 | + private CustomCodec(DataType custom) { | |
| 643 | 981 | super(custom, ByteBuffer.class); |
| 644 | 982 | assert custom.getName() == Name.CUSTOM; |
| 645 | 983 | } |
| 646 | 984 | |
| 647 | 985 | @Override |
| 648 | 986 | public ByteBuffer parse(String value) { |
| 649 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : Bytes.fromHexString(value); | |
| 987 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : Bytes.fromHexString(value); | |
| 650 | 988 | } |
| 651 | 989 | |
| 652 | 990 | @Override |
| 653 | 991 | public String format(ByteBuffer value) { |
| 654 | 992 | if (value == null) |
| 655 | - return NULL; | |
| 993 | + return "NULL"; | |
| 656 | 994 | return Bytes.toHexString(value); |
| 657 | 995 | } |
| 658 | 996 | |
| @@ -670,20 +1008,20 @@ | ||
| 670 | 1008 | /** |
| 671 | 1009 | * This codec maps a CQL {@link DataType#cboolean()} to a Java {@link Boolean}. |
| 672 | 1010 | */ |
| 673 | - public static class BooleanCodec extends TypeCodec<Boolean> implements PrimitiveBooleanCodec { | |
| 1011 | + private static class BooleanCodec extends PrimitiveBooleanCodec { | |
| 674 | 1012 | |
| 675 | 1013 | private static final ByteBuffer TRUE = ByteBuffer.wrap(new byte[]{ 1 }); |
| 676 | 1014 | private static final ByteBuffer FALSE = ByteBuffer.wrap(new byte[]{ 0 }); |
| 677 | 1015 | |
| 678 | - public static final BooleanCodec instance = new BooleanCodec(); | |
| 1016 | + private static final BooleanCodec instance = new BooleanCodec(); | |
| 679 | 1017 | |
| 680 | 1018 | private BooleanCodec() { |
| 681 | - super(cboolean(), Boolean.class); | |
| 1019 | + super(DataType.cboolean()); | |
| 682 | 1020 | } |
| 683 | 1021 | |
| 684 | 1022 | @Override |
| 685 | 1023 | public Boolean parse(String value) { |
| 686 | - if (value == null || value.isEmpty() || value.equals(NULL)) | |
| 1024 | + if (value == null || value.isEmpty() || value.equals("NULL")) | |
| 687 | 1025 | return null; |
| 688 | 1026 | if (value.equalsIgnoreCase(Boolean.FALSE.toString())) |
| 689 | 1027 | return false; |
| @@ -696,25 +1034,15 @@ | ||
| 696 | 1034 | @Override |
| 697 | 1035 | public String format(Boolean value) { |
| 698 | 1036 | if (value == null) |
| 699 | - return NULL; | |
| 1037 | + return "NULL"; | |
| 700 | 1038 | return value ? "true" : "false"; |
| 701 | 1039 | } |
| 702 | 1040 | |
| 703 | - @Override | |
| 704 | - public ByteBuffer serialize(Boolean value, ProtocolVersion protocolVersion) { | |
| 705 | - return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 706 | - } | |
| 707 | - | |
| 708 | 1041 | @Override |
| 709 | 1042 | public ByteBuffer serializeNoBoxing(boolean value, ProtocolVersion protocolVersion) { |
| 710 | 1043 | return value ? TRUE.duplicate() : FALSE.duplicate(); |
| 711 | 1044 | } |
| 712 | 1045 | |
| 713 | - @Override | |
| 714 | - public Boolean deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 715 | - return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 716 | - } | |
| 717 | - | |
| 718 | 1046 | @Override |
| 719 | 1047 | public boolean deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 720 | 1048 | if (bytes == null || bytes.remaining() == 0) |
| @@ -729,18 +1057,18 @@ | ||
| 729 | 1057 | /** |
| 730 | 1058 | * This codec maps a CQL {@link DataType#decimal()} to a Java {@link BigDecimal}. |
| 731 | 1059 | */ |
| 732 | - public static class DecimalCodec extends TypeCodec<BigDecimal> { | |
| 1060 | + private static class DecimalCodec extends TypeCodec<BigDecimal> { | |
| 733 | 1061 | |
| 734 | - public static final DecimalCodec instance = new DecimalCodec(); | |
| 1062 | + private static final DecimalCodec instance = new DecimalCodec(); | |
| 735 | 1063 | |
| 736 | 1064 | private DecimalCodec() { |
| 737 | - super(decimal(), BigDecimal.class); | |
| 1065 | + super(DataType.decimal(), BigDecimal.class); | |
| 738 | 1066 | } |
| 739 | 1067 | |
| 740 | 1068 | @Override |
| 741 | 1069 | public BigDecimal parse(String value) { |
| 742 | 1070 | try { |
| 743 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : new BigDecimal(value); | |
| 1071 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : new BigDecimal(value); | |
| 744 | 1072 | } catch (NumberFormatException e) { |
| 745 | 1073 | throw new InvalidTypeException(String.format("Cannot parse decimal value from \"%s\"", value)); |
| 746 | 1074 | } |
| @@ -749,7 +1077,7 @@ | ||
| 749 | 1077 | @Override |
| 750 | 1078 | public String format(BigDecimal value) { |
| 751 | 1079 | if (value == null) |
| 752 | - return NULL; | |
| 1080 | + return "NULL"; | |
| 753 | 1081 | return value.toString(); |
| 754 | 1082 | } |
| 755 | 1083 | |
| @@ -788,18 +1116,18 @@ | ||
| 788 | 1116 | /** |
| 789 | 1117 | * This codec maps a CQL {@link DataType#cdouble()} to a Java {@link Double}. |
| 790 | 1118 | */ |
| 791 | - public static class DoubleCodec extends TypeCodec<Double> implements PrimitiveDoubleCodec { | |
| 1119 | + private static class DoubleCodec extends PrimitiveDoubleCodec { | |
| 792 | 1120 | |
| 793 | - public static final DoubleCodec instance = new DoubleCodec(); | |
| 1121 | + private static final DoubleCodec instance = new DoubleCodec(); | |
| 794 | 1122 | |
| 795 | 1123 | private DoubleCodec() { |
| 796 | - super(cdouble(), Double.class); | |
| 1124 | + super(DataType.cdouble()); | |
| 797 | 1125 | } |
| 798 | 1126 | |
| 799 | 1127 | @Override |
| 800 | 1128 | public Double parse(String value) { |
| 801 | 1129 | try { |
| 802 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : Double.parseDouble(value); | |
| 1130 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : Double.parseDouble(value); | |
| 803 | 1131 | } catch (NumberFormatException e) { |
| 804 | 1132 | throw new InvalidTypeException(String.format("Cannot parse 64-bits double value from \"%s\"", value)); |
| 805 | 1133 | } |
| @@ -808,15 +1136,10 @@ | ||
| 808 | 1136 | @Override |
| 809 | 1137 | public String format(Double value) { |
| 810 | 1138 | if (value == null) |
| 811 | - return NULL; | |
| 1139 | + return "NULL"; | |
| 812 | 1140 | return Double.toString(value); |
| 813 | 1141 | } |
| 814 | 1142 | |
| 815 | - @Override | |
| 816 | - public ByteBuffer serialize(Double value, ProtocolVersion protocolVersion) { | |
| 817 | - return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 818 | - } | |
| 819 | - | |
| 820 | 1143 | @Override |
| 821 | 1144 | public ByteBuffer serializeNoBoxing(double value, ProtocolVersion protocolVersion) { |
| 822 | 1145 | ByteBuffer bb = ByteBuffer.allocate(8); |
| @@ -824,11 +1147,6 @@ | ||
| 824 | 1147 | return bb; |
| 825 | 1148 | } |
| 826 | 1149 | |
| 827 | - @Override | |
| 828 | - public Double deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 829 | - return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 830 | - } | |
| 831 | - | |
| 832 | 1150 | @Override |
| 833 | 1151 | public double deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 834 | 1152 | if (bytes == null || bytes.remaining() == 0) |
| @@ -843,18 +1161,18 @@ | ||
| 843 | 1161 | /** |
| 844 | 1162 | * This codec maps a CQL {@link DataType#cfloat()} to a Java {@link Float}. |
| 845 | 1163 | */ |
| 846 | - public static class FloatCodec extends TypeCodec<Float> implements PrimitiveFloatCodec { | |
| 1164 | + private static class FloatCodec extends PrimitiveFloatCodec { | |
| 847 | 1165 | |
| 848 | - public static final FloatCodec instance = new FloatCodec(); | |
| 1166 | + private static final FloatCodec instance = new FloatCodec(); | |
| 849 | 1167 | |
| 850 | 1168 | private FloatCodec() { |
| 851 | - super(cfloat(), Float.class); | |
| 1169 | + super(DataType.cfloat()); | |
| 852 | 1170 | } |
| 853 | 1171 | |
| 854 | 1172 | @Override |
| 855 | 1173 | public Float parse(String value) { |
| 856 | 1174 | try { |
| 857 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : Float.parseFloat(value); | |
| 1175 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : Float.parseFloat(value); | |
| 858 | 1176 | } catch (NumberFormatException e) { |
| 859 | 1177 | throw new InvalidTypeException(String.format("Cannot parse 32-bits float value from \"%s\"", value)); |
| 860 | 1178 | } |
| @@ -863,15 +1181,10 @@ | ||
| 863 | 1181 | @Override |
| 864 | 1182 | public String format(Float value) { |
| 865 | 1183 | if (value == null) |
| 866 | - return NULL; | |
| 1184 | + return "NULL"; | |
| 867 | 1185 | return Float.toString(value); |
| 868 | 1186 | } |
| 869 | 1187 | |
| 870 | - @Override | |
| 871 | - public ByteBuffer serialize(Float value, ProtocolVersion protocolVersion) { | |
| 872 | - return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 873 | - } | |
| 874 | - | |
| 875 | 1188 | @Override |
| 876 | 1189 | public ByteBuffer serializeNoBoxing(float value, ProtocolVersion protocolVersion) { |
| 877 | 1190 | ByteBuffer bb = ByteBuffer.allocate(4); |
| @@ -879,11 +1192,6 @@ | ||
| 879 | 1192 | return bb; |
| 880 | 1193 | } |
| 881 | 1194 | |
| 882 | - @Override | |
| 883 | - public Float deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 884 | - return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 885 | - } | |
| 886 | - | |
| 887 | 1195 | @Override |
| 888 | 1196 | public float deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 889 | 1197 | if (bytes == null || bytes.remaining() == 0) |
| @@ -898,17 +1206,17 @@ | ||
| 898 | 1206 | /** |
| 899 | 1207 | * This codec maps a CQL {@link DataType#inet()} to a Java {@link InetAddress}. |
| 900 | 1208 | */ |
| 901 | - public static class InetCodec extends TypeCodec<InetAddress> { | |
| 1209 | + private static class InetCodec extends TypeCodec<InetAddress> { | |
| 902 | 1210 | |
| 903 | - public static final InetCodec instance = new InetCodec(); | |
| 1211 | + private static final InetCodec instance = new InetCodec(); | |
| 904 | 1212 | |
| 905 | 1213 | private InetCodec() { |
| 906 | - super(inet(), InetAddress.class); | |
| 1214 | + super(DataType.inet(), InetAddress.class); | |
| 907 | 1215 | } |
| 908 | 1216 | |
| 909 | 1217 | @Override |
| 910 | 1218 | public InetAddress parse(String value) { |
| 911 | - if (value == null || value.isEmpty() || value.equals(NULL)) | |
| 1219 | + if (value == null || value.isEmpty() || value.equals("NULL")) | |
| 912 | 1220 | return null; |
| 913 | 1221 | |
| 914 | 1222 | value = value.trim(); |
| @@ -924,7 +1232,7 @@ | ||
| 924 | 1232 | @Override |
| 925 | 1233 | public String format(InetAddress value) { |
| 926 | 1234 | if (value == null) |
| 927 | - return NULL; | |
| 1235 | + return "NULL"; | |
| 928 | 1236 | return "'" + value.getHostAddress() + "'"; |
| 929 | 1237 | } |
| 930 | 1238 | |
| @@ -948,18 +1256,18 @@ | ||
| 948 | 1256 | /** |
| 949 | 1257 | * This codec maps a CQL {@link DataType#tinyint()} to a Java {@link Byte}. |
| 950 | 1258 | */ |
| 951 | - public static class TinyIntCodec extends TypeCodec<Byte> implements PrimitiveByteCodec { | |
| 1259 | + private static class TinyIntCodec extends PrimitiveByteCodec { | |
| 952 | 1260 | |
| 953 | - public static final TinyIntCodec instance = new TinyIntCodec(); | |
| 1261 | + private static final TinyIntCodec instance = new TinyIntCodec(); | |
| 954 | 1262 | |
| 955 | 1263 | private TinyIntCodec() { |
| 956 | - super(tinyint(), Byte.class); | |
| 1264 | + super(tinyint()); | |
| 957 | 1265 | } |
| 958 | 1266 | |
| 959 | 1267 | @Override |
| 960 | 1268 | public Byte parse(String value) { |
| 961 | 1269 | try { |
| 962 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : Byte.parseByte(value); | |
| 1270 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : Byte.parseByte(value); | |
| 963 | 1271 | } catch (NumberFormatException e) { |
| 964 | 1272 | throw new InvalidTypeException(String.format("Cannot parse 8-bits int value from \"%s\"", value)); |
| 965 | 1273 | } |
| @@ -968,15 +1276,10 @@ | ||
| 968 | 1276 | @Override |
| 969 | 1277 | public String format(Byte value) { |
| 970 | 1278 | if (value == null) |
| 971 | - return NULL; | |
| 1279 | + return "NULL"; | |
| 972 | 1280 | return Byte.toString(value); |
| 973 | 1281 | } |
| 974 | 1282 | |
| 975 | - @Override | |
| 976 | - public ByteBuffer serialize(Byte value, ProtocolVersion protocolVersion) { | |
| 977 | - return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 978 | - } | |
| 979 | - | |
| 980 | 1283 | @Override |
| 981 | 1284 | public ByteBuffer serializeNoBoxing(byte value, ProtocolVersion protocolVersion) { |
| 982 | 1285 | ByteBuffer bb = ByteBuffer.allocate(1); |
| @@ -984,11 +1287,6 @@ | ||
| 984 | 1287 | return bb; |
| 985 | 1288 | } |
| 986 | 1289 | |
| 987 | - @Override | |
| 988 | - public Byte deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 989 | - return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 990 | - } | |
| 991 | - | |
| 992 | 1290 | @Override |
| 993 | 1291 | public byte deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 994 | 1292 | if (bytes == null || bytes.remaining() == 0) |
| @@ -1003,18 +1301,18 @@ | ||
| 1003 | 1301 | /** |
| 1004 | 1302 | * This codec maps a CQL {@link DataType#smallint()} to a Java {@link Short}. |
| 1005 | 1303 | */ |
| 1006 | - public static class SmallIntCodec extends TypeCodec<Short> implements PrimitiveShortCodec { | |
| 1304 | + private static class SmallIntCodec extends PrimitiveShortCodec { | |
| 1007 | 1305 | |
| 1008 | - public static final SmallIntCodec instance = new SmallIntCodec(); | |
| 1306 | + private static final SmallIntCodec instance = new SmallIntCodec(); | |
| 1009 | 1307 | |
| 1010 | 1308 | private SmallIntCodec() { |
| 1011 | - super(smallint(), Short.class); | |
| 1309 | + super(smallint()); | |
| 1012 | 1310 | } |
| 1013 | 1311 | |
| 1014 | 1312 | @Override |
| 1015 | 1313 | public Short parse(String value) { |
| 1016 | 1314 | try { |
| 1017 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : Short.parseShort(value); | |
| 1315 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : Short.parseShort(value); | |
| 1018 | 1316 | } catch (NumberFormatException e) { |
| 1019 | 1317 | throw new InvalidTypeException(String.format("Cannot parse 16-bits int value from \"%s\"", value)); |
| 1020 | 1318 | } |
| @@ -1023,15 +1321,10 @@ | ||
| 1023 | 1321 | @Override |
| 1024 | 1322 | public String format(Short value) { |
| 1025 | 1323 | if (value == null) |
| 1026 | - return NULL; | |
| 1324 | + return "NULL"; | |
| 1027 | 1325 | return Short.toString(value); |
| 1028 | 1326 | } |
| 1029 | 1327 | |
| 1030 | - @Override | |
| 1031 | - public ByteBuffer serialize(Short value, ProtocolVersion protocolVersion) { | |
| 1032 | - return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 1033 | - } | |
| 1034 | - | |
| 1035 | 1328 | @Override |
| 1036 | 1329 | public ByteBuffer serializeNoBoxing(short value, ProtocolVersion protocolVersion) { |
| 1037 | 1330 | ByteBuffer bb = ByteBuffer.allocate(2); |
| @@ -1039,11 +1332,6 @@ | ||
| 1039 | 1332 | return bb; |
| 1040 | 1333 | } |
| 1041 | 1334 | |
| 1042 | - @Override | |
| 1043 | - public Short deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 1044 | - return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 1045 | - } | |
| 1046 | - | |
| 1047 | 1335 | @Override |
| 1048 | 1336 | public short deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 1049 | 1337 | if (bytes == null || bytes.remaining() == 0) |
| @@ -1058,18 +1346,18 @@ | ||
| 1058 | 1346 | /** |
| 1059 | 1347 | * This codec maps a CQL {@link DataType#cint()} to a Java {@link Integer}. |
| 1060 | 1348 | */ |
| 1061 | - public static class IntCodec extends TypeCodec<Integer> implements PrimitiveIntCodec { | |
| 1349 | + private static class IntCodec extends PrimitiveIntCodec { | |
| 1062 | 1350 | |
| 1063 | - public static final IntCodec instance = new IntCodec(); | |
| 1351 | + private static final IntCodec instance = new IntCodec(); | |
| 1064 | 1352 | |
| 1065 | 1353 | private IntCodec() { |
| 1066 | - super(cint(), Integer.class); | |
| 1354 | + super(DataType.cint()); | |
| 1067 | 1355 | } |
| 1068 | 1356 | |
| 1069 | 1357 | @Override |
| 1070 | 1358 | public Integer parse(String value) { |
| 1071 | 1359 | try { |
| 1072 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : Integer.parseInt(value); | |
| 1360 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : Integer.parseInt(value); | |
| 1073 | 1361 | } catch (NumberFormatException e) { |
| 1074 | 1362 | throw new InvalidTypeException(String.format("Cannot parse 32-bits int value from \"%s\"", value)); |
| 1075 | 1363 | } |
| @@ -1078,15 +1366,10 @@ | ||
| 1078 | 1366 | @Override |
| 1079 | 1367 | public String format(Integer value) { |
| 1080 | 1368 | if (value == null) |
| 1081 | - return NULL; | |
| 1369 | + return "NULL"; | |
| 1082 | 1370 | return Integer.toString(value); |
| 1083 | 1371 | } |
| 1084 | 1372 | |
| 1085 | - @Override | |
| 1086 | - public ByteBuffer serialize(Integer value, ProtocolVersion protocolVersion) { | |
| 1087 | - return value == null ? null : serializeNoBoxing(value, protocolVersion); | |
| 1088 | - } | |
| 1089 | - | |
| 1090 | 1373 | @Override |
| 1091 | 1374 | public ByteBuffer serializeNoBoxing(int value, ProtocolVersion protocolVersion) { |
| 1092 | 1375 | ByteBuffer bb = ByteBuffer.allocate(4); |
| @@ -1094,11 +1377,6 @@ | ||
| 1094 | 1377 | return bb; |
| 1095 | 1378 | } |
| 1096 | 1379 | |
| 1097 | - @Override | |
| 1098 | - public Integer deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { | |
| 1099 | - return bytes == null || bytes.remaining() == 0 ? null : deserializeNoBoxing(bytes, protocolVersion); | |
| 1100 | - } | |
| 1101 | - | |
| 1102 | 1380 | @Override |
| 1103 | 1381 | public int deserializeNoBoxing(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 1104 | 1382 | if (bytes == null || bytes.remaining() == 0) |
| @@ -1113,7 +1391,7 @@ | ||
| 1113 | 1391 | /** |
| 1114 | 1392 | * This codec maps a CQL {@link DataType#timestamp()} to a Java {@link Date}. |
| 1115 | 1393 | */ |
| 1116 | - public static class TimestampCodec extends TypeCodec<Date> { | |
| 1394 | + private static class TimestampCodec extends TypeCodec<Date> { | |
| 1117 | 1395 | |
| 1118 | 1396 | private static final String[] iso8601Patterns = new String[]{ |
| 1119 | 1397 | "yyyy-MM-dd HH:mm", |
| @@ -1132,12 +1410,12 @@ | ||
| 1132 | 1410 | "yyyy-MM-ddZ" |
| 1133 | 1411 | }; |
| 1134 | 1412 | |
| 1135 | - public static final TimestampCodec instance = new TimestampCodec(); | |
| 1413 | + private static final TimestampCodec instance = new TimestampCodec(); | |
| 1136 | 1414 | |
| 1137 | 1415 | private static final Pattern IS_LONG_PATTERN = Pattern.compile("^-?\\d+$"); |
| 1138 | 1416 | |
| 1139 | 1417 | private TimestampCodec() { |
| 1140 | - super(timestamp(), Date.class); | |
| 1418 | + super(DataType.timestamp(), Date.class); | |
| 1141 | 1419 | } |
| 1142 | 1420 | |
| 1143 | 1421 | /* |
| @@ -1151,14 +1429,12 @@ | ||
| 1151 | 1429 | |
| 1152 | 1430 | ParsePosition pos = new ParsePosition(0); |
| 1153 | 1431 | for (String parsePattern : parsePatterns) { |
| 1154 | - String pattern = parsePattern; | |
| 1155 | 1432 | |
| 1156 | - parser.applyPattern(pattern); | |
| 1433 | + parser.applyPattern(parsePattern); | |
| 1157 | 1434 | pos.setIndex(0); |
| 1158 | 1435 | |
| 1159 | - String str2 = str; | |
| 1160 | - Date date = parser.parse(str2, pos); | |
| 1161 | - if (date != null && pos.getIndex() == str2.length()) { | |
| 1436 | + Date date = parser.parse(str, pos); | |
| 1437 | + if (date != null && pos.getIndex() == str.length()) { | |
| 1162 | 1438 | return date; |
| 1163 | 1439 | } |
| 1164 | 1440 | } |
| @@ -1167,7 +1443,7 @@ | ||
| 1167 | 1443 | |
| 1168 | 1444 | @Override |
| 1169 | 1445 | public Date parse(String value) { |
| 1170 | - if (value == null || value.isEmpty() || value.equals(NULL)) | |
| 1446 | + if (value == null || value.isEmpty() || value.equals("NULL")) | |
| 1171 | 1447 | return null; |
| 1172 | 1448 | |
| 1173 | 1449 | // strip enclosing single quotes, if any |
| @@ -1192,7 +1468,7 @@ | ||
| 1192 | 1468 | @Override |
| 1193 | 1469 | public String format(Date value) { |
| 1194 | 1470 | if (value == null) |
| 1195 | - return NULL; | |
| 1471 | + return "NULL"; | |
| 1196 | 1472 | return Long.toString(value.getTime()); |
| 1197 | 1473 | } |
| 1198 | 1474 | |
| @@ -1210,9 +1486,9 @@ | ||
| 1210 | 1486 | /** |
| 1211 | 1487 | * This codec maps a CQL {@link DataType#date()} to the custom {@link LocalDate} class. |
| 1212 | 1488 | */ |
| 1213 | - public static class DateCodec extends TypeCodec<LocalDate> { | |
| 1489 | + private static class DateCodec extends TypeCodec<LocalDate> { | |
| 1214 | 1490 | |
| 1215 | - public static final DateCodec instance = new DateCodec(); | |
| 1491 | + private static final DateCodec instance = new DateCodec(); | |
| 1216 | 1492 | |
| 1217 | 1493 | private static final Pattern IS_LONG_PATTERN = Pattern.compile("^-?\\d+$"); |
| 1218 | 1494 | private static final String pattern = "yyyy-MM-dd"; |
| @@ -1220,12 +1496,12 @@ | ||
| 1220 | 1496 | private static final long EPOCH_AS_CQL_LONG = (1L << 31); |
| 1221 | 1497 | |
| 1222 | 1498 | private DateCodec() { |
| 1223 | - super(date(), LocalDate.class); | |
| 1499 | + super(DataType.date(), LocalDate.class); | |
| 1224 | 1500 | } |
| 1225 | 1501 | |
| 1226 | 1502 | @Override |
| 1227 | 1503 | public LocalDate parse(String value) { |
| 1228 | - if (value == null || value.isEmpty() || value.equals(NULL)) | |
| 1504 | + if (value == null || value.isEmpty() || value.equals("NULL")) | |
| 1229 | 1505 | return null; |
| 1230 | 1506 | |
| 1231 | 1507 | // strip enclosing single quotes, if any |
| @@ -1251,7 +1527,6 @@ | ||
| 1251 | 1527 | return LocalDate.fromDaysSinceEpoch(days); |
| 1252 | 1528 | } |
| 1253 | 1529 | |
| 1254 | - | |
| 1255 | 1530 | SimpleDateFormat parser = new SimpleDateFormat(pattern); |
| 1256 | 1531 | parser.setLenient(false); |
| 1257 | 1532 | parser.setTimeZone(TimeZone.getTimeZone("UTC")); |
| @@ -1268,7 +1543,7 @@ | ||
| 1268 | 1543 | @Override |
| 1269 | 1544 | public String format(LocalDate value) { |
| 1270 | 1545 | if (value == null) |
| 1271 | - return NULL; | |
| 1546 | + return "NULL"; | |
| 1272 | 1547 | return "'" + value.toString() + "'"; |
| 1273 | 1548 | } |
| 1274 | 1549 | |
| @@ -1297,14 +1572,14 @@ | ||
| 1297 | 1572 | /** |
| 1298 | 1573 | * This codec maps a CQL {@link DataType#time()} to a Java {@link Long}. |
| 1299 | 1574 | */ |
| 1300 | - public static class TimeCodec extends LongCodec { | |
| 1575 | + private static class TimeCodec extends LongCodec { | |
| 1301 | 1576 | |
| 1302 | 1577 | private static final Pattern IS_LONG_PATTERN = Pattern.compile("^-?\\d+$"); |
| 1303 | 1578 | |
| 1304 | - public static final TimeCodec instance = new TimeCodec(); | |
| 1579 | + private static final TimeCodec instance = new TimeCodec(); | |
| 1305 | 1580 | |
| 1306 | 1581 | private TimeCodec() { |
| 1307 | - super(time()); | |
| 1582 | + super(DataType.time()); | |
| 1308 | 1583 | } |
| 1309 | 1584 | |
| 1310 | 1585 | // Time specific parsing loosely based on java.sql.Timestamp |
| @@ -1370,7 +1645,7 @@ | ||
| 1370 | 1645 | |
| 1371 | 1646 | @Override |
| 1372 | 1647 | public Long parse(String value) { |
| 1373 | - if (value == null || value.isEmpty() || value.equals(NULL)) | |
| 1648 | + if (value == null || value.isEmpty() || value.equals("NULL")) | |
| 1374 | 1649 | return null; |
| 1375 | 1650 | |
| 1376 | 1651 | // enclosing single quotes required, even for long literals |
| @@ -1396,7 +1671,7 @@ | ||
| 1396 | 1671 | @Override |
| 1397 | 1672 | public String format(Long value) { |
| 1398 | 1673 | if (value == null) |
| 1399 | - return NULL; | |
| 1674 | + return "NULL"; | |
| 1400 | 1675 | int nano = (int)(value % 1000000000); |
| 1401 | 1676 | value -= nano; |
| 1402 | 1677 | value /= 1000000000; |
| @@ -1409,7 +1684,7 @@ | ||
| 1409 | 1684 | int hours = (int)(value % 24); |
| 1410 | 1685 | value -= hours; |
| 1411 | 1686 | value /= 24; |
| 1412 | - assert(value == 0); | |
| 1687 | + assert (value == 0); | |
| 1413 | 1688 | StringBuilder sb = new StringBuilder("'"); |
| 1414 | 1689 | leftPadZeros(hours, 2, sb); |
| 1415 | 1690 | sb.append(":"); |
| @@ -1431,7 +1706,7 @@ | ||
| 1431 | 1706 | /** |
| 1432 | 1707 | * Base class for codecs handling CQL UUID types such as {@link DataType#uuid()} and {@link DataType#timeuuid()}. |
| 1433 | 1708 | */ |
| 1434 | - public static abstract class AbstractUUIDCodec extends TypeCodec<UUID> { | |
| 1709 | + private static abstract class AbstractUUIDCodec extends TypeCodec<UUID> { | |
| 1435 | 1710 | |
| 1436 | 1711 | private AbstractUUIDCodec(DataType cqlType) { |
| 1437 | 1712 | super(cqlType, UUID.class); |
| @@ -1440,7 +1715,7 @@ | ||
| 1440 | 1715 | @Override |
| 1441 | 1716 | public UUID parse(String value) { |
| 1442 | 1717 | try { |
| 1443 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : UUID.fromString(value); | |
| 1718 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : UUID.fromString(value); | |
| 1444 | 1719 | } catch (IllegalArgumentException e) { |
| 1445 | 1720 | throw new InvalidTypeException(String.format("Cannot parse UUID value from \"%s\"", value)); |
| 1446 | 1721 | } |
| @@ -1449,13 +1724,13 @@ | ||
| 1449 | 1724 | @Override |
| 1450 | 1725 | public String format(UUID value) { |
| 1451 | 1726 | if (value == null) |
| 1452 | - return NULL; | |
| 1727 | + return "NULL"; | |
| 1453 | 1728 | return value.toString(); |
| 1454 | 1729 | } |
| 1455 | 1730 | |
| 1456 | 1731 | @Override |
| 1457 | 1732 | public ByteBuffer serialize(UUID value, ProtocolVersion protocolVersion) { |
| 1458 | - if(value == null) | |
| 1733 | + if (value == null) | |
| 1459 | 1734 | return null; |
| 1460 | 1735 | ByteBuffer bb = ByteBuffer.allocate(16); |
| 1461 | 1736 | bb.putLong(0, value.getMostSignificantBits()); |
| @@ -1472,12 +1747,12 @@ | ||
| 1472 | 1747 | /** |
| 1473 | 1748 | * This codec maps a CQL {@link DataType#uuid()} to a Java {@link UUID}. |
| 1474 | 1749 | */ |
| 1475 | - public static class UUIDCodec extends AbstractUUIDCodec { | |
| 1750 | + private static class UUIDCodec extends AbstractUUIDCodec { | |
| 1476 | 1751 | |
| 1477 | - public static final UUIDCodec instance = new UUIDCodec(); | |
| 1752 | + private static final UUIDCodec instance = new UUIDCodec(); | |
| 1478 | 1753 | |
| 1479 | 1754 | private UUIDCodec() { |
| 1480 | - super(uuid()); | |
| 1755 | + super(DataType.uuid()); | |
| 1481 | 1756 | } |
| 1482 | 1757 | |
| 1483 | 1758 | } |
| @@ -1485,9 +1760,9 @@ | ||
| 1485 | 1760 | /** |
| 1486 | 1761 | * This codec maps a CQL {@link DataType#timeuuid()} to a Java {@link UUID}. |
| 1487 | 1762 | */ |
| 1488 | - public static class TimeUUIDCodec extends AbstractUUIDCodec { | |
| 1763 | + private static class TimeUUIDCodec extends AbstractUUIDCodec { | |
| 1489 | 1764 | |
| 1490 | - public static final TimeUUIDCodec instance = new TimeUUIDCodec(); | |
| 1765 | + private static final TimeUUIDCodec instance = new TimeUUIDCodec(); | |
| 1491 | 1766 | |
| 1492 | 1767 | private TimeUUIDCodec() { |
| 1493 | 1768 | super(timeuuid()); |
| @@ -1495,17 +1770,14 @@ | ||
| 1495 | 1770 | |
| 1496 | 1771 | @Override |
| 1497 | 1772 | public boolean accepts(Object value) { |
| 1498 | - if (super.accepts(value)) { | |
| 1499 | - // only accept time-based uuids | |
| 1500 | - return ((UUID)value).version() == 1; | |
| 1501 | - } | |
| 1502 | - return false; | |
| 1773 | + // only accept time-based uuids | |
| 1774 | + return super.accepts(value) && ((UUID)value).version() == 1; | |
| 1503 | 1775 | } |
| 1504 | 1776 | |
| 1505 | 1777 | @Override |
| 1506 | 1778 | public String format(UUID value) { |
| 1507 | 1779 | if (value == null) |
| 1508 | - return NULL; | |
| 1780 | + return "NULL"; | |
| 1509 | 1781 | if (value.version() != 1) |
| 1510 | 1782 | throw new InvalidTypeException(String.format("%s is not a Type 1 (time-based) UUID", value)); |
| 1511 | 1783 | return super.format(value); |
| @@ -1513,7 +1785,7 @@ | ||
| 1513 | 1785 | |
| 1514 | 1786 | @Override |
| 1515 | 1787 | public ByteBuffer serialize(UUID value, ProtocolVersion protocolVersion) { |
| 1516 | - if(value == null) | |
| 1788 | + if (value == null) | |
| 1517 | 1789 | return null; |
| 1518 | 1790 | if (value.version() != 1) |
| 1519 | 1791 | throw new InvalidTypeException(String.format("%s is not a Type 1 (time-based) UUID", value)); |
| @@ -1524,18 +1796,18 @@ | ||
| 1524 | 1796 | /** |
| 1525 | 1797 | * This codec maps a CQL {@link DataType#varint()} to a Java {@link BigInteger}. |
| 1526 | 1798 | */ |
| 1527 | - public static class VarintCodec extends TypeCodec<BigInteger> { | |
| 1799 | + private static class VarintCodec extends TypeCodec<BigInteger> { | |
| 1528 | 1800 | |
| 1529 | - public static final VarintCodec instance = new VarintCodec(); | |
| 1801 | + private static final VarintCodec instance = new VarintCodec(); | |
| 1530 | 1802 | |
| 1531 | 1803 | private VarintCodec() { |
| 1532 | - super(varint(), BigInteger.class); | |
| 1804 | + super(DataType.varint(), BigInteger.class); | |
| 1533 | 1805 | } |
| 1534 | 1806 | |
| 1535 | 1807 | @Override |
| 1536 | 1808 | public BigInteger parse(String value) { |
| 1537 | 1809 | try { |
| 1538 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : new BigInteger(value); | |
| 1810 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : new BigInteger(value); | |
| 1539 | 1811 | } catch (NumberFormatException e) { |
| 1540 | 1812 | throw new InvalidTypeException(String.format("Cannot parse varint value from \"%s\"", value)); |
| 1541 | 1813 | } |
| @@ -1544,7 +1816,7 @@ | ||
| 1544 | 1816 | @Override |
| 1545 | 1817 | public String format(BigInteger value) { |
| 1546 | 1818 | if (value == null) |
| 1547 | - return NULL; | |
| 1819 | + return "NULL"; | |
| 1548 | 1820 | return value.toString(); |
| 1549 | 1821 | } |
| 1550 | 1822 | |
| @@ -1564,18 +1836,18 @@ | ||
| 1564 | 1836 | * or {@link DataType#set(DataType)}. |
| 1565 | 1837 | * Note that for practical reasons, {@link MapCodec} does not inherit from this class. |
| 1566 | 1838 | */ |
| 1567 | - public abstract static class CollectionCodec<E, C extends Collection<E>> extends TypeCodec<C> { | |
| 1839 | + private abstract static class CollectionCodec<E, C extends Collection<E>> extends TypeCodec<C> { | |
| 1568 | 1840 | |
| 1569 | 1841 | protected final TypeCodec<E> eltCodec; |
| 1570 | 1842 | |
| 1571 | - public CollectionCodec(CollectionType cqlType, TypeToken<C> javaType, TypeCodec<E> eltCodec) { | |
| 1843 | + private CollectionCodec(CollectionType cqlType, TypeToken<C> javaType, TypeCodec<E> eltCodec) { | |
| 1572 | 1844 | super(cqlType, javaType); |
| 1573 | 1845 | this.eltCodec = eltCodec; |
| 1574 | 1846 | } |
| 1575 | 1847 | |
| 1576 | 1848 | @Override |
| 1577 | 1849 | public ByteBuffer serialize(C value, ProtocolVersion protocolVersion) { |
| 1578 | - if(value == null) | |
| 1850 | + if (value == null) | |
| 1579 | 1851 | return null; |
| 1580 | 1852 | List<ByteBuffer> bbs = new ArrayList<ByteBuffer>(value.size()); |
| 1581 | 1853 | for (E elt : value) { |
| @@ -1598,7 +1870,7 @@ | ||
| 1598 | 1870 | @Override |
| 1599 | 1871 | public C deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 1600 | 1872 | if (bytes == null || bytes.remaining() == 0) |
| 1601 | - return unmodifiable(newInstance(0)); | |
| 1873 | + return newInstance(0); | |
| 1602 | 1874 | try { |
| 1603 | 1875 | ByteBuffer input = bytes.duplicate(); |
| 1604 | 1876 | int n = CodecUtils.readCollectionSize(input, protocolVersion); |
| @@ -1607,7 +1879,7 @@ | ||
| 1607 | 1879 | ByteBuffer databb = CodecUtils.readCollectionValue(input, protocolVersion); |
| 1608 | 1880 | coll.add(eltCodec.deserialize(databb, protocolVersion)); |
| 1609 | 1881 | } |
| 1610 | - return unmodifiable(coll); | |
| 1882 | + return coll; | |
| 1611 | 1883 | } catch (BufferUnderflowException e) { |
| 1612 | 1884 | throw new InvalidTypeException("Not enough bytes to deserialize list"); |
| 1613 | 1885 | } |
| @@ -1616,7 +1888,7 @@ | ||
| 1616 | 1888 | @Override |
| 1617 | 1889 | public String format(C value) { |
| 1618 | 1890 | if (value == null) |
| 1619 | - return NULL; | |
| 1891 | + return "NULL"; | |
| 1620 | 1892 | StringBuilder sb = new StringBuilder(); |
| 1621 | 1893 | sb.append(getOpeningChar()); |
| 1622 | 1894 | int i = 0; |
| @@ -1631,7 +1903,7 @@ | ||
| 1631 | 1903 | |
| 1632 | 1904 | @Override |
| 1633 | 1905 | public C parse(String value) { |
| 1634 | - if (value == null || value.isEmpty() || value.equals(NULL)) | |
| 1906 | + if (value == null || value.isEmpty() || value.equals("NULL")) | |
| 1635 | 1907 | return null; |
| 1636 | 1908 | |
| 1637 | 1909 | int idx = ParseUtils.skipSpaces(value, 0); |
| @@ -1681,8 +1953,6 @@ | ||
| 1681 | 1953 | |
| 1682 | 1954 | protected abstract C newInstance(int capacity); |
| 1683 | 1955 | |
| 1684 | - protected abstract C unmodifiable(C coll); | |
| 1685 | - | |
| 1686 | 1956 | protected abstract char getOpeningChar(); |
| 1687 | 1957 | |
| 1688 | 1958 | protected abstract char getClosingChar(); |
| @@ -1691,11 +1961,12 @@ | ||
| 1691 | 1961 | |
| 1692 | 1962 | /** |
| 1693 | 1963 | * This codec maps a CQL {@link DataType#list(DataType) list type} to a Java {@link List}. |
| 1964 | + * Implementation note: this codec returns mutable, non thread-safe {@link ArrayList} instances. | |
| 1694 | 1965 | */ |
| 1695 | - public static class ListCodec<T> extends CollectionCodec<T, List<T>> { | |
| 1966 | + private static class ListCodec<T> extends CollectionCodec<T, List<T>> { | |
| 1696 | 1967 | |
| 1697 | - public ListCodec(TypeCodec<T> eltCodec) { | |
| 1698 | - super(list(eltCodec.getCqlType()), listOf(eltCodec.getJavaType()), eltCodec); | |
| 1968 | + private ListCodec(TypeCodec<T> eltCodec) { | |
| 1969 | + super(DataType.list(eltCodec.getCqlType()), listOf(eltCodec.getJavaType()), eltCodec); | |
| 1699 | 1970 | } |
| 1700 | 1971 | |
| 1701 | 1972 | @Override |
| @@ -1703,11 +1974,6 @@ | ||
| 1703 | 1974 | return new ArrayList<T>(capacity); |
| 1704 | 1975 | } |
| 1705 | 1976 | |
| 1706 | - @Override | |
| 1707 | - protected List<T> unmodifiable(List<T> coll) { | |
| 1708 | - return Collections.unmodifiableList(coll); | |
| 1709 | - } | |
| 1710 | - | |
| 1711 | 1977 | @Override |
| 1712 | 1978 | protected char getOpeningChar() { |
| 1713 | 1979 | return '['; |
| @@ -1722,11 +1988,12 @@ | ||
| 1722 | 1988 | |
| 1723 | 1989 | /** |
| 1724 | 1990 | * This codec maps a CQL {@link DataType#set(DataType) set type} to a Java {@link Set}. |
| 1991 | + * Implementation note: this codec returns mutable, non thread-safe {@link LinkedHashSet} instances. | |
| 1725 | 1992 | */ |
| 1726 | - public static class SetCodec<T> extends CollectionCodec<T, Set<T>> { | |
| 1993 | + private static class SetCodec<T> extends CollectionCodec<T, Set<T>> { | |
| 1727 | 1994 | |
| 1728 | - public SetCodec(TypeCodec<T> eltCodec) { | |
| 1729 | - super(set(eltCodec.cqlType), setOf(eltCodec.getJavaType()), eltCodec); | |
| 1995 | + private SetCodec(TypeCodec<T> eltCodec) { | |
| 1996 | + super(DataType.set(eltCodec.cqlType), setOf(eltCodec.getJavaType()), eltCodec); | |
| 1730 | 1997 | } |
| 1731 | 1998 | |
| 1732 | 1999 | @Override |
| @@ -1734,11 +2001,6 @@ | ||
| 1734 | 2001 | return new LinkedHashSet<T>(capacity); |
| 1735 | 2002 | } |
| 1736 | 2003 | |
| 1737 | - @Override | |
| 1738 | - protected Set<T> unmodifiable(Set<T> coll) { | |
| 1739 | - return Collections.unmodifiableSet(coll); | |
| 1740 | - } | |
| 1741 | - | |
| 1742 | 2004 | @Override |
| 1743 | 2005 | protected char getOpeningChar() { |
| 1744 | 2006 | return '{'; |
| @@ -1752,15 +2014,16 @@ | ||
| 1752 | 2014 | |
| 1753 | 2015 | /** |
| 1754 | 2016 | * This codec maps a CQL {@link DataType#map(DataType, DataType) map type} to a Java {@link Map}. |
| 2017 | + * Implementation note: this codec returns mutable, non thread-safe {@link LinkedHashMap} instances. | |
| 1755 | 2018 | */ |
| 1756 | - public static class MapCodec<K, V> extends TypeCodec<Map<K, V>> { | |
| 2019 | + private static class MapCodec<K, V> extends TypeCodec<Map<K, V>> { | |
| 1757 | 2020 | |
| 1758 | 2021 | private final TypeCodec<K> keyCodec; |
| 1759 | 2022 | |
| 1760 | 2023 | private final TypeCodec<V> valueCodec; |
| 1761 | 2024 | |
| 1762 | - public MapCodec(TypeCodec<K> keyCodec, TypeCodec<V> valueCodec) { | |
| 1763 | - super(map(keyCodec.getCqlType(), valueCodec.getCqlType()), mapOf(keyCodec.getJavaType(), valueCodec.getJavaType())); | |
| 2025 | + private MapCodec(TypeCodec<K> keyCodec, TypeCodec<V> valueCodec) { | |
| 2026 | + super(DataType.map(keyCodec.getCqlType(), valueCodec.getCqlType()), mapOf(keyCodec.getJavaType(), valueCodec.getJavaType())); | |
| 1764 | 2027 | this.keyCodec = keyCodec; |
| 1765 | 2028 | this.valueCodec = valueCodec; |
| 1766 | 2029 | } |
| @@ -1780,7 +2043,7 @@ | ||
| 1780 | 2043 | |
| 1781 | 2044 | @Override |
| 1782 | 2045 | public Map<K, V> parse(String value) { |
| 1783 | - if (value == null || value.isEmpty() || value.equals(NULL)) | |
| 2046 | + if (value == null || value.isEmpty() || value.equals("NULL")) | |
| 1784 | 2047 | return null; |
| 1785 | 2048 | |
| 1786 | 2049 | int idx = ParseUtils.skipSpaces(value, 0); |
| @@ -1790,7 +2053,7 @@ | ||
| 1790 | 2053 | idx = ParseUtils.skipSpaces(value, idx); |
| 1791 | 2054 | |
| 1792 | 2055 | if (value.charAt(idx) == '}') |
| 1793 | - return Collections.emptyMap(); | |
| 2056 | + return new LinkedHashMap<K, V>(0); | |
| 1794 | 2057 | |
| 1795 | 2058 | Map<K, V> m = new HashMap<K, V>(); |
| 1796 | 2059 | while (idx < value.length()) { |
| @@ -1835,7 +2098,7 @@ | ||
| 1835 | 2098 | @Override |
| 1836 | 2099 | public String format(Map<K, V> value) { |
| 1837 | 2100 | if (value == null) |
| 1838 | - return NULL; | |
| 2101 | + return "NULL"; | |
| 1839 | 2102 | StringBuilder sb = new StringBuilder(); |
| 1840 | 2103 | sb.append("{"); |
| 1841 | 2104 | int i = 0; |
| @@ -1885,7 +2148,7 @@ | ||
| 1885 | 2148 | @Override |
| 1886 | 2149 | public Map<K, V> deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) { |
| 1887 | 2150 | if (bytes == null || bytes.remaining() == 0) |
| 1888 | - return Collections.emptyMap(); | |
| 2151 | + return new LinkedHashMap<K, V>(0); | |
| 1889 | 2152 | try { |
| 1890 | 2153 | ByteBuffer input = bytes.duplicate(); |
| 1891 | 2154 | int n = CodecUtils.readCollectionSize(input, protocolVersion); |
| @@ -1895,7 +2158,7 @@ | ||
| 1895 | 2158 | ByteBuffer vbb = CodecUtils.readCollectionValue(input, protocolVersion); |
| 1896 | 2159 | m.put(keyCodec.deserialize(kbb, protocolVersion), valueCodec.deserialize(vbb, protocolVersion)); |
| 1897 | 2160 | } |
| 1898 | - return Collections.unmodifiableMap(m); | |
| 2161 | + return m; | |
| 1899 | 2162 | } catch (BufferUnderflowException e) { |
| 1900 | 2163 | throw new InvalidTypeException("Not enough bytes to deserialize a map"); |
| 1901 | 2164 | } |
| @@ -1905,26 +2168,23 @@ | ||
| 1905 | 2168 | /** |
| 1906 | 2169 | * This codec maps a CQL {@link UserType} to a {@link UDTValue}. |
| 1907 | 2170 | */ |
| 1908 | - public static class UDTCodec extends TypeCodec<UDTValue> { | |
| 2171 | + private static class UDTCodec extends TypeCodec<UDTValue> { | |
| 1909 | 2172 | |
| 1910 | 2173 | private final UserType definition; |
| 1911 | 2174 | |
| 1912 | - public UDTCodec(UserType definition) { | |
| 2175 | + private UDTCodec(UserType definition) { | |
| 1913 | 2176 | super(definition, UDTValue.class); |
| 1914 | 2177 | this.definition = definition; |
| 1915 | 2178 | } |
| 1916 | 2179 | |
| 1917 | 2180 | @Override |
| 1918 | 2181 | public boolean accepts(Object value) { |
| 1919 | - if(value instanceof UDTValue) { | |
| 1920 | - return ((UDTValue) value).getType().equals(definition); | |
| 1921 | - } | |
| 1922 | - return false; | |
| 2182 | + return value instanceof UDTValue && ((UDTValue)value).getType().equals(definition); | |
| 1923 | 2183 | } |
| 1924 | 2184 | |
| 1925 | 2185 | @Override |
| 1926 | 2186 | public UDTValue parse(String value) { |
| 1927 | - if (value == null || value.isEmpty() || value.equals(NULL)) | |
| 2187 | + if (value == null || value.isEmpty() || value.equals("NULL")) | |
| 1928 | 2188 | return null; |
| 1929 | 2189 | |
| 1930 | 2190 | UDTValue v = definition.newValue(); |
| @@ -1983,7 +2243,7 @@ | ||
| 1983 | 2243 | @Override |
| 1984 | 2244 | public String format(UDTValue value) { |
| 1985 | 2245 | if (value == null) |
| 1986 | - return NULL; | |
| 2246 | + return "NULL"; | |
| 1987 | 2247 | return value.toString(); |
| 1988 | 2248 | } |
| 1989 | 2249 | |
| @@ -2027,11 +2287,11 @@ | ||
| 2027 | 2287 | /** |
| 2028 | 2288 | * This codec maps a CQL {@link TupleType} to a {@link TupleValue}. |
| 2029 | 2289 | */ |
| 2030 | - public static class TupleCodec extends TypeCodec<TupleValue> { | |
| 2290 | + private static class TupleCodec extends TypeCodec<TupleValue> { | |
| 2031 | 2291 | |
| 2032 | 2292 | private final TupleType definition; |
| 2033 | 2293 | |
| 2034 | - public TupleCodec(TupleType definition) { | |
| 2294 | + private TupleCodec(TupleType definition) { | |
| 2035 | 2295 | super(definition, TupleValue.class); |
| 2036 | 2296 | this.definition = definition; |
| 2037 | 2297 | } |
| @@ -2047,12 +2307,12 @@ | ||
| 2047 | 2307 | public boolean accepts(Object value) { |
| 2048 | 2308 | // a tuple codec should accept tuple values of a different type, |
| 2049 | 2309 | // provided that the latter is contained in this codec's type. |
| 2050 | - return value instanceof TupleValue && definition.contains(((TupleValue) value).getType()); | |
| 2310 | + return value instanceof TupleValue && definition.contains(((TupleValue)value).getType()); | |
| 2051 | 2311 | } |
| 2052 | 2312 | |
| 2053 | 2313 | @Override |
| 2054 | 2314 | public TupleValue parse(String value) { |
| 2055 | - if (value == null || value.isEmpty() || value.equals(NULL)) | |
| 2315 | + if (value == null || value.isEmpty() || value.equals("NULL")) | |
| 2056 | 2316 | return null; |
| 2057 | 2317 | |
| 2058 | 2318 | TupleValue v = definition.newValue(); |
| @@ -2096,7 +2356,7 @@ | ||
| 2096 | 2356 | @Override |
| 2097 | 2357 | public String format(TupleValue value) { |
| 2098 | 2358 | if (value == null) |
| 2099 | - return NULL; | |
| 2359 | + return "NULL"; | |
| 2100 | 2360 | return value.toString(); |
| 2101 | 2361 | } |
| 2102 | 2362 | |
| @@ -2137,211 +2397,4 @@ | ||
| 2137 | 2397 | } |
| 2138 | 2398 | } |
| 2139 | 2399 | |
| 2140 | - /** | |
| 2141 | - * An abstract TypeCodec that actually stores objects as serialized strings. | |
| 2142 | - * This can serve as a base for codecs dealing with XML or JSON formats. | |
| 2143 | - * | |
| 2144 | - * @param <T> The Java type this codec serializes from and deserializes to. | |
| 2145 | - */ | |
| 2146 | - public abstract static class StringParsingCodec<T> extends TypeCodec<T> { | |
| 2147 | - | |
| 2148 | - private final TypeCodec<String> innerCodec; | |
| 2149 | - | |
| 2150 | - public StringParsingCodec(Class<T> javaType) { | |
| 2151 | - this(TypeToken.of(javaType)); | |
| 2152 | - } | |
| 2153 | - | |
| 2154 | - public StringParsingCodec(TypeToken<T> javaType) { | |
| 2155 | - this(VarcharCodec.instance, javaType); | |
| 2156 | - } | |
| 2157 | - | |
| 2158 | - public StringParsingCodec(TypeCodec<String> innerCodec, Class<T> javaType) { | |
| 2159 | - this(innerCodec, TypeToken.of(javaType)); | |
| 2160 | - } | |
| 2161 | - | |
| 2162 | - public StringParsingCodec(TypeCodec<String> innerCodec, TypeToken<T> javaType) { | |
| 2163 | - super(innerCodec.getCqlType(), javaType); | |
| 2164 | - this.innerCodec = innerCodec; | |
| 2165 | - } | |
| 2166 | - | |
| 2167 | - @Override | |
| 2168 | - public ByteBuffer serialize(T value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 2169 | - return value == null ? null : innerCodec.serialize(toString(value), protocolVersion); | |
| 2170 | - } | |
| 2171 | - | |
| 2172 | - @Override | |
| 2173 | - public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 2174 | - String value = innerCodec.deserialize(bytes, protocolVersion); | |
| 2175 | - return value == null ? null : fromString(value); | |
| 2176 | - } | |
| 2177 | - | |
| 2178 | - @Override | |
| 2179 | - public String format(T value) throws InvalidTypeException { | |
| 2180 | - return value == null ? null : innerCodec.format(toString(value)); | |
| 2181 | - } | |
| 2182 | - | |
| 2183 | - @Override | |
| 2184 | - public T parse(String value) throws InvalidTypeException { | |
| 2185 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : fromString(innerCodec.parse(value)); | |
| 2186 | - } | |
| 2187 | - | |
| 2188 | - /** | |
| 2189 | - * Return the String representation of the given object; | |
| 2190 | - * no special CQL quoting should be applied here. | |
| 2191 | - * Null values should be accepted; in most cases, implementors | |
| 2192 | - * should return null for null inputs. | |
| 2193 | - * | |
| 2194 | - * @param value the value to convert into a string | |
| 2195 | - * @return the String representation of the given object | |
| 2196 | - */ | |
| 2197 | - protected abstract String toString(T value); | |
| 2198 | - | |
| 2199 | - /** | |
| 2200 | - * Parse the given string into an object; | |
| 2201 | - * no special CQL unquoting should be applied here. | |
| 2202 | - * Null values should be accepted; in most cases, implementors | |
| 2203 | - * should return null for null inputs. | |
| 2204 | - * | |
| 2205 | - * @param value the string to parse | |
| 2206 | - * @return the parsed object. | |
| 2207 | - */ | |
| 2208 | - protected abstract T fromString(String value); | |
| 2209 | - | |
| 2210 | - } | |
| 2211 | - | |
| 2212 | - /** | |
| 2213 | - * An abstract TypeCodec that maps a Java Pojo to another Java object | |
| 2214 | - * that can in turn be serialized into a CQL type. | |
| 2215 | - * This can serve as a base for libraries dealing with Pojo mappings. | |
| 2216 | - * | |
| 2217 | - * @param <T> The outer Java type | |
| 2218 | - * @param <U> The inner Java type | |
| 2219 | - */ | |
| 2220 | - public abstract static class MappingCodec<T, U> extends TypeCodec<T> { | |
| 2221 | - | |
| 2222 | - protected final TypeCodec<U> innerCodec; | |
| 2223 | - | |
| 2224 | - public MappingCodec(TypeCodec<U> innerCodec, Class<T> javaType) { | |
| 2225 | - this(innerCodec, TypeToken.of(javaType)); | |
| 2226 | - } | |
| 2227 | - | |
| 2228 | - public MappingCodec(TypeCodec<U> innerCodec, TypeToken<T> javaType) { | |
| 2229 | - super(innerCodec.getCqlType(), javaType); | |
| 2230 | - this.innerCodec = innerCodec; | |
| 2231 | - } | |
| 2232 | - | |
| 2233 | - @Override | |
| 2234 | - public ByteBuffer serialize(T value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 2235 | - return innerCodec.serialize(serialize(value), protocolVersion); | |
| 2236 | - } | |
| 2237 | - | |
| 2238 | - @Override | |
| 2239 | - public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 2240 | - return deserialize(innerCodec.deserialize(bytes, protocolVersion)); | |
| 2241 | - } | |
| 2242 | - | |
| 2243 | - @Override | |
| 2244 | - public T parse(String value) throws InvalidTypeException { | |
| 2245 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : deserialize(innerCodec.parse(value)); | |
| 2246 | - } | |
| 2247 | - | |
| 2248 | - @Override | |
| 2249 | - public String format(T value) throws InvalidTypeException { | |
| 2250 | - return value == null ? null : innerCodec.format(serialize(value)); | |
| 2251 | - } | |
| 2252 | - | |
| 2253 | - protected abstract T deserialize(U value); | |
| 2254 | - | |
| 2255 | - protected abstract U serialize(T value); | |
| 2256 | - | |
| 2257 | - } | |
| 2258 | - | |
| 2259 | - /** | |
| 2260 | - * A codec that serializes {@link Enum} instances as CQL {@link DataType#varchar() varchar}s | |
| 2261 | - * representing their programmatic names as returned by {@link Enum#name()}. | |
| 2262 | - * <p> | |
| 2263 | - * This codec is automatically used by the driver to create codecs for {@link Enum} | |
| 2264 | - * instances on the fly. | |
| 2265 | - * | |
| 2266 | - * @param <E> The Enum class this codec serializes from and deserializes to. | |
| 2267 | - */ | |
| 2268 | - public static class EnumStringCodec<E extends Enum<E>> extends StringParsingCodec<E> { | |
| 2269 | - | |
| 2270 | - private final Class<E> enumClass; | |
| 2271 | - | |
| 2272 | - public EnumStringCodec(Class<E> enumClass) { | |
| 2273 | - super(enumClass); | |
| 2274 | - this.enumClass = enumClass; | |
| 2275 | - } | |
| 2276 | - | |
| 2277 | - public EnumStringCodec(TypeCodec<String> innerCodec, Class<E> enumClass) { | |
| 2278 | - super(innerCodec, enumClass); | |
| 2279 | - this.enumClass = enumClass; | |
| 2280 | - } | |
| 2281 | - | |
| 2282 | - @Override | |
| 2283 | - protected String toString(E value) { | |
| 2284 | - return value == null ? null : value.name(); | |
| 2285 | - } | |
| 2286 | - | |
| 2287 | - @Override | |
| 2288 | - protected E fromString(String value) { | |
| 2289 | - return value == null ? null : Enum.valueOf(enumClass, value); | |
| 2290 | - } | |
| 2291 | - | |
| 2292 | - } | |
| 2293 | - | |
| 2294 | - /** | |
| 2295 | - * A codec that serializes {@link Enum} instances as CQL {@link DataType#cint() int}s | |
| 2296 | - * representing their ordinal values as returned by {@link Enum#ordinal()}. | |
| 2297 | - * <p> | |
| 2298 | - * Note that, by default, the driver uses {@link EnumStringCodec} to serialize {@link Enum} instances; | |
| 2299 | - * to force the use of this codec instead for a specific {@link Enum} instance, | |
| 2300 | - * the appropriate {@link EnumIntCodec} instance must be explicitly | |
| 2301 | - * {@link CodecRegistry#register(TypeCodec) registered}. | |
| 2302 | - * <p> | |
| 2303 | - * <strong>Note that this codec relies on the enum constants declaration order; | |
| 2304 | - * it is therefore vital that this order remains immutable.</strong> | |
| 2305 | - * | |
| 2306 | - * @param <E> The Enum class this codec serializes from and deserializes to. | |
| 2307 | - */ | |
| 2308 | - public static class EnumIntCodec<E extends Enum<E>> extends TypeCodec<E> { | |
| 2309 | - | |
| 2310 | - private final E[] enumConstants; | |
| 2311 | - | |
| 2312 | - private final TypeCodec<Integer> innerCodec; | |
| 2313 | - | |
| 2314 | - public EnumIntCodec(Class<E> enumClass) { | |
| 2315 | - this(IntCodec.instance, enumClass); | |
| 2316 | - } | |
| 2317 | - | |
| 2318 | - public EnumIntCodec(TypeCodec<Integer> innerCodec, Class<E> enumClass) { | |
| 2319 | - super(innerCodec.getCqlType(), enumClass); | |
| 2320 | - this.enumConstants = enumClass.getEnumConstants(); | |
| 2321 | - this.innerCodec = innerCodec; | |
| 2322 | - } | |
| 2323 | - | |
| 2324 | - @Override | |
| 2325 | - public ByteBuffer serialize(E value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 2326 | - return innerCodec.serialize(value.ordinal(), protocolVersion); | |
| 2327 | - } | |
| 2328 | - | |
| 2329 | - @Override | |
| 2330 | - public E deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 2331 | - return enumConstants[innerCodec.deserialize(bytes, protocolVersion)]; | |
| 2332 | - } | |
| 2333 | - | |
| 2334 | - @Override | |
| 2335 | - public E parse(String value) throws InvalidTypeException { | |
| 2336 | - return value == null || value.isEmpty() || value.equals(NULL) ? null : enumConstants[Integer.parseInt(value)]; | |
| 2337 | - } | |
| 2338 | - | |
| 2339 | - @Override | |
| 2340 | - public String format(E value) throws InvalidTypeException { | |
| 2341 | - if (value == null) | |
| 2342 | - return NULL; | |
| 2343 | - return Integer.toString(value.ordinal()); | |
| 2344 | - } | |
| 2345 | - } | |
| 2346 | - | |
| 2347 | 2400 | } |
modified driver-core/src/test/java/com/datastax/driver/core/AggregateMetadataTest.java +3 −3
| @@ -152,13 +152,13 @@ | ||
| 152 | 152 | private static final String TEXT = "org.apache.cassandra.db.marshal.UTF8Type"; |
| 153 | 153 | |
| 154 | 154 | private static final Row SYSTEM_ROW_CAT_TOS = mockRow("cat_tos", ImmutableList.of("int"), ImmutableList.of(INT), |
| 155 | - null, TypeCodec.VarcharCodec.instance.serialize("0", PROTOCOL_VERSION), TEXT, "cat", TEXT); | |
| 155 | + null, TypeCodec.varchar().serialize("0", PROTOCOL_VERSION), TEXT, "cat", TEXT); | |
| 156 | 156 | |
| 157 | 157 | private static final Row SYSTEM_ROW_MYCOUNT = mockRow("mycount", Collections.<String>emptyList(), Collections.<String>emptyList(), |
| 158 | - null, TypeCodec.IntCodec.instance.serialize(0, PROTOCOL_VERSION), INT, "inc", INT); | |
| 158 | + null, TypeCodec.cint().serialize(0, PROTOCOL_VERSION), INT, "inc", INT); | |
| 159 | 159 | |
| 160 | 160 | private static final Row SYSTEM_ROW_PRETTYSUM = mockRow("prettysum", ImmutableList.of("int"), ImmutableList.of(INT), |
| 161 | - "announce", TypeCodec.IntCodec.instance.serialize(0, PROTOCOL_VERSION), TEXT, "plus", INT); | |
| 161 | + "announce", TypeCodec.cint().serialize(0, PROTOCOL_VERSION), TEXT, "plus", INT); | |
| 162 | 162 | |
| 163 | 163 | private static final Row SYSTEM_ROW_SUM = mockRow("sum", ImmutableList.of("int"), ImmutableList.of(INT), |
| 164 | 164 | null, null, INT, "plus", INT); |
modified driver-core/src/test/java/com/datastax/driver/core/BoundStatementTest.java +3 −3
| @@ -25,7 +25,7 @@ | ||
| 25 | 25 | import static org.assertj.core.api.Assertions.assertThat; |
| 26 | 26 | import static org.assertj.core.api.Assertions.fail; |
| 27 | 27 | |
| 28 | -import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 28 | +import com.datastax.driver.core.exceptions.CodecNotFoundException; | |
| 29 | 29 | |
| 30 | 30 | public class BoundStatementTest extends CCMBridge.PerClassSingleNodeCluster { |
| 31 | 31 | |
| @@ -60,8 +60,8 @@ | ||
| 60 | 60 | |
| 61 | 61 | try { |
| 62 | 62 | statement.getString(0); |
| 63 | - fail("Expected type error"); | |
| 64 | - } catch (InvalidTypeException e) { /* expected */ } | |
| 63 | + fail("Expected codec not found error"); | |
| 64 | + } catch (CodecNotFoundException e) { /* expected */ } | |
| 65 | 65 | |
| 66 | 66 | try { |
| 67 | 67 | statement.getString(3); |
added driver-core/src/test/java/com/datastax/driver/core/CodecRegistryTest.java +92 −0
| @@ -0,0 +1,92 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 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.driver.core; | |
| 17 | + | |
| 18 | +import java.util.List; | |
| 19 | + | |
| 20 | +import com.google.common.reflect.TypeToken; | |
| 21 | +import org.apache.log4j.Level; | |
| 22 | +import org.apache.log4j.Logger; | |
| 23 | +import org.testng.annotations.Test; | |
| 24 | + | |
| 25 | +import static org.mockito.Mockito.mock; | |
| 26 | +import static org.mockito.Mockito.when; | |
| 27 | + | |
| 28 | +import static com.datastax.driver.core.Assertions.assertThat; | |
| 29 | + | |
| 30 | +public class CodecRegistryTest { | |
| 31 | + TypeToken<List<Integer>> LIST_OF_INT_TOKEN = new TypeToken<List<Integer>>() { | |
| 32 | + }; | |
| 33 | + | |
| 34 | + @Test(groups = "unit") | |
| 35 | + public void should_ignore_codec_colliding_with_already_registered_codec() { | |
| 36 | + MemoryAppender logs = startCapturingLogs(); | |
| 37 | + | |
| 38 | + CodecRegistry registry = new CodecRegistry(); | |
| 39 | + | |
| 40 | + TypeCodec newCodec = mock(TypeCodec.class); | |
| 41 | + when(newCodec.getCqlType()).thenReturn(DataType.cint()); | |
| 42 | + when(newCodec.getJavaType()).thenReturn(TypeToken.of(Integer.class)); | |
| 43 | + when(newCodec.toString()).thenReturn("newCodec"); | |
| 44 | + | |
| 45 | + registry.register(newCodec); | |
| 46 | + | |
| 47 | + assertThat(logs.getNext()).contains("Ignoring codec newCodec"); | |
| 48 | + assertThat( | |
| 49 | + registry.codecFor(DataType.cint(), Integer.class) | |
| 50 | + ).isNotSameAs(newCodec); | |
| 51 | + | |
| 52 | + stopCapturingLogs(logs); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Test(groups = "unit") | |
| 56 | + public void should_ignore_codec_colliding_with_already_generated_codec() { | |
| 57 | + MemoryAppender logs = startCapturingLogs(); | |
| 58 | + | |
| 59 | + CodecRegistry registry = new CodecRegistry(); | |
| 60 | + | |
| 61 | + // Force generation of a list token from the default token | |
| 62 | + registry.codecFor(DataType.list(DataType.cint()), LIST_OF_INT_TOKEN); | |
| 63 | + | |
| 64 | + TypeCodec newCodec = mock(TypeCodec.class); | |
| 65 | + when(newCodec.getCqlType()).thenReturn(DataType.list(DataType.cint())); | |
| 66 | + when(newCodec.getJavaType()).thenReturn(LIST_OF_INT_TOKEN); | |
| 67 | + when(newCodec.toString()).thenReturn("newCodec"); | |
| 68 | + | |
| 69 | + registry.register(newCodec); | |
| 70 | + | |
| 71 | + assertThat(logs.getNext()).contains("Ignoring codec newCodec"); | |
| 72 | + assertThat( | |
| 73 | + registry.codecFor(DataType.list(DataType.cint()), LIST_OF_INT_TOKEN) | |
| 74 | + ).isNotSameAs(newCodec); | |
| 75 | + | |
| 76 | + stopCapturingLogs(logs); | |
| 77 | + } | |
| 78 | + | |
| 79 | + private MemoryAppender startCapturingLogs() { | |
| 80 | + Logger registryLogger = Logger.getLogger(CodecRegistry.class); | |
| 81 | + registryLogger.setLevel(Level.WARN); | |
| 82 | + MemoryAppender logs = new MemoryAppender(); | |
| 83 | + registryLogger.addAppender(logs); | |
| 84 | + return logs; | |
| 85 | + } | |
| 86 | + | |
| 87 | + private void stopCapturingLogs(MemoryAppender logs) { | |
| 88 | + Logger registryLogger = Logger.getLogger(CodecRegistry.class); | |
| 89 | + registryLogger.setLevel(null); | |
| 90 | + registryLogger.removeAppender(logs); | |
| 91 | + } | |
| 92 | +} | |
| No newline at the end of the file |
added driver-core/src/test/java/com/datastax/driver/core/EnumIntCodec.java +75 −0
| @@ -0,0 +1,75 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 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.driver.core; | |
| 17 | + | |
| 18 | +import java.nio.ByteBuffer; | |
| 19 | + | |
| 20 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * A codec that serializes {@link Enum} instances as CQL {@link DataType#cint() int}s | |
| 24 | + * representing their ordinal values as returned by {@link Enum#ordinal()}. | |
| 25 | + * <p> | |
| 26 | + * Note that, by default, the driver uses {@link EnumStringCodec} to serialize {@link Enum} instances; | |
| 27 | + * to force the use of this codec instead for a specific {@link Enum} instance, | |
| 28 | + * the appropriate {@link EnumIntCodec} instance must be explicitly | |
| 29 | + * {@link CodecRegistry#register(TypeCodec) registered}. | |
| 30 | + * <p> | |
| 31 | + * <strong>Note that this codec relies on the enum constants declaration order; | |
| 32 | + * it is therefore vital that this order remains immutable.</strong> | |
| 33 | + * | |
| 34 | + * @param <E> The Enum class this codec serializes from and deserializes to. | |
| 35 | + */ | |
| 36 | +public class EnumIntCodec<E extends Enum<E>> extends TypeCodec<E> { | |
| 37 | + | |
| 38 | + private final E[] enumConstants; | |
| 39 | + | |
| 40 | + private final TypeCodec<Integer> innerCodec; | |
| 41 | + | |
| 42 | + public EnumIntCodec(Class<E> enumClass) { | |
| 43 | + this(TypeCodec.cint(), enumClass); | |
| 44 | + } | |
| 45 | + | |
| 46 | + public EnumIntCodec(TypeCodec<Integer> innerCodec, Class<E> enumClass) { | |
| 47 | + super(innerCodec.getCqlType(), enumClass); | |
| 48 | + this.enumConstants = enumClass.getEnumConstants(); | |
| 49 | + this.innerCodec = innerCodec; | |
| 50 | + } | |
| 51 | + | |
| 52 | + @Override | |
| 53 | + public ByteBuffer serialize(E value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 54 | + return value == null ? null : innerCodec.serialize(value.ordinal(), protocolVersion); | |
| 55 | + } | |
| 56 | + | |
| 57 | + @Override | |
| 58 | + public E deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 59 | + Integer value = innerCodec.deserialize(bytes, protocolVersion); | |
| 60 | + return value == null ? null : enumConstants[value]; | |
| 61 | + } | |
| 62 | + | |
| 63 | + @Override | |
| 64 | + public String format(E value) throws InvalidTypeException { | |
| 65 | + if (value == null) | |
| 66 | + return "NULL"; | |
| 67 | + return innerCodec.format(value.ordinal()); | |
| 68 | + } | |
| 69 | + | |
| 70 | + @Override | |
| 71 | + public E parse(String value) throws InvalidTypeException { | |
| 72 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : enumConstants[innerCodec.parse(value)]; | |
| 73 | + } | |
| 74 | + | |
| 75 | +} |
added driver-core/src/test/java/com/datastax/driver/core/EnumStringCodec.java +70 −0
| @@ -0,0 +1,70 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 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.driver.core; | |
| 17 | + | |
| 18 | +import java.nio.ByteBuffer; | |
| 19 | + | |
| 20 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 21 | + | |
| 22 | +/** | |
| 23 | + * A codec that serializes {@link Enum} instances as CQL {@link DataType#varchar() varchar}s | |
| 24 | + * representing their programmatic names as returned by {@link Enum#name()}. | |
| 25 | + * <p> | |
| 26 | + * This codec is automatically used by the driver to create codecs for {@link Enum} | |
| 27 | + * instances on the fly. | |
| 28 | + * | |
| 29 | + * @param <E> The Enum class this codec serializes from and deserializes to. | |
| 30 | + */ | |
| 31 | +public class EnumStringCodec<E extends Enum<E>> extends TypeCodec<E> { | |
| 32 | + | |
| 33 | + private final Class<E> enumClass; | |
| 34 | + | |
| 35 | + private final TypeCodec<String> innerCodec; | |
| 36 | + | |
| 37 | + public EnumStringCodec(Class<E> enumClass) { | |
| 38 | + this(TypeCodec.varchar(), enumClass); | |
| 39 | + } | |
| 40 | + | |
| 41 | + public EnumStringCodec(TypeCodec<String> innerCodec, Class<E> enumClass) { | |
| 42 | + super(innerCodec.getCqlType(), enumClass); | |
| 43 | + this.innerCodec = innerCodec; | |
| 44 | + this.enumClass = enumClass; | |
| 45 | + } | |
| 46 | + | |
| 47 | + @Override | |
| 48 | + public ByteBuffer serialize(E value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 49 | + return value == null ? null : innerCodec.serialize(value.name(), protocolVersion); | |
| 50 | + } | |
| 51 | + | |
| 52 | + @Override | |
| 53 | + public E deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 54 | + String value = innerCodec.deserialize(bytes, protocolVersion); | |
| 55 | + return value == null ? null : Enum.valueOf(enumClass, value); | |
| 56 | + } | |
| 57 | + | |
| 58 | + @Override | |
| 59 | + public String format(E value) throws InvalidTypeException { | |
| 60 | + if (value == null) | |
| 61 | + return "NULL"; | |
| 62 | + return innerCodec.format(value.name()); | |
| 63 | + } | |
| 64 | + | |
| 65 | + @Override | |
| 66 | + public E parse(String value) throws InvalidTypeException { | |
| 67 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : Enum.valueOf(enumClass, innerCodec.parse(value)); | |
| 68 | + } | |
| 69 | + | |
| 70 | +} |
added driver-core/src/test/java/com/datastax/driver/core/GettableDataIntegrationTest.java +497 −0
| @@ -0,0 +1,497 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 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.driver.core; | |
| 17 | + | |
| 18 | +import java.math.BigDecimal; | |
| 19 | +import java.math.BigInteger; | |
| 20 | +import java.net.InetAddress; | |
| 21 | +import java.net.UnknownHostException; | |
| 22 | +import java.nio.ByteBuffer; | |
| 23 | +import java.util.Collection; | |
| 24 | +import java.util.Date; | |
| 25 | +import java.util.List; | |
| 26 | +import java.util.Map; | |
| 27 | +import java.util.Set; | |
| 28 | +import java.util.UUID; | |
| 29 | +import java.util.concurrent.atomic.AtomicInteger; | |
| 30 | + | |
| 31 | +import com.google.common.collect.ImmutableMap; | |
| 32 | +import com.google.common.collect.ImmutableSet; | |
| 33 | +import com.google.common.reflect.TypeToken; | |
| 34 | +import org.testng.annotations.BeforeClass; | |
| 35 | +import org.testng.annotations.Test; | |
| 36 | + | |
| 37 | +import static com.google.common.collect.Lists.newArrayList; | |
| 38 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 39 | + | |
| 40 | +import static com.datastax.driver.core.CodecUtils.listOf; | |
| 41 | +import static com.datastax.driver.core.TestUtils.getValue; | |
| 42 | +import static com.datastax.driver.core.TestUtils.setValue; | |
| 43 | + | |
| 44 | +public class GettableDataIntegrationTest extends CCMBridge.PerClassSingleNodeCluster { | |
| 45 | + | |
| 46 | + boolean is21 = cassandraVersion.compareTo(VersionNumber.parse("2.1.3")) > 0; | |
| 47 | + boolean is20 = cassandraVersion.compareTo(VersionNumber.parse("2.0")) > 0; | |
| 48 | + | |
| 49 | + CodecRegistry registry = new CodecRegistry(); | |
| 50 | + | |
| 51 | + // Used for generating unique keys. | |
| 52 | + AtomicInteger keyCounter = new AtomicInteger(0); | |
| 53 | + | |
| 54 | + @Override | |
| 55 | + protected Collection<String> getTableDefinitions() { | |
| 56 | + // only add tuples / nested collections at > 2.1.3. | |
| 57 | + return newArrayList("CREATE TABLE codec_mapping (k int PRIMARY KEY, " | |
| 58 | + + "v int, l list<int>, m map<int,int>" + | |
| 59 | + (is21 ? ", t tuple<int,int>, s set<frozen<list<int>>>)" : ")")); | |
| 60 | + } | |
| 61 | + | |
| 62 | + @Override | |
| 63 | + protected Cluster.Builder configure(Cluster.Builder builder) { | |
| 64 | + return super.configure(builder).withCodecRegistry(registry); | |
| 65 | + } | |
| 66 | + | |
| 67 | + @BeforeClass(groups = "short") | |
| 68 | + public void setUpRegistry() { | |
| 69 | + for(TypeMapping<?> mapping : mappings) { | |
| 70 | + registry.register(mapping.codec); | |
| 71 | + } | |
| 72 | + } | |
| 73 | + | |
| 74 | + static final ByteBuffer intBuf = ByteBuffer.allocate(4); | |
| 75 | + static { | |
| 76 | + intBuf.putInt(1); | |
| 77 | + intBuf.flip(); | |
| 78 | + } | |
| 79 | + | |
| 80 | + static InetAddress localhost; | |
| 81 | + static { | |
| 82 | + try { | |
| 83 | + localhost = InetAddress.getLocalHost(); | |
| 84 | + } catch(UnknownHostException e) { | |
| 85 | + localhost = null; | |
| 86 | + } | |
| 87 | + } | |
| 88 | + | |
| 89 | + // Mappings of Codecs, Data Type to access as (used for determining what get|set method to call), value to set. | |
| 90 | + private TypeMapping<?>[] mappings = { | |
| 91 | + // set|getString | |
| 92 | + new TypeMapping<String>(new IntToStringCodec(), DataType.varchar(), "1"), | |
| 93 | + // set|getLong | |
| 94 | + new TypeMapping<Long>(new IntToLongCodec(), DataType.bigint(), 1L), | |
| 95 | + // set|getBytes | |
| 96 | + new TypeMapping<ByteBuffer>(new IntToByteBufferCodec(), DataType.blob(), intBuf), | |
| 97 | + // set|getBool | |
| 98 | + new TypeMapping<Boolean>(new IntToBooleanCodec(), DataType.cboolean(), true), | |
| 99 | + // set|getDecimal | |
| 100 | + new TypeMapping<BigDecimal>(new IntToBigDecimalCodec(), DataType.decimal(), new BigDecimal(1)), | |
| 101 | + // set|getDouble | |
| 102 | + new TypeMapping<Double>(new IntToDoubleCodec(), DataType.cdouble(), 1.0d), | |
| 103 | + // set|getFloat | |
| 104 | + new TypeMapping<Float>(new IntToFloatCodec(), DataType.cfloat(), 1.0f), | |
| 105 | + // set|getInet | |
| 106 | + new TypeMapping<InetAddress>(new IntToInetAddressCodec(), DataType.inet(), localhost), | |
| 107 | + // set|getTime | |
| 108 | + new TypeMapping<Long>(new IntToLongCodec(), DataType.time(), 8675309L), | |
| 109 | + // set|getByte | |
| 110 | + new TypeMapping<Byte>(new IntToByteCodec(), DataType.tinyint(), (byte)0xCF), | |
| 111 | + // set|getShort | |
| 112 | + new TypeMapping<Short>(new IntToShortCodec(), DataType.smallint(), (short)1003), | |
| 113 | + // set|getTimestamp | |
| 114 | + new TypeMapping<Date>(new IntToDateCodec(), DataType.timestamp(), new Date(124677)), | |
| 115 | + // set|getDate | |
| 116 | + new TypeMapping<LocalDate>(new IntToLocalDateCodec(), DataType.date(), LocalDate.fromDaysSinceEpoch(1523)), | |
| 117 | + // set|getUUID | |
| 118 | + new TypeMapping<UUID>(new IntToUUIDCodec(), DataType.uuid(), new UUID(244242, 0)), | |
| 119 | + // set|getVarint | |
| 120 | + new TypeMapping<BigInteger>(new IntToBigIntegerCodec(), DataType.varint(), BigInteger.valueOf(4566432L)) | |
| 121 | + }; | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Validates that all {@link GettableData} types will allow their get methods to be invoked on a column that does | |
| 125 | + * not match data's cql type if a codec is registered that maps the java type of the getter method to the cql type | |
| 126 | + * of the column. | |
| 127 | + * <p/> | |
| 128 | + * Also validates that all {@link SettableData} types will allow their set methods to be invoked on a column that | |
| 129 | + * does not match data's cql type if a codec is registered that maps the java type of the setter method to the cql | |
| 130 | + * type of the column. | |
| 131 | + * <p/> | |
| 132 | + * Executes the following for each set|get set: | |
| 133 | + * | |
| 134 | + * <ol> | |
| 135 | + * <li>Insert row using a prepared statement and binding by name.</li> | |
| 136 | + * <li>Insert row using a prepared statement and binding by index.</li> | |
| 137 | + * <li>Insert row using a prepared statement and binding everything at once.</li> | |
| 138 | + * <li>Retrieve inserted rows and get values by name.</li> | |
| 139 | + * <li>Retrieve inserted rows and get values by index.</li> | |
| 140 | + * </ol> | |
| 141 | + * | |
| 142 | + * @jira_ticket JAVA-940 | |
| 143 | + * @test_category queries | |
| 144 | + */ | |
| 145 | + @Test(groups = "short") | |
| 146 | + public void should_allow_getting_and_setting_by_type_if_codec_registered() { | |
| 147 | + String insertStmt = "INSERT INTO codec_mapping (k,v,l,m" + (is21 ? ",t,s" : "") + ") values (?,?,?,?" + (is21 ? ",?,?)" : ")"); | |
| 148 | + PreparedStatement insert = session.prepare(insertStmt); | |
| 149 | + PreparedStatement select = session.prepare("SELECT v,l,m" + (is21 ? ",t,s" : "") + " from codec_mapping where k=?"); | |
| 150 | + | |
| 151 | + TupleType tupleType = new TupleType(newArrayList(DataType.cint(), DataType.cint()), | |
| 152 | + cluster.getConfiguration().getProtocolOptions().getProtocolVersion(), registry); | |
| 153 | + | |
| 154 | + for(TypeMapping<?> mapping : mappings) { | |
| 155 | + // Keys used to insert data in this iteration. | |
| 156 | + List<Integer> keys = newArrayList(); | |
| 157 | + | |
| 158 | + // Values to store. | |
| 159 | + Map<Object,Object> map = ImmutableMap.of(mapping.value, mapping.value); | |
| 160 | + List<Object> list = newArrayList(mapping.value); | |
| 161 | + Set<List<Object>> set = ImmutableSet.of(list); | |
| 162 | + TupleValue tupleValue = new TupleValue(tupleType); | |
| 163 | + setValue(tupleValue, 0, mapping.outerType, mapping.value); | |
| 164 | + setValue(tupleValue, 1, mapping.outerType, mapping.value); | |
| 165 | + | |
| 166 | + // Insert by name. | |
| 167 | + BoundStatement byName = insert.bind(); | |
| 168 | + int byNameKey = keyCounter.incrementAndGet(); | |
| 169 | + keys.add(byNameKey); | |
| 170 | + byName.setInt("k", byNameKey); | |
| 171 | + setValue(byName, "v", mapping.outerType, mapping.value); | |
| 172 | + byName.setList("l", list, mapping.javaType); | |
| 173 | + byName.setMap("m", map, mapping.javaType, mapping.javaType); | |
| 174 | + if(is21) { | |
| 175 | + byName.setTupleValue("t", tupleValue); | |
| 176 | + byName.setSet("s", set, listOf(mapping.javaType)); | |
| 177 | + } | |
| 178 | + session.execute(byName); | |
| 179 | + | |
| 180 | + // Insert by index. | |
| 181 | + BoundStatement byIndex = insert.bind(); | |
| 182 | + int byIndexKey = keyCounter.incrementAndGet(); | |
| 183 | + keys.add(byIndexKey); | |
| 184 | + byIndex.setInt(0, byIndexKey); | |
| 185 | + setValue(byIndex, 1, mapping.outerType, mapping.value); | |
| 186 | + byIndex.setList(2, list, mapping.javaType); | |
| 187 | + byIndex.setMap(3, map, mapping.javaType, mapping.javaType); | |
| 188 | + if(is21) { | |
| 189 | + byIndex.setTupleValue(4, tupleValue); | |
| 190 | + byIndex.setSet(5, set, listOf(mapping.javaType)); | |
| 191 | + } | |
| 192 | + session.execute(byIndex); | |
| 193 | + | |
| 194 | + // Insert by binding all at once. | |
| 195 | + BoundStatement fullBind; | |
| 196 | + int fullBindKey = keyCounter.incrementAndGet(); | |
| 197 | + keys.add(fullBindKey); | |
| 198 | + if(is21) { | |
| 199 | + fullBind = insert.bind(fullBindKey, mapping.value, list, map, tupleValue, set); | |
| 200 | + } else { | |
| 201 | + fullBind = insert.bind(fullBindKey, mapping.value, list, map); | |
| 202 | + } | |
| 203 | + session.execute(fullBind); | |
| 204 | + | |
| 205 | + for(int key : keys) { | |
| 206 | + // Retrieve by name. | |
| 207 | + Row row = session.execute(select.bind(key)).one(); | |
| 208 | + assertThat(getValue(row, "v", mapping.outerType, registry)).isEqualTo(mapping.value); | |
| 209 | + assertThat(row.getList("l", mapping.codec.getJavaType())).isEqualTo(list); | |
| 210 | + assertThat(row.getMap("m", mapping.codec.getJavaType(), mapping.codec.getJavaType())).isEqualTo(map); | |
| 211 | + | |
| 212 | + if(is21) { | |
| 213 | + TupleValue returnedTuple = row.getTupleValue("t"); | |
| 214 | + assertThat(getValue(returnedTuple, 0, mapping.outerType, registry)).isEqualTo(mapping.value); | |
| 215 | + assertThat(getValue(returnedTuple, 1, mapping.outerType, registry)).isEqualTo(mapping.value); | |
| 216 | + | |
| 217 | + assertThat(row.getSet("s", listOf(mapping.javaType))).isEqualTo(set); | |
| 218 | + } | |
| 219 | + | |
| 220 | + // Retrieve by index. | |
| 221 | + assertThat(getValue(row, 0, mapping.outerType, registry)).isEqualTo(mapping.value); | |
| 222 | + assertThat(row.getList(1, mapping.codec.getJavaType())).isEqualTo(list); | |
| 223 | + assertThat(row.getMap(2, mapping.codec.getJavaType(), mapping.codec.getJavaType())).isEqualTo(map); | |
| 224 | + | |
| 225 | + if(is21) { | |
| 226 | + TupleValue returnedTuple = row.getTupleValue(3); | |
| 227 | + assertThat(getValue(returnedTuple, 0, mapping.outerType, registry)).isEqualTo(mapping.value); | |
| 228 | + assertThat(getValue(returnedTuple, 1, mapping.outerType, registry)).isEqualTo(mapping.value); | |
| 229 | + | |
| 230 | + assertThat(row.getSet(4, listOf(mapping.javaType))).isEqualTo(set); | |
| 231 | + } | |
| 232 | + } | |
| 233 | + } | |
| 234 | + } | |
| 235 | + | |
| 236 | + private static class TypeMapping<T> { | |
| 237 | + final TypeCodec<T> codec; | |
| 238 | + final TypeToken<Object> javaType; | |
| 239 | + final DataType outerType; | |
| 240 | + final T value; | |
| 241 | + | |
| 242 | + @SuppressWarnings("unchecked") | |
| 243 | + TypeMapping(TypeCodec<T> codec, DataType outerType, T value) { | |
| 244 | + this.codec = codec; | |
| 245 | + this.javaType = (TypeToken<Object>)codec.getJavaType(); | |
| 246 | + this.outerType = outerType; | |
| 247 | + this.value = value; | |
| 248 | + } | |
| 249 | + } | |
| 250 | + | |
| 251 | + // Int <-> Type mappings. | |
| 252 | + private static class IntToLongCodec extends MappingCodec<Long, Integer> { | |
| 253 | + | |
| 254 | + IntToLongCodec() { | |
| 255 | + super(TypeCodec.cint(), Long.class); | |
| 256 | + } | |
| 257 | + | |
| 258 | + @Override | |
| 259 | + protected Long deserialize(Integer value) { | |
| 260 | + return value.longValue(); | |
| 261 | + } | |
| 262 | + | |
| 263 | + @Override | |
| 264 | + protected Integer serialize(Long value) { | |
| 265 | + return value.intValue(); | |
| 266 | + } | |
| 267 | + } | |
| 268 | + | |
| 269 | + private static class IntToStringCodec extends MappingCodec<String, Integer> { | |
| 270 | + | |
| 271 | + IntToStringCodec() { | |
| 272 | + super(TypeCodec.cint(), String.class); | |
| 273 | + } | |
| 274 | + | |
| 275 | + @Override | |
| 276 | + protected String deserialize(Integer value) { | |
| 277 | + return value.toString(); | |
| 278 | + } | |
| 279 | + | |
| 280 | + @Override | |
| 281 | + protected Integer serialize(String value) { | |
| 282 | + return Integer.parseInt(value); | |
| 283 | + } | |
| 284 | + } | |
| 285 | + | |
| 286 | + private static class IntToByteBufferCodec extends MappingCodec<ByteBuffer, Integer> { | |
| 287 | + | |
| 288 | + IntToByteBufferCodec() { | |
| 289 | + super(TypeCodec.cint(), ByteBuffer.class); | |
| 290 | + } | |
| 291 | + | |
| 292 | + @Override | |
| 293 | + protected ByteBuffer deserialize(Integer value) { | |
| 294 | + ByteBuffer buf = ByteBuffer.allocate(4); | |
| 295 | + buf.putInt(value); | |
| 296 | + buf.flip(); | |
| 297 | + return buf; | |
| 298 | + } | |
| 299 | + | |
| 300 | + @Override | |
| 301 | + protected Integer serialize(ByteBuffer value) { | |
| 302 | + return value.duplicate().getInt(); | |
| 303 | + } | |
| 304 | + } | |
| 305 | + | |
| 306 | + private static class IntToBooleanCodec extends MappingCodec<Boolean, Integer> { | |
| 307 | + | |
| 308 | + IntToBooleanCodec() { | |
| 309 | + super(TypeCodec.cint(), Boolean.class); | |
| 310 | + } | |
| 311 | + | |
| 312 | + @Override | |
| 313 | + protected Boolean deserialize(Integer value) { | |
| 314 | + return value != 0; | |
| 315 | + } | |
| 316 | + | |
| 317 | + @Override | |
| 318 | + protected Integer serialize(Boolean value) { | |
| 319 | + return value ? 1 : 0; | |
| 320 | + } | |
| 321 | + } | |
| 322 | + | |
| 323 | + private static class IntToBigDecimalCodec extends MappingCodec<BigDecimal, Integer> { | |
| 324 | + | |
| 325 | + IntToBigDecimalCodec() { | |
| 326 | + super(TypeCodec.cint(), BigDecimal.class); | |
| 327 | + } | |
| 328 | + | |
| 329 | + @Override | |
| 330 | + protected BigDecimal deserialize(Integer value) { | |
| 331 | + return new BigDecimal(value); | |
| 332 | + } | |
| 333 | + | |
| 334 | + @Override | |
| 335 | + protected Integer serialize(BigDecimal value) { | |
| 336 | + return value.intValue(); | |
| 337 | + } | |
| 338 | + } | |
| 339 | + | |
| 340 | + private static class IntToDoubleCodec extends MappingCodec<Double, Integer> { | |
| 341 | + | |
| 342 | + IntToDoubleCodec() { | |
| 343 | + super(TypeCodec.cint(), Double.class); | |
| 344 | + } | |
| 345 | + | |
| 346 | + @Override | |
| 347 | + protected Double deserialize(Integer value) { | |
| 348 | + return value.doubleValue(); | |
| 349 | + } | |
| 350 | + | |
| 351 | + @Override | |
| 352 | + protected Integer serialize(Double value) { | |
| 353 | + return value.intValue(); | |
| 354 | + } | |
| 355 | + } | |
| 356 | + | |
| 357 | + private static class IntToFloatCodec extends MappingCodec<Float, Integer> { | |
| 358 | + | |
| 359 | + IntToFloatCodec() { | |
| 360 | + super(TypeCodec.cint(), Float.class); | |
| 361 | + } | |
| 362 | + | |
| 363 | + @Override | |
| 364 | + protected Float deserialize(Integer value) { | |
| 365 | + return value.floatValue(); | |
| 366 | + } | |
| 367 | + | |
| 368 | + @Override | |
| 369 | + protected Integer serialize(Float value) { | |
| 370 | + return value.intValue(); | |
| 371 | + } | |
| 372 | + } | |
| 373 | + | |
| 374 | + private static class IntToInetAddressCodec extends MappingCodec<InetAddress, Integer> { | |
| 375 | + | |
| 376 | + IntToInetAddressCodec() { | |
| 377 | + super(TypeCodec.cint(), InetAddress.class); | |
| 378 | + } | |
| 379 | + | |
| 380 | + @Override | |
| 381 | + protected InetAddress deserialize(Integer value) { | |
| 382 | + byte[] address = ByteBuffer.allocate(4).putInt(value).array(); | |
| 383 | + try { | |
| 384 | + return InetAddress.getByAddress(address); | |
| 385 | + } catch(UnknownHostException e) { | |
| 386 | + return null; | |
| 387 | + } | |
| 388 | + } | |
| 389 | + | |
| 390 | + @Override | |
| 391 | + protected Integer serialize(InetAddress value) { | |
| 392 | + return ByteBuffer.wrap(value.getAddress()).getInt(); | |
| 393 | + } | |
| 394 | + } | |
| 395 | + | |
| 396 | + private static class IntToByteCodec extends MappingCodec<Byte, Integer> { | |
| 397 | + | |
| 398 | + IntToByteCodec() { | |
| 399 | + super(TypeCodec.cint(), Byte.class); | |
| 400 | + } | |
| 401 | + | |
| 402 | + @Override | |
| 403 | + protected Byte deserialize(Integer value) { | |
| 404 | + return value.byteValue(); | |
| 405 | + } | |
| 406 | + | |
| 407 | + @Override | |
| 408 | + protected Integer serialize(Byte value) { | |
| 409 | + return value.intValue(); | |
| 410 | + } | |
| 411 | + } | |
| 412 | + | |
| 413 | + private static class IntToShortCodec extends MappingCodec<Short, Integer> { | |
| 414 | + | |
| 415 | + IntToShortCodec() { | |
| 416 | + super(TypeCodec.cint(), Short.class); | |
| 417 | + } | |
| 418 | + | |
| 419 | + @Override | |
| 420 | + protected Short deserialize(Integer value) { | |
| 421 | + return value.shortValue(); | |
| 422 | + } | |
| 423 | + | |
| 424 | + @Override | |
| 425 | + protected Integer serialize(Short value) { | |
| 426 | + return value.intValue(); | |
| 427 | + } | |
| 428 | + } | |
| 429 | + | |
| 430 | + private static class IntToDateCodec extends MappingCodec<Date, Integer> { | |
| 431 | + | |
| 432 | + IntToDateCodec() { | |
| 433 | + super(TypeCodec.cint(), Date.class); | |
| 434 | + } | |
| 435 | + | |
| 436 | + @Override | |
| 437 | + protected Date deserialize(Integer value) { | |
| 438 | + return new Date(value); | |
| 439 | + } | |
| 440 | + | |
| 441 | + @Override | |
| 442 | + protected Integer serialize(Date value) { | |
| 443 | + return new Long(value.getTime()).intValue(); | |
| 444 | + } | |
| 445 | + } | |
| 446 | + | |
| 447 | + private static class IntToLocalDateCodec extends MappingCodec<LocalDate, Integer> { | |
| 448 | + | |
| 449 | + IntToLocalDateCodec() { | |
| 450 | + super(TypeCodec.cint(), LocalDate.class); | |
| 451 | + } | |
| 452 | + | |
| 453 | + @Override | |
| 454 | + protected LocalDate deserialize(Integer value) { | |
| 455 | + return LocalDate.fromDaysSinceEpoch(value); | |
| 456 | + } | |
| 457 | + | |
| 458 | + @Override | |
| 459 | + protected Integer serialize(LocalDate value) { | |
| 460 | + return value.getDaysSinceEpoch(); | |
| 461 | + } | |
| 462 | + } | |
| 463 | + | |
| 464 | + private static class IntToUUIDCodec extends MappingCodec<UUID, Integer> { | |
| 465 | + | |
| 466 | + IntToUUIDCodec() { | |
| 467 | + super(TypeCodec.cint(), UUID.class); | |
| 468 | + } | |
| 469 | + | |
| 470 | + @Override | |
| 471 | + protected UUID deserialize(Integer value) { | |
| 472 | + return new UUID(value, 0); | |
| 473 | + } | |
| 474 | + | |
| 475 | + @Override | |
| 476 | + protected Integer serialize(UUID value) { | |
| 477 | + return new Long(value.getMostSignificantBits()).intValue(); | |
| 478 | + } | |
| 479 | + } | |
| 480 | + | |
| 481 | + private static class IntToBigIntegerCodec extends MappingCodec<BigInteger, Integer> { | |
| 482 | + | |
| 483 | + IntToBigIntegerCodec() { | |
| 484 | + super(TypeCodec.cint(), BigInteger.class); | |
| 485 | + } | |
| 486 | + | |
| 487 | + @Override | |
| 488 | + protected BigInteger deserialize(Integer value) { | |
| 489 | + return BigInteger.valueOf((long)value); | |
| 490 | + } | |
| 491 | + | |
| 492 | + @Override | |
| 493 | + protected Integer serialize(BigInteger value) { | |
| 494 | + return value.intValue(); | |
| 495 | + } | |
| 496 | + } | |
| 497 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/IndexMetadataTest.java +2 −1
| @@ -18,6 +18,7 @@ | ||
| 18 | 18 | import java.nio.ByteBuffer; |
| 19 | 19 | import java.util.Collection; |
| 20 | 20 | import java.util.List; |
| 21 | +import java.util.Map; | |
| 21 | 22 | |
| 22 | 23 | import com.google.common.collect.ImmutableList; |
| 23 | 24 | import com.google.common.collect.ImmutableMap; |
| @@ -60,7 +61,7 @@ | ||
| 60 | 61 | definition(IndexMetadata.KIND, text()), |
| 61 | 62 | definition(IndexMetadata.OPTIONS, map(text(), text())) |
| 62 | 63 | }); |
| 63 | - public static final TypeCodec.MapCodec<String, String> MAP_CODEC = new TypeCodec.MapCodec<String, String>(TypeCodec.VarcharCodec.instance, TypeCodec.VarcharCodec.instance); | |
| 64 | + public static final TypeCodec<Map<String, String>> MAP_CODEC = TypeCodec.map(TypeCodec.varchar(), TypeCodec.varchar()); | |
| 64 | 65 | |
| 65 | 66 | private static ProtocolVersion protocolVersion; |
| 66 | 67 | private static CodecRegistry codecRegistry; |
modified driver-core/src/test/java/com/datastax/driver/core/JodaCodecs.java +8 −11
| @@ -25,9 +25,6 @@ | ||
| 25 | 25 | |
| 26 | 26 | import static com.google.common.base.Preconditions.checkArgument; |
| 27 | 27 | |
| 28 | -import static com.datastax.driver.core.DataType.timestamp; | |
| 29 | -import static com.datastax.driver.core.DataType.varchar; | |
| 30 | - | |
| 31 | 28 | /** |
| 32 | 29 | * A collection of convenience {@link TypeCodec} instances useful for |
| 33 | 30 | * serializing between CQL types and Joda types like {@link DateTime}. |
| @@ -58,12 +55,12 @@ | ||
| 58 | 55 | * IMPORTANT: {@link LocalTime} as millisecond precision; nanoseconds below one millisecond will be lost |
| 59 | 56 | * during deserialization. |
| 60 | 57 | */ |
| 61 | - public static class LocalTimeCodec extends TypeCodec.MappingCodec<LocalTime, Long> { | |
| 58 | + public static class LocalTimeCodec extends MappingCodec<LocalTime, Long> { | |
| 62 | 59 | |
| 63 | 60 | public static final LocalTimeCodec instance = new LocalTimeCodec(); |
| 64 | 61 | |
| 65 | 62 | private LocalTimeCodec() { |
| 66 | - super(TimeCodec.instance, LocalTime.class); | |
| 63 | + super(TypeCodec.time(), LocalTime.class); | |
| 67 | 64 | } |
| 68 | 65 | |
| 69 | 66 | @Override |
| @@ -87,12 +84,12 @@ | ||
| 87 | 84 | * setting and retrieval of <code>date</code> columns as |
| 88 | 85 | * {@link org.joda.time.LocalDate} instances. |
| 89 | 86 | */ |
| 90 | - public static class LocalDateCodec extends TypeCodec.MappingCodec<org.joda.time.LocalDate, LocalDate> { | |
| 87 | + public static class LocalDateCodec extends MappingCodec<org.joda.time.LocalDate, LocalDate> { | |
| 91 | 88 | |
| 92 | 89 | public static final LocalDateCodec instance = new LocalDateCodec(); |
| 93 | 90 | |
| 94 | 91 | private LocalDateCodec() { |
| 95 | - super(DateCodec.instance, org.joda.time.LocalDate.class); | |
| 92 | + super(TypeCodec.date(), org.joda.time.LocalDate.class); | |
| 96 | 93 | } |
| 97 | 94 | |
| 98 | 95 | @Override |
| @@ -120,12 +117,12 @@ | ||
| 120 | 117 | * |
| 121 | 118 | * @see TimeZonePreservingDateTimeCodec |
| 122 | 119 | */ |
| 123 | - public static class DateTimeCodec extends TypeCodec.MappingCodec<DateTime, Date> { | |
| 120 | + public static class DateTimeCodec extends MappingCodec<DateTime, Date> { | |
| 124 | 121 | |
| 125 | 122 | public static final DateTimeCodec instance = new DateTimeCodec(); |
| 126 | 123 | |
| 127 | 124 | private DateTimeCodec() { |
| 128 | - super(TimestampCodec.instance, DateTime.class); | |
| 125 | + super(TypeCodec.timestamp(), DateTime.class); | |
| 129 | 126 | } |
| 130 | 127 | |
| 131 | 128 | @Override |
| @@ -158,7 +155,7 @@ | ||
| 158 | 155 | * </p> |
| 159 | 156 | * |
| 160 | 157 | */ |
| 161 | - public static class TimeZonePreservingDateTimeCodec extends TypeCodec.MappingCodec<DateTime, TupleValue> { | |
| 158 | + public static class TimeZonePreservingDateTimeCodec extends MappingCodec<DateTime, TupleValue> { | |
| 162 | 159 | |
| 163 | 160 | private final TupleType tupleType; |
| 164 | 161 | |
| @@ -167,7 +164,7 @@ | ||
| 167 | 164 | tupleType = (TupleType)innerCodec.getCqlType(); |
| 168 | 165 | List<DataType> types = tupleType.getComponentTypes(); |
| 169 | 166 | checkArgument( |
| 170 | - types.size() == 2 && types.get(0).equals(timestamp()) && types.get(1).equals(varchar()), | |
| 167 | + types.size() == 2 && types.get(0).equals(DataType.timestamp()) && types.get(1).equals(DataType.varchar()), | |
| 171 | 168 | "Expected tuple<timestamp,varchar>, got %s", |
| 172 | 169 | tupleType); |
| 173 | 170 | } |
modified driver-core/src/test/java/com/datastax/driver/core/JsonCodec.java +46 −9
| @@ -16,43 +16,80 @@ | ||
| 16 | 16 | package com.datastax.driver.core; |
| 17 | 17 | |
| 18 | 18 | import java.io.IOException; |
| 19 | +import java.nio.ByteBuffer; | |
| 19 | 20 | |
| 20 | 21 | import com.fasterxml.jackson.core.JsonProcessingException; |
| 22 | +import com.fasterxml.jackson.databind.JavaType; | |
| 21 | 23 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 24 | +import com.fasterxml.jackson.databind.type.TypeFactory; | |
| 22 | 25 | |
| 23 | 26 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 27 | +import com.datastax.driver.core.utils.Bytes; | |
| 24 | 28 | |
| 25 | 29 | /** |
| 26 | - * A simple Json codec for TypeCodec tests. | |
| 30 | + * A simple Json codec for TypeCodec tests that uses Jackson to perform serialization and deserialization. | |
| 27 | 31 | */ |
| 28 | -public class JsonCodec<T> extends TypeCodec.StringParsingCodec<T> { | |
| 32 | +public class JsonCodec<T> extends TypeCodec<T> { | |
| 29 | 33 | |
| 30 | 34 | private final ObjectMapper objectMapper = new ObjectMapper(); |
| 31 | 35 | |
| 32 | 36 | public JsonCodec(Class<T> javaType) { |
| 33 | - super(javaType); | |
| 37 | + super(DataType.varchar(), javaType); | |
| 34 | 38 | } |
| 35 | 39 | |
| 36 | 40 | @Override |
| 37 | - @SuppressWarnings("unchecked") | |
| 38 | - public T fromString(String value) { | |
| 41 | + public ByteBuffer serialize(T value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 39 | 42 | if (value == null) |
| 40 | 43 | return null; |
| 41 | 44 | try { |
| 42 | - return (T) objectMapper.readValue(value, getJavaType().getRawType()); | |
| 45 | + return ByteBuffer.wrap(objectMapper.writeValueAsBytes(value)); | |
| 46 | + } catch (JsonProcessingException e) { | |
| 47 | + throw new InvalidTypeException(e.getMessage(), e); | |
| 48 | + } | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Override | |
| 52 | + @SuppressWarnings("unchecked") | |
| 53 | + public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 54 | + if (bytes == null) | |
| 55 | + return null; | |
| 56 | + try { | |
| 57 | + return (T)objectMapper.readValue(Bytes.getArray(bytes), toJacksonJavaType()); | |
| 43 | 58 | } catch (IOException e) { |
| 44 | 59 | throw new InvalidTypeException(e.getMessage(), e); |
| 45 | 60 | } |
| 46 | 61 | } |
| 47 | 62 | |
| 48 | 63 | @Override |
| 49 | - public String toString(T value) { | |
| 64 | + public String format(T value) throws InvalidTypeException { | |
| 50 | 65 | if (value == null) |
| 66 | + return "NULL"; | |
| 67 | + String json; | |
| 68 | + try { | |
| 69 | + json = objectMapper.writeValueAsString(value); | |
| 70 | + } catch (IOException e) { | |
| 71 | + throw new InvalidTypeException(e.getMessage(), e); | |
| 72 | + } | |
| 73 | + return '\'' + json.replace("\'", "''") + '\''; | |
| 74 | + } | |
| 75 | + | |
| 76 | + @Override | |
| 77 | + @SuppressWarnings("unchecked") | |
| 78 | + public T parse(String value) throws InvalidTypeException { | |
| 79 | + if (value == null || value.isEmpty() || value.equals("NULL")) | |
| 51 | 80 | return null; |
| 81 | + if (value.charAt(0) != '\'' || value.charAt(value.length() - 1) != '\'') | |
| 82 | + throw new InvalidTypeException("JSON strings must be enclosed by single quotes"); | |
| 83 | + String json = value.substring(1, value.length() - 1).replace("''", "'"); | |
| 52 | 84 | try { |
| 53 | - return objectMapper.writeValueAsString(value); | |
| 54 | - } catch (JsonProcessingException e) { | |
| 85 | + return (T)objectMapper.readValue(json, toJacksonJavaType()); | |
| 86 | + } catch (IOException e) { | |
| 55 | 87 | throw new InvalidTypeException(e.getMessage(), e); |
| 56 | 88 | } |
| 57 | 89 | } |
| 90 | + | |
| 91 | + protected JavaType toJacksonJavaType() { | |
| 92 | + return TypeFactory.defaultInstance().constructType(getJavaType().getType()); | |
| 93 | + } | |
| 94 | + | |
| 58 | 95 | } |
added driver-core/src/test/java/com/datastax/driver/core/MappingCodec.java +69 −0
| @@ -0,0 +1,69 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 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.driver.core; | |
| 17 | + | |
| 18 | +import java.nio.ByteBuffer; | |
| 19 | + | |
| 20 | +import com.google.common.reflect.TypeToken; | |
| 21 | + | |
| 22 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * An abstract TypeCodec that maps a Java Pojo to another Java object | |
| 26 | + * that can in turn be serialized into a CQL type. | |
| 27 | + * This can serve as a base for libraries dealing with Pojo mappings. | |
| 28 | + * | |
| 29 | + * @param <T> The outer Java type | |
| 30 | + * @param <U> The inner Java type | |
| 31 | + */ | |
| 32 | +public abstract class MappingCodec<T, U> extends TypeCodec<T> { | |
| 33 | + | |
| 34 | + protected final TypeCodec<U> innerCodec; | |
| 35 | + | |
| 36 | + public MappingCodec(TypeCodec<U> innerCodec, Class<T> javaType) { | |
| 37 | + this(innerCodec, TypeToken.of(javaType)); | |
| 38 | + } | |
| 39 | + | |
| 40 | + public MappingCodec(TypeCodec<U> innerCodec, TypeToken<T> javaType) { | |
| 41 | + super(innerCodec.getCqlType(), javaType); | |
| 42 | + this.innerCodec = innerCodec; | |
| 43 | + } | |
| 44 | + | |
| 45 | + @Override | |
| 46 | + public ByteBuffer serialize(T value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 47 | + return innerCodec.serialize(serialize(value), protocolVersion); | |
| 48 | + } | |
| 49 | + | |
| 50 | + @Override | |
| 51 | + public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 52 | + return deserialize(innerCodec.deserialize(bytes, protocolVersion)); | |
| 53 | + } | |
| 54 | + | |
| 55 | + @Override | |
| 56 | + public T parse(String value) throws InvalidTypeException { | |
| 57 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : deserialize(innerCodec.parse(value)); | |
| 58 | + } | |
| 59 | + | |
| 60 | + @Override | |
| 61 | + public String format(T value) throws InvalidTypeException { | |
| 62 | + return value == null ? null : innerCodec.format(serialize(value)); | |
| 63 | + } | |
| 64 | + | |
| 65 | + protected abstract T deserialize(U value); | |
| 66 | + | |
| 67 | + protected abstract U serialize(T value); | |
| 68 | + | |
| 69 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/OptionalCodec.java +3 −2
| @@ -23,9 +23,10 @@ | ||
| 23 | 23 | import com.google.common.reflect.TypeParameter; |
| 24 | 24 | import com.google.common.reflect.TypeToken; |
| 25 | 25 | |
| 26 | -import static com.google.common.base.Predicates.*; | |
| 26 | +import static com.google.common.base.Predicates.isNull; | |
| 27 | +import static com.google.common.base.Predicates.or; | |
| 27 | 28 | |
| 28 | -public class OptionalCodec<T> extends TypeCodec.MappingCodec<Optional<T>, T> { | |
| 29 | +public class OptionalCodec<T> extends MappingCodec<Optional<T>, T> { | |
| 29 | 30 | |
| 30 | 31 | private final Predicate<T> isAbsent; |
| 31 | 32 |
modified driver-core/src/test/java/com/datastax/driver/core/PreparedStatementTest.java +17 −9
| @@ -34,7 +34,7 @@ | ||
| 34 | 34 | import static com.datastax.driver.core.TestUtils.getFixedValue; |
| 35 | 35 | import static com.datastax.driver.core.TestUtils.getFixedValue2; |
| 36 | 36 | import static com.datastax.driver.core.TestUtils.getValue; |
| 37 | -import static com.datastax.driver.core.TestUtils.setBoundValue; | |
| 37 | +import static com.datastax.driver.core.TestUtils.setValue; | |
| 38 | 38 | |
| 39 | 39 | /** |
| 40 | 40 | * Prepared statement tests. |
| @@ -131,7 +131,8 @@ | ||
| 131 | 131 | String name = "c_" + type; |
| 132 | 132 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_native', ?)", ALL_NATIVE_TABLE, name)); |
| 133 | 133 | BoundStatement bs = ps.bind(); |
| 134 | - session.execute(setBoundValue(bs, name, type, getFixedValue(type))); | |
| 134 | + setValue(bs, name, type, getFixedValue(type)); | |
| 135 | + session.execute(bs); | |
| 135 | 136 | |
| 136 | 137 | Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_native'", name, ALL_NATIVE_TABLE)).one(); |
| 137 | 138 | assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), getFixedValue(type), "For type " + type); |
| @@ -152,7 +153,8 @@ | ||
| 152 | 153 | String name = "c_" + type; |
| 153 | 154 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_native', ?)", ALL_NATIVE_TABLE, name)); |
| 154 | 155 | BoundStatement bs = ps.bind(); |
| 155 | - session.execute(setBoundValue(bs, name, type, getFixedValue2(type))); | |
| 156 | + setValue(bs, name, type, getFixedValue2(type)); | |
| 157 | + session.execute(bs); | |
| 156 | 158 | |
| 157 | 159 | Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_native'", name, ALL_NATIVE_TABLE)).one(); |
| 158 | 160 | assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), getFixedValue2(type), "For type " + type); |
| @@ -173,7 +175,8 @@ | ||
| 173 | 175 | List<Object> value = (List<Object>)getFixedValue(type); |
| 174 | 176 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name)); |
| 175 | 177 | BoundStatement bs = ps.bind(); |
| 176 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 178 | + setValue(bs, name, type, value); | |
| 179 | + session.execute(bs); | |
| 177 | 180 | |
| 178 | 181 | Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_list'", name, ALL_LIST_TABLE)).one(); |
| 179 | 182 | assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type); |
| @@ -197,7 +200,8 @@ | ||
| 197 | 200 | List<Object> value = (List<Object>)getFixedValue2(type); |
| 198 | 201 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_list', ?)", ALL_LIST_TABLE, name)); |
| 199 | 202 | BoundStatement bs = ps.bind(); |
| 200 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 203 | + setValue(bs, name, type, value); | |
| 204 | + session.execute(bs); | |
| 201 | 205 | |
| 202 | 206 | Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_list'", name, ALL_LIST_TABLE)).one(); |
| 203 | 207 | assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type); |
| @@ -218,7 +222,8 @@ | ||
| 218 | 222 | Set<Object> value = (Set<Object>)getFixedValue(type); |
| 219 | 223 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name)); |
| 220 | 224 | BoundStatement bs = ps.bind(); |
| 221 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 225 | + setValue(bs, name, type, value); | |
| 226 | + session.execute(bs); | |
| 222 | 227 | |
| 223 | 228 | Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_set'", name, ALL_SET_TABLE)).one(); |
| 224 | 229 | assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type); |
| @@ -242,7 +247,8 @@ | ||
| 242 | 247 | Set<Object> value = (Set<Object>)getFixedValue2(type); |
| 243 | 248 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_set', ?)", ALL_SET_TABLE, name)); |
| 244 | 249 | BoundStatement bs = ps.bind(); |
| 245 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 250 | + setValue(bs, name, type, value); | |
| 251 | + session.execute(bs); | |
| 246 | 252 | |
| 247 | 253 | Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_set'", name, ALL_SET_TABLE)).one(); |
| 248 | 254 | assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type); |
| @@ -268,7 +274,8 @@ | ||
| 268 | 274 | Map<Object, Object> value = (Map<Object, Object>)getFixedValue(type); |
| 269 | 275 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name)); |
| 270 | 276 | BoundStatement bs = ps.bind(); |
| 271 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 277 | + setValue(bs, name, type, value); | |
| 278 | + session.execute(bs); | |
| 272 | 279 | |
| 273 | 280 | Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_map'", name, ALL_MAP_TABLE)).one(); |
| 274 | 281 | assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type); |
| @@ -298,7 +305,8 @@ | ||
| 298 | 305 | Map<Object, Object> value = (Map<Object, Object>)getFixedValue2(type); |
| 299 | 306 | PreparedStatement ps = session.prepare(String.format("INSERT INTO %s(k, %s) VALUES ('prepared_map', ?)", ALL_MAP_TABLE, name)); |
| 300 | 307 | BoundStatement bs = ps.bind(); |
| 301 | - session.execute(setBoundValue(bs, name, type, value)); | |
| 308 | + setValue(bs, name, type, value); | |
| 309 | + session.execute(bs); | |
| 302 | 310 | |
| 303 | 311 | Row row = session.execute(String.format("SELECT %s FROM %s WHERE k='prepared_map'", name, ALL_MAP_TABLE)).one(); |
| 304 | 312 | assertEquals(getValue(row, name, type, cluster.getConfiguration().getCodecRegistry()), value, "For type " + type); |
modified driver-core/src/test/java/com/datastax/driver/core/SchemaAgreementTest.java +1 −1
| @@ -102,7 +102,7 @@ | ||
| 102 | 102 | |
| 103 | 103 | private static void forceSchemaVersion(Session session, InetAddress peerAddress, UUID schemaVersion) { |
| 104 | 104 | session.execute(String.format("UPDATE system.peers SET schema_version = %s WHERE peer = %s", |
| 105 | - TypeCodec.UUIDCodec.instance.format(schemaVersion), TypeCodec.InetCodec.instance.format(peerAddress))); | |
| 105 | + TypeCodec.uuid().format(schemaVersion), TypeCodec.inet().format(peerAddress))); | |
| 106 | 106 | } |
| 107 | 107 | |
| 108 | 108 | @AfterMethod(groups = "short") |
added driver-core/src/test/java/com/datastax/driver/core/StringParsingCodec.java +94 −0
| @@ -0,0 +1,94 @@ | ||
| 1 | +package com.datastax.driver.core; | |
| 2 | + | |
| 3 | +import java.nio.ByteBuffer; | |
| 4 | + | |
| 5 | +import com.google.common.reflect.TypeToken; | |
| 6 | + | |
| 7 | +/* | |
| 8 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 9 | + * | |
| 10 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 11 | + * you may not use this file except in compliance with the License. | |
| 12 | + * You may obtain a copy of the License at | |
| 13 | + * | |
| 14 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 15 | + * | |
| 16 | + * Unless required by applicable law or agreed to in writing, software | |
| 17 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 18 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 19 | + * See the License for the specific language governing permissions and | |
| 20 | + * limitations under the License. | |
| 21 | + */ | |
| 22 | +import com.datastax.driver.core.exceptions.InvalidTypeException; | |
| 23 | + | |
| 24 | +/** | |
| 25 | + * An abstract TypeCodec that actually stores objects as serialized strings. | |
| 26 | + * This can serve as a base for codecs dealing with XML or JSON formats. | |
| 27 | + * | |
| 28 | + * @param <T> The Java type this codec serializes from and deserializes to. | |
| 29 | + */ | |
| 30 | +public abstract class StringParsingCodec<T> extends TypeCodec<T> { | |
| 31 | + | |
| 32 | + private final TypeCodec<String> innerCodec; | |
| 33 | + | |
| 34 | + public StringParsingCodec(Class<T> javaType) { | |
| 35 | + this(TypeToken.of(javaType)); | |
| 36 | + } | |
| 37 | + | |
| 38 | + public StringParsingCodec(TypeToken<T> javaType) { | |
| 39 | + this(TypeCodec.varchar(), javaType); | |
| 40 | + } | |
| 41 | + | |
| 42 | + public StringParsingCodec(TypeCodec<String> innerCodec, Class<T> javaType) { | |
| 43 | + this(innerCodec, TypeToken.of(javaType)); | |
| 44 | + } | |
| 45 | + | |
| 46 | + public StringParsingCodec(TypeCodec<String> innerCodec, TypeToken<T> javaType) { | |
| 47 | + super(innerCodec.getCqlType(), javaType); | |
| 48 | + this.innerCodec = innerCodec; | |
| 49 | + } | |
| 50 | + | |
| 51 | + @Override | |
| 52 | + public ByteBuffer serialize(T value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 53 | + return value == null ? null : innerCodec.serialize(toString(value), protocolVersion); | |
| 54 | + } | |
| 55 | + | |
| 56 | + @Override | |
| 57 | + public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 58 | + String value = innerCodec.deserialize(bytes, protocolVersion); | |
| 59 | + return value == null ? null : fromString(value); | |
| 60 | + } | |
| 61 | + | |
| 62 | + @Override | |
| 63 | + public String format(T value) throws InvalidTypeException { | |
| 64 | + return value == null ? null : innerCodec.format(toString(value)); | |
| 65 | + } | |
| 66 | + | |
| 67 | + @Override | |
| 68 | + public T parse(String value) throws InvalidTypeException { | |
| 69 | + return value == null || value.isEmpty() || value.equals("NULL") ? null : fromString(innerCodec.parse(value)); | |
| 70 | + } | |
| 71 | + | |
| 72 | + /** | |
| 73 | + * Return the String representation of the given object; | |
| 74 | + * no special CQL quoting should be applied here. | |
| 75 | + * Null values should be accepted; in most cases, implementors | |
| 76 | + * should return null for null inputs. | |
| 77 | + * | |
| 78 | + * @param value the value to convert into a string | |
| 79 | + * @return the String representation of the given object | |
| 80 | + */ | |
| 81 | + protected abstract String toString(T value); | |
| 82 | + | |
| 83 | + /** | |
| 84 | + * Parse the given string into an object; | |
| 85 | + * no special CQL unquoting should be applied here. | |
| 86 | + * Null values should be accepted; in most cases, implementors | |
| 87 | + * should return null for null inputs. | |
| 88 | + * | |
| 89 | + * @param value the string to parse | |
| 90 | + * @return the parsed object. | |
| 91 | + */ | |
| 92 | + protected abstract T fromString(String value); | |
| 93 | + | |
| 94 | +} |
modified driver-core/src/test/java/com/datastax/driver/core/TestUtils.java +179 −48
| @@ -51,136 +51,267 @@ | ||
| 51 | 51 | |
| 52 | 52 | public static final int TEST_BASE_NODE_WAIT = SystemProperties.getInt("com.datastax.driver.TEST_BASE_NODE_WAIT", 60); |
| 53 | 53 | |
| 54 | + public static void setValue(SettableByIndexData<?> data, int i, DataType type, Object value) { | |
| 55 | + switch (type.getName()) { | |
| 56 | + case ASCII: | |
| 57 | + data.setString(i, (String)value); | |
| 58 | + break; | |
| 59 | + case BIGINT: | |
| 60 | + data.setLong(i, (Long)value); | |
| 61 | + break; | |
| 62 | + case BLOB: | |
| 63 | + data.setBytes(i, (ByteBuffer)value); | |
| 64 | + break; | |
| 65 | + case BOOLEAN: | |
| 66 | + data.setBool(i, (Boolean)value); | |
| 67 | + break; | |
| 68 | + case COUNTER: | |
| 69 | + // Just a no-op, we shouldn't handle counters the same way than other types | |
| 70 | + break; | |
| 71 | + case DECIMAL: | |
| 72 | + data.setDecimal(i, (BigDecimal)value); | |
| 73 | + break; | |
| 74 | + case DOUBLE: | |
| 75 | + data.setDouble(i, (Double)value); | |
| 76 | + break; | |
| 77 | + case FLOAT: | |
| 78 | + data.setFloat(i, (Float)value); | |
| 79 | + break; | |
| 80 | + case INET: | |
| 81 | + data.setInet(i, (InetAddress)value); | |
| 82 | + break; | |
| 83 | + case TINYINT: | |
| 84 | + data.setByte(i, (Byte) value); | |
| 85 | + break; | |
| 86 | + case SMALLINT: | |
| 87 | + data.setShort(i, (Short) value); | |
| 88 | + break; | |
| 89 | + case INT: | |
| 90 | + data.setInt(i, (Integer) value); | |
| 91 | + break; | |
| 92 | + case TEXT: | |
| 93 | + data.setString(i, (String) value); | |
| 94 | + break; | |
| 95 | + case TIMESTAMP: | |
| 96 | + data.setTimestamp(i, (Date) value); | |
| 97 | + break; | |
| 98 | + case DATE: | |
| 99 | + data.setDate(i, (LocalDate)value); | |
| 100 | + break; | |
| 101 | + case TIME: | |
| 102 | + data.setTime(i, (Long) value); | |
| 103 | + break; | |
| 104 | + case UUID: | |
| 105 | + data.setUUID(i, (UUID)value); | |
| 106 | + break; | |
| 107 | + case VARCHAR: | |
| 108 | + data.setString(i, (String)value); | |
| 109 | + break; | |
| 110 | + case VARINT: | |
| 111 | + data.setVarint(i, (BigInteger)value); | |
| 112 | + break; | |
| 113 | + case TIMEUUID: | |
| 114 | + data.setUUID(i, (UUID)value); | |
| 115 | + break; | |
| 116 | + case LIST: | |
| 117 | + data.setList(i, (List)value); | |
| 118 | + break; | |
| 119 | + case SET: | |
| 120 | + data.setSet(i, (Set)value); | |
| 121 | + break; | |
| 122 | + case MAP: | |
| 123 | + data.setMap(i, (Map)value); | |
| 124 | + break; | |
| 125 | + default: | |
| 126 | + throw new RuntimeException("Missing handling of " + type); | |
| 127 | + } | |
| 128 | + } | |
| 129 | + | |
| 54 | 130 | @SuppressWarnings({ "unchecked", "rawtypes" }) |
| 55 | - public static BoundStatement setBoundValue(BoundStatement bs, String name, DataType type, Object value) { | |
| 131 | + public static void setValue(SettableByNameData<?> data, String name, DataType type, Object value) { | |
| 56 | 132 | switch (type.getName()) { |
| 57 | 133 | case ASCII: |
| 58 | - bs.setString(name, (String)value); | |
| 134 | + data.setString(name, (String)value); | |
| 59 | 135 | break; |
| 60 | 136 | case BIGINT: |
| 61 | - bs.setLong(name, (Long)value); | |
| 137 | + data.setLong(name, (Long)value); | |
| 62 | 138 | break; |
| 63 | 139 | case BLOB: |
| 64 | - bs.setBytes(name, (ByteBuffer)value); | |
| 140 | + data.setBytes(name, (ByteBuffer)value); | |
| 65 | 141 | break; |
| 66 | 142 | case BOOLEAN: |
| 67 | - bs.setBool(name, (Boolean)value); | |
| 143 | + data.setBool(name, (Boolean)value); | |
| 68 | 144 | break; |
| 69 | 145 | case COUNTER: |
| 70 | 146 | // Just a no-op, we shouldn't handle counters the same way than other types |
| 71 | 147 | break; |
| 72 | 148 | case DECIMAL: |
| 73 | - bs.setDecimal(name, (BigDecimal)value); | |
| 149 | + data.setDecimal(name, (BigDecimal)value); | |
| 74 | 150 | break; |
| 75 | 151 | case DOUBLE: |
| 76 | - bs.setDouble(name, (Double)value); | |
| 152 | + data.setDouble(name, (Double)value); | |
| 77 | 153 | break; |
| 78 | 154 | case FLOAT: |
| 79 | - bs.setFloat(name, (Float)value); | |
| 155 | + data.setFloat(name, (Float)value); | |
| 80 | 156 | break; |
| 81 | 157 | case INET: |
| 82 | - bs.setInet(name, (InetAddress)value); | |
| 158 | + data.setInet(name, (InetAddress)value); | |
| 83 | 159 | break; |
| 84 | 160 | case TINYINT: |
| 85 | - bs.setByte(name, (Byte) value); | |
| 161 | + data.setByte(name, (Byte) value); | |
| 86 | 162 | break; |
| 87 | 163 | case SMALLINT: |
| 88 | - bs.setShort(name, (Short) value); | |
| 164 | + data.setShort(name, (Short) value); | |
| 89 | 165 | break; |
| 90 | 166 | case INT: |
| 91 | - bs.setInt(name, (Integer) value); | |
| 167 | + data.setInt(name, (Integer) value); | |
| 92 | 168 | break; |
| 93 | 169 | case TEXT: |
| 94 | - bs.setString(name, (String) value); | |
| 170 | + data.setString(name, (String) value); | |
| 95 | 171 | break; |
| 96 | 172 | case TIMESTAMP: |
| 97 | - bs.setTimestamp(name, (Date) value); | |
| 173 | + data.setTimestamp(name, (Date) value); | |
| 98 | 174 | break; |
| 99 | 175 | case DATE: |
| 100 | - bs.setDate(name, (LocalDate)value); | |
| 176 | + data.setDate(name, (LocalDate)value); | |
| 101 | 177 | break; |
| 102 | 178 | case TIME: |
| 103 | - bs.setTime(name, (Long) value); | |
| 179 | + data.setTime(name, (Long) value); | |
| 104 | 180 | break; |
| 105 | 181 | case UUID: |
| 106 | - bs.setUUID(name, (UUID)value); | |
| 182 | + data.setUUID(name, (UUID)value); | |
| 107 | 183 | break; |
| 108 | 184 | case VARCHAR: |
| 109 | - bs.setString(name, (String)value); | |
| 185 | + data.setString(name, (String)value); | |
| 110 | 186 | break; |
| 111 | 187 | case VARINT: |
| 112 | - bs.setVarint(name, (BigInteger)value); | |
| 188 | + data.setVarint(name, (BigInteger)value); | |
| 113 | 189 | break; |
| 114 | 190 | case TIMEUUID: |
| 115 | - bs.setUUID(name, (UUID)value); | |
| 191 | + data.setUUID(name, (UUID)value); | |
| 116 | 192 | break; |
| 117 | 193 | case LIST: |
| 118 | - bs.setList(name, (List)value); | |
| 194 | + data.setList(name, (List)value); | |
| 119 | 195 | break; |
| 120 | 196 | case SET: |
| 121 | - bs.setSet(name, (Set)value); | |
| 197 | + data.setSet(name, (Set)value); | |
| 122 | 198 | break; |
| 123 | 199 | case MAP: |
| 124 | - bs.setMap(name, (Map)value); | |
| 200 | + data.setMap(name, (Map)value); | |
| 125 | 201 | break; |
| 126 | 202 | default: |
| 127 | 203 | throw new RuntimeException("Missing handling of " + type); |
| 128 | 204 | } |
| 129 | - return bs; | |
| 130 | 205 | } |
| 131 | 206 | |
| 132 | - public static Object getValue(Row row, String name, DataType type, CodecRegistry codecRegistry) { | |
| 207 | + public static Object getValue(GettableByIndexData data, int i, DataType type, CodecRegistry codecRegistry) { | |
| 208 | + switch (type.getName()) { | |
| 209 | + case ASCII: | |
| 210 | + return data.getString(i); | |
| 211 | + case BIGINT: | |
| 212 | + return data.getLong(i); | |
| 213 | + case BLOB: | |
| 214 | + return data.getBytes(i); | |
| 215 | + case BOOLEAN: | |
| 216 | + return data.getBool(i); | |
| 217 | + case COUNTER: | |
| 218 | + return data.getLong(i); | |
| 219 | + case DECIMAL: | |
| 220 | + return data.getDecimal(i); | |
| 221 | + case DOUBLE: | |
| 222 | + return data.getDouble(i); | |
| 223 | + case FLOAT: | |
| 224 | + return data.getFloat(i); | |
| 225 | + case INET: | |
| 226 | + return data.getInet(i); | |
| 227 | + case TINYINT: | |
| 228 | + return data.getByte(i); | |
| 229 | + case SMALLINT: | |
| 230 | + return data.getShort(i); | |
| 231 | + case INT: | |
| 232 | + return data.getInt(i); | |
| 233 | + case TEXT: | |
| 234 | + return data.getString(i); | |
| 235 | + case TIMESTAMP: | |
| 236 | + return data.getTimestamp(i); | |
| 237 | + case DATE: | |
| 238 | + return data.getDate(i); | |
| 239 | + case TIME: | |
| 240 | + return data.getTime(i); | |
| 241 | + case UUID: | |
| 242 | + return data.getUUID(i); | |
| 243 | + case VARCHAR: | |
| 244 | + return data.getString(i); | |
| 245 | + case VARINT: | |
| 246 | + return data.getVarint(i); | |
| 247 | + case TIMEUUID: | |
| 248 | + return data.getUUID(i); | |
| 249 | + case LIST: | |
| 250 | + Class<?> listEltClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType(); | |
| 251 | + return data.getList(i, listEltClass); | |
| 252 | + case SET: | |
| 253 | + Class<?> setEltClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType(); | |
| 254 | + return data.getSet(i, setEltClass); | |
| 255 | + case MAP: | |
| 256 | + Class<?> keyClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType(); | |
| 257 | + Class<?> valueClass = codecRegistry.codecFor(type.getTypeArguments().get(1)).getJavaType().getRawType(); | |
| 258 | + return data.getMap(i, keyClass, valueClass); | |
| 259 | + } | |
| 260 | + throw new RuntimeException("Missing handling of " + type); | |
| 261 | + } | |
| 262 | + | |
| 263 | + public static Object getValue(GettableByNameData data, String name, DataType type, CodecRegistry codecRegistry) { | |
| 133 | 264 | switch (type.getName()) { |
| 134 | 265 | case ASCII: |
| 135 | - return row.getString(name); | |
| 266 | + return data.getString(name); | |
| 136 | 267 | case BIGINT: |
| 137 | - return row.getLong(name); | |
| 268 | + return data.getLong(name); | |
| 138 | 269 | case BLOB: |
| 139 | - return row.getBytes(name); | |
| 270 | + return data.getBytes(name); | |
| 140 | 271 | case BOOLEAN: |
| 141 | - return row.getBool(name); | |
| 272 | + return data.getBool(name); | |
| 142 | 273 | case COUNTER: |
| 143 | - return row.getLong(name); | |
| 274 | + return data.getLong(name); | |
| 144 | 275 | case DECIMAL: |
| 145 | - return row.getDecimal(name); | |
| 276 | + return data.getDecimal(name); | |
| 146 | 277 | case DOUBLE: |
| 147 | - return row.getDouble(name); | |
| 278 | + return data.getDouble(name); | |
| 148 | 279 | case FLOAT: |
| 149 | - return row.getFloat(name); | |
| 280 | + return data.getFloat(name); | |
| 150 | 281 | case INET: |
| 151 | - return row.getInet(name); | |
| 282 | + return data.getInet(name); | |
| 152 | 283 | case TINYINT: |
| 153 | - return row.getByte(name); | |
| 284 | + return data.getByte(name); | |
| 154 | 285 | case SMALLINT: |
| 155 | - return row.getShort(name); | |
| 286 | + return data.getShort(name); | |
| 156 | 287 | case INT: |
| 157 | - return row.getInt(name); | |
| 288 | + return data.getInt(name); | |
| 158 | 289 | case TEXT: |
| 159 | - return row.getString(name); | |
| 290 | + return data.getString(name); | |
| 160 | 291 | case TIMESTAMP: |
| 161 | - return row.getTimestamp(name); | |
| 292 | + return data.getTimestamp(name); | |
| 162 | 293 | case DATE: |
| 163 | - return row.getDate(name); | |
| 294 | + return data.getDate(name); | |
| 164 | 295 | case TIME: |
| 165 | - return row.getTime(name); | |
| 296 | + return data.getTime(name); | |
| 166 | 297 | case UUID: |
| 167 | - return row.getUUID(name); | |
| 298 | + return data.getUUID(name); | |
| 168 | 299 | case VARCHAR: |
| 169 | - return row.getString(name); | |
| 300 | + return data.getString(name); | |
| 170 | 301 | case VARINT: |
| 171 | - return row.getVarint(name); | |
| 302 | + return data.getVarint(name); | |
| 172 | 303 | case TIMEUUID: |
| 173 | - return row.getUUID(name); | |
| 304 | + return data.getUUID(name); | |
| 174 | 305 | case LIST: |
| 175 | 306 | Class<?> listEltClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType(); |
| 176 | - return row.getList(name, listEltClass); | |
| 307 | + return data.getList(name, listEltClass); | |
| 177 | 308 | case SET: |
| 178 | 309 | Class<?> setEltClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType(); |
| 179 | - return row.getSet(name, setEltClass); | |
| 310 | + return data.getSet(name, setEltClass); | |
| 180 | 311 | case MAP: |
| 181 | 312 | Class<?> keyClass = codecRegistry.codecFor(type.getTypeArguments().get(0)).getJavaType().getRawType(); |
| 182 | 313 | Class<?> valueClass = codecRegistry.codecFor(type.getTypeArguments().get(1)).getJavaType().getRawType(); |
| 183 | - return row.getMap(name, keyClass, valueClass); | |
| 314 | + return data.getMap(name, keyClass, valueClass); | |
| 184 | 315 | } |
| 185 | 316 | throw new RuntimeException("Missing handling of " + type); |
| 186 | 317 | } |
modified driver-core/src/test/java/com/datastax/driver/core/TokenIntegrationTest.java +1 −1
| @@ -119,7 +119,7 @@ | ||
| 119 | 119 | |
| 120 | 120 | // Find the replica for a given partition key |
| 121 | 121 | int testKey = 1; |
| 122 | - Set<Host> replicas = metadata.getReplicas("test", TypeCodec.IntCodec.instance.serialize(testKey, cluster.getConfiguration().getProtocolOptions().getProtocolVersion())); | |
| 122 | + Set<Host> replicas = metadata.getReplicas("test", TypeCodec.cint().serialize(testKey, cluster.getConfiguration().getProtocolOptions().getProtocolVersion())); | |
| 123 | 123 | assertThat(replicas).hasSize(1); |
| 124 | 124 | Host replica = replicas.iterator().next(); |
| 125 | 125 |
modified driver-core/src/test/java/com/datastax/driver/core/TupleTest.java +1 −1
| @@ -55,7 +55,7 @@ | ||
| 55 | 55 | assertEquals(v.getString(1), "a"); |
| 56 | 56 | assertEquals(v.getFloat(2), 1.0f); |
| 57 | 57 | |
| 58 | - assertEquals(new TypeCodec.TupleCodec(t).format(v), "(1, 'a', 1.0)"); | |
| 58 | + assertEquals(TypeCodec.tuple(t).format(v), "(1, 'a', 1.0)"); | |
| 59 | 59 | } |
| 60 | 60 | |
| 61 | 61 | @Test(groups = "short") |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecCollectionsIntegrationTest.java +0 −19
| @@ -126,25 +126,6 @@ | ||
| 126 | 126 | |
| 127 | 127 | @Test(groups = "short") |
| 128 | 128 | public void should_use_collection_codecs_with_prepared_statements_3() { |
| 129 | - session.execute(session.prepare(insertQuery).bind() | |
| 130 | - .setInt(0, n_int) | |
| 131 | - .setObject(1, l_int) | |
| 132 | - .setObject(2, l_bigint) | |
| 133 | - .setObject(3, s_float) | |
| 134 | - .setObject(4, s_double) | |
| 135 | - .setObject(5, m_varint) | |
| 136 | - .setObject(6, m_decimal) | |
| 137 | - ); | |
| 138 | - PreparedStatement ps = session.prepare(selectQuery); | |
| 139 | - ResultSet rows = session.execute(ps.bind() | |
| 140 | - .setInt(0, n_int) | |
| 141 | - ); | |
| 142 | - Row row = rows.one(); | |
| 143 | - assertRow(row); | |
| 144 | - } | |
| 145 | - | |
| 146 | - @Test(groups = "short") | |
| 147 | - public void should_use_collection_codecs_with_prepared_statements_4() { | |
| 148 | 129 | session.execute(session.prepare(insertQuery).bind() |
| 149 | 130 | .setInt(0, n_int) |
| 150 | 131 | .set(1, l_int, listOf(Integer.class)) |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecEncapsulationIntegrationTest.java +6 −45
| @@ -29,7 +29,6 @@ | ||
| 29 | 29 | import static com.google.common.collect.Lists.newArrayList; |
| 30 | 30 | import static org.assertj.core.api.Assertions.assertThat; |
| 31 | 31 | |
| 32 | -import com.datastax.driver.core.TypeCodec.*; | |
| 33 | 32 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| 34 | 33 | import com.datastax.driver.core.querybuilder.BuiltStatement; |
| 35 | 34 | import com.datastax.driver.core.querybuilder.QueryBuilder; |
| @@ -74,12 +73,12 @@ | ||
| 74 | 73 | return builder.withCodecRegistry( |
| 75 | 74 | new CodecRegistry() |
| 76 | 75 | .register( |
| 77 | - new NumberBoxCodec<Integer>(IntCodec.instance), | |
| 78 | - new NumberBoxCodec<Long>(BigintCodec.instance), | |
| 79 | - new NumberBoxCodec<Float>(FloatCodec.instance), | |
| 80 | - new NumberBoxCodec<Double>(DoubleCodec.instance), | |
| 81 | - new NumberBoxCodec<BigInteger>(VarintCodec.instance), | |
| 82 | - new NumberBoxCodec<BigDecimal>(DecimalCodec.instance) | |
| 76 | + new NumberBoxCodec<Integer>(TypeCodec.cint()), | |
| 77 | + new NumberBoxCodec<Long>(TypeCodec.bigint()), | |
| 78 | + new NumberBoxCodec<Float>(TypeCodec.cfloat()), | |
| 79 | + new NumberBoxCodec<Double>(TypeCodec.cdouble()), | |
| 80 | + new NumberBoxCodec<BigInteger>(TypeCodec.varint()), | |
| 81 | + new NumberBoxCodec<BigDecimal>(TypeCodec.decimal()) | |
| 83 | 82 | ) |
| 84 | 83 | ); |
| 85 | 84 | } |
| @@ -131,25 +130,6 @@ | ||
| 131 | 130 | |
| 132 | 131 | @Test(groups = "short") |
| 133 | 132 | public void should_use_custom_codecs_with_prepared_statements_2() { |
| 134 | - session.execute(session.prepare(insertQuery).bind() | |
| 135 | - .setObject(0, new NumberBox<Integer>(n_int)) | |
| 136 | - .setObject(1, new NumberBox<Long>(n_bigint)) | |
| 137 | - .setObject(2, new NumberBox<Float>(n_float)) | |
| 138 | - .setObject(3, new NumberBox<Double>(n_double)) | |
| 139 | - .setObject(4, new NumberBox<BigInteger>(n_varint)) | |
| 140 | - .setObject(5, new NumberBox<BigDecimal>(n_decimal)) | |
| 141 | - ); | |
| 142 | - PreparedStatement ps = session.prepare(selectQuery); | |
| 143 | - ResultSet rows = session.execute(ps.bind() | |
| 144 | - .setObject(0, new NumberBox<Integer>(n_int)) | |
| 145 | - .setObject(1, new NumberBox<Long>(n_bigint)) | |
| 146 | - ); | |
| 147 | - Row row = rows.one(); | |
| 148 | - assertRow(row); | |
| 149 | - } | |
| 150 | - | |
| 151 | - @Test(groups = "short") | |
| 152 | - public void should_use_custom_codecs_with_prepared_statements_3() { | |
| 153 | 133 | session.execute(session.prepare(insertQuery).bind() |
| 154 | 134 | .set(0, new NumberBox<Integer>(n_int), new TypeToken<NumberBox<Integer>>() {}) |
| 155 | 135 | .set(1, new NumberBox<Long>(n_bigint), new TypeToken<NumberBox<Long>>() {}) |
| @@ -184,25 +164,6 @@ | ||
| 184 | 164 | |
| 185 | 165 | @Test(groups = "short") |
| 186 | 166 | public void should_use_custom_codecs_with_built_statements_2() { |
| 187 | - session.execute(session.prepare(insertStmt).bind() | |
| 188 | - .setObject(0, new NumberBox<Integer>(n_int)) | |
| 189 | - .setObject(1, new NumberBox<Long>(n_bigint)) | |
| 190 | - .setObject(2, new NumberBox<Float>(n_float)) | |
| 191 | - .setObject(3, new NumberBox<Double>(n_double)) | |
| 192 | - .setObject(4, new NumberBox<BigInteger>(n_varint)) | |
| 193 | - .setObject(5, new NumberBox<BigDecimal>(n_decimal)) | |
| 194 | - ); | |
| 195 | - PreparedStatement ps = session.prepare(selectStmt); | |
| 196 | - ResultSet rows = session.execute(ps.bind() | |
| 197 | - .setObject(0, new NumberBox<Integer>(n_int)) | |
| 198 | - .setObject(1, new NumberBox<Long>(n_bigint)) | |
| 199 | - ); | |
| 200 | - Row row = rows.one(); | |
| 201 | - assertRow(row); | |
| 202 | - } | |
| 203 | - | |
| 204 | - @Test(groups = "short") | |
| 205 | - public void should_use_custom_codecs_with_built_statements_3() { | |
| 206 | 167 | session.execute(session.prepare(insertStmt).bind() |
| 207 | 168 | .set(0, new NumberBox<Integer>(n_int), new TypeToken<NumberBox<Integer>>() {}) |
| 208 | 169 | .set(1, new NumberBox<Long>(n_bigint), new TypeToken<NumberBox<Long>>() {}) |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecEnumIntegrationTest.java +3 −4
| @@ -29,7 +29,6 @@ | ||
| 29 | 29 | import static com.google.common.collect.Sets.newHashSet; |
| 30 | 30 | import static org.assertj.core.api.Assertions.assertThat; |
| 31 | 31 | |
| 32 | -import com.datastax.driver.core.TypeCodec.EnumIntCodec; | |
| 33 | 32 | import com.datastax.driver.core.utils.CassandraVersion; |
| 34 | 33 | |
| 35 | 34 | import static com.datastax.driver.core.DataType.cint; |
| @@ -79,10 +78,10 @@ | ||
| 79 | 78 | |
| 80 | 79 | @Override |
| 81 | 80 | protected Cluster.Builder configure(Cluster.Builder builder) { |
| 82 | - // explicitly register an EnumIntCodec for Foo enums | |
| 83 | - // Bar enums will use the default EnumStringCodec so it doesn't need to be explicitly registered. | |
| 84 | 81 | return builder.withCodecRegistry( |
| 85 | - new CodecRegistry().register(new EnumIntCodec<Foo>(Foo.class)) | |
| 82 | + new CodecRegistry() | |
| 83 | + .register(new EnumIntCodec<Foo>(Foo.class)) | |
| 84 | + .register(new EnumStringCodec<Bar>(Bar.class)) | |
| 86 | 85 | ); |
| 87 | 86 | } |
| 88 | 87 |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecJodaIntegrationTest.java +1 −1
| @@ -151,7 +151,7 @@ | ||
| 151 | 151 | public void should_map_tuple_to_datetime() { |
| 152 | 152 | // Register codec that maps DateTime <-> tuple<timestamp,varchar> |
| 153 | 153 | TupleType dateWithTimeZoneType = cluster.getMetadata().newTupleType(timestamp(), varchar()); |
| 154 | - JodaCodecs.TimeZonePreservingDateTimeCodec dateTimeCodec = new JodaCodecs.TimeZonePreservingDateTimeCodec(new TypeCodec.TupleCodec(dateWithTimeZoneType)); | |
| 154 | + JodaCodecs.TimeZonePreservingDateTimeCodec dateTimeCodec = new JodaCodecs.TimeZonePreservingDateTimeCodec(TypeCodec.tuple(dateWithTimeZoneType)); | |
| 155 | 155 | cluster.getConfiguration().getCodecRegistry().register(dateTimeCodec); |
| 156 | 156 | |
| 157 | 157 | DateTime dateTime = DateTime.parse("2010-06-30T01:20+05:30"); |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecJsonIntegrationTest.java +1 −10
| @@ -123,7 +123,7 @@ | ||
| 123 | 123 | session.execute(session.prepare(insertQuery).bind() |
| 124 | 124 | .setString(0, notAJsonString) |
| 125 | 125 | .set(1, alice, User.class) |
| 126 | - .setList(2, bobAndCharlie) | |
| 126 | + .setList(2, bobAndCharlie, User.class) | |
| 127 | 127 | ); |
| 128 | 128 | PreparedStatement ps = session.prepare(selectQuery); |
| 129 | 129 | ResultSet rows = session.execute(ps.bind() |
| @@ -135,15 +135,6 @@ | ||
| 135 | 135 | ); |
| 136 | 136 | Row row = rows.one(); |
| 137 | 137 | assertRow(row); |
| 138 | - rows = session.execute(ps.bind() | |
| 139 | - .setString(0, notAJsonString) | |
| 140 | - // here we lost information about the java type of alice | |
| 141 | - // so the registry will look for a codec accepting varchar <-> ANY | |
| 142 | - // and will find jsonCodec because it is the only matching one | |
| 143 | - .setObject(1, alice) | |
| 144 | - ); | |
| 145 | - row = rows.one(); | |
| 146 | - assertRow(row); | |
| 147 | 138 | } |
| 148 | 139 | |
| 149 | 140 | private void assertRow(Row row) { |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecNestedCollectionsIntegrationTest.java +2 −16
| @@ -125,20 +125,6 @@ | ||
| 125 | 125 | |
| 126 | 126 | @Test(groups = "short") |
| 127 | 127 | public void should_work_with_prepared_statements_3() { |
| 128 | - session.execute(session.prepare(insertQuery).bind() | |
| 129 | - .setInt(0, pk) | |
| 130 | - .setObject(1, v) | |
| 131 | - ); | |
| 132 | - PreparedStatement ps = session.prepare(selectQuery); | |
| 133 | - ResultSet rows = session.execute(ps.bind() | |
| 134 | - .setInt(0, pk) | |
| 135 | - ); | |
| 136 | - Row row = rows.one(); | |
| 137 | - assertRow(row); | |
| 138 | - } | |
| 139 | - | |
| 140 | - @Test(groups = "short") | |
| 141 | - public void should_work_with_prepared_statements_4() { | |
| 142 | 128 | session.execute(session.prepare(insertQuery).bind() |
| 143 | 129 | .setInt(0, pk) |
| 144 | 130 | .set(1, v, listType) |
| @@ -202,12 +188,12 @@ | ||
| 202 | 188 | |
| 203 | 189 | @Override |
| 204 | 190 | public ByteBuffer serialize(MyInt value, ProtocolVersion protocolVersion) throws InvalidTypeException { |
| 205 | - return IntCodec.instance.serialize(value.i, protocolVersion); | |
| 191 | + return TypeCodec.cint().serialize(value.i, protocolVersion); | |
| 206 | 192 | } |
| 207 | 193 | |
| 208 | 194 | @Override |
| 209 | 195 | public MyInt deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { |
| 210 | - return new MyInt(IntCodec.instance.deserialize(bytes, protocolVersion)); | |
| 196 | + return new MyInt(TypeCodec.cint().deserialize(bytes, protocolVersion)); | |
| 211 | 197 | } |
| 212 | 198 | |
| 213 | 199 | @Override |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecNumbersIntegrationTest.java +0 −19
| @@ -94,25 +94,6 @@ | ||
| 94 | 94 | |
| 95 | 95 | @Test(groups = "short") |
| 96 | 96 | public void should_use_default_codecs_with_prepared_statements_3() { |
| 97 | - session.execute(session.prepare(insertQuery).bind() | |
| 98 | - .setObject(0, n_int) | |
| 99 | - .setObject(1, n_bigint) | |
| 100 | - .setObject(2, n_float) | |
| 101 | - .setObject(3, n_double) | |
| 102 | - .setObject(4, n_varint) | |
| 103 | - .setObject(5, n_decimal) | |
| 104 | - ); | |
| 105 | - PreparedStatement ps = session.prepare(selectQuery); | |
| 106 | - ResultSet rows = session.execute(ps.bind() | |
| 107 | - .setInt(0, n_int) | |
| 108 | - .setLong(1, n_bigint) | |
| 109 | - ); | |
| 110 | - Row row = rows.one(); | |
| 111 | - assertRow(row); | |
| 112 | - } | |
| 113 | - | |
| 114 | - @Test(groups = "short") | |
| 115 | - public void should_use_default_codecs_with_prepared_statements_4() { | |
| 116 | 97 | session.execute(session.prepare(insertQuery).bind() |
| 117 | 98 | .set(0, n_int, Integer.class) |
| 118 | 99 | .set(1, n_bigint, Long.class) |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecOverlappingJavaTypeIntegrationTest.java +6 −13
| @@ -67,20 +67,13 @@ | ||
| 67 | 67 | ); |
| 68 | 68 | session.execute( |
| 69 | 69 | ps.bind() |
| 70 | - .setObject(0, 42) | |
| 71 | - .setObject(1, newArrayList(42)) | |
| 72 | - .setObject(2, "42") // here we have the CQL type so VarcharCodec will be used even if IntToStringCodec accepts it | |
| 73 | - ); | |
| 74 | - session.execute( | |
| 75 | - ps.bind() | |
| 76 | - .setObject(0, "42") | |
| 77 | - .setObject(1, newArrayList("42")) | |
| 78 | - .setObject(2, "42") // here we have the CQL type so VarcharCodec will be used even if IntToStringCodec accepts it | |
| 70 | + .setString(0, "42") | |
| 71 | + .setList(1, newArrayList("42"), String.class) | |
| 72 | + .setString(2, "42") // here we have the CQL type so VarcharCodec will be used even if IntToStringCodec accepts it | |
| 79 | 73 | ); |
| 80 | 74 | ps = session.prepare(selectQuery); |
| 81 | 75 | assertRow(session.execute(ps.bind().setInt(0, 42)).one()); |
| 82 | - assertRow(session.execute(ps.bind().setObject(0, 42)).one()); | |
| 83 | - assertRow(session.execute(ps.bind().setObject(0, "42")).one()); | |
| 76 | + assertRow(session.execute(ps.bind().setString(0, "42")).one()); | |
| 84 | 77 | } |
| 85 | 78 | |
| 86 | 79 | private void assertRow(Row row) { |
| @@ -104,12 +97,12 @@ | ||
| 104 | 97 | |
| 105 | 98 | @Override |
| 106 | 99 | public ByteBuffer serialize(String value, ProtocolVersion protocolVersion) throws InvalidTypeException { |
| 107 | - return IntCodec.instance.serialize(value == null ? null : Integer.parseInt(value), protocolVersion); | |
| 100 | + return TypeCodec.cint().serialize(value == null ? null : Integer.parseInt(value), protocolVersion); | |
| 108 | 101 | } |
| 109 | 102 | |
| 110 | 103 | @Override |
| 111 | 104 | public String deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { |
| 112 | - Integer i = IntCodec.instance.deserialize(bytes, protocolVersion); | |
| 105 | + Integer i = TypeCodec.cint().deserialize(bytes, protocolVersion); | |
| 113 | 106 | return i == null ? null : Integer.toString(i); |
| 114 | 107 | } |
| 115 | 108 |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecTest.java +24 −21
| @@ -31,7 +31,6 @@ | ||
| 31 | 31 | import static com.google.common.collect.Lists.newArrayList; |
| 32 | 32 | import static org.testng.Assert.fail; |
| 33 | 33 | |
| 34 | -import com.datastax.driver.core.TypeCodec.UDTCodec; | |
| 35 | 34 | import com.datastax.driver.core.UserType.Field; |
| 36 | 35 | import com.datastax.driver.core.exceptions.CodecNotFoundException; |
| 37 | 36 | import com.datastax.driver.core.exceptions.InvalidTypeException; |
| @@ -45,10 +44,7 @@ | ||
| 45 | 44 | |
| 46 | 45 | public static final DataType CUSTOM_FOO = DataType.custom("com.example.FooBar"); |
| 47 | 46 | |
| 48 | - public static final TypeCodec.CustomCodec CUSTOM_FOO_CODEC = new TypeCodec.CustomCodec(CUSTOM_FOO); | |
| 49 | - | |
| 50 | - private CodecRegistry codecRegistry = new CodecRegistry() | |
| 51 | - .register(CUSTOM_FOO_CODEC); | |
| 47 | + private CodecRegistry codecRegistry = new CodecRegistry(); | |
| 52 | 48 | |
| 53 | 49 | @Test(groups = "unit") |
| 54 | 50 | public void testCustomList() throws Exception { |
| @@ -117,8 +113,8 @@ | ||
| 117 | 113 | |
| 118 | 114 | @Test(groups = "unit") |
| 119 | 115 | public void test_ascii_vs_utf8() { |
| 120 | - TypeCodec.AsciiCodec asciiCodec = TypeCodec.AsciiCodec.instance; | |
| 121 | - TypeCodec.VarcharCodec utf8Codec = TypeCodec.VarcharCodec.instance; | |
| 116 | + TypeCodec<String> asciiCodec = TypeCodec.ascii(); | |
| 117 | + TypeCodec<String> utf8Codec = TypeCodec.varchar(); | |
| 122 | 118 | String ascii = "The quick brown fox jumps over the lazy dog!"; |
| 123 | 119 | String utf8 = "Dès Noël, où un zéphyr haï me vêt de glaçons würmiens, je dîne d’exquis rôtis de bœuf au kir à l’aÿ d’âge mûr & cætera!"; |
| 124 | 120 | assertThat(asciiCodec) |
| @@ -143,17 +139,17 @@ | ||
| 143 | 139 | |
| 144 | 140 | @Test(groups = "unit") |
| 145 | 141 | public void test_varchar_vs_text() { |
| 146 | - assertThat(TypeCodec.VarcharCodec.instance) | |
| 142 | + assertThat(TypeCodec.varchar()) | |
| 147 | 143 | .accepts(String.class) |
| 148 | 144 | .accepts(varchar()) |
| 149 | 145 | .accepts(text()); |
| 150 | - assertThat(new TypeCodec.ListCodec<String>(TypeCodec.VarcharCodec.instance)) | |
| 146 | + assertThat(TypeCodec.list(TypeCodec.varchar())) | |
| 151 | 147 | .accepts(list(varchar())) |
| 152 | 148 | .accepts(list(text())); |
| 153 | - assertThat(new TypeCodec.SetCodec<String>(TypeCodec.VarcharCodec.instance)) | |
| 149 | + assertThat(TypeCodec.set(TypeCodec.varchar())) | |
| 154 | 150 | .accepts(set(varchar())) |
| 155 | 151 | .accepts(set(text())); |
| 156 | - assertThat(new TypeCodec.MapCodec<String, String>(TypeCodec.VarcharCodec.instance, TypeCodec.VarcharCodec.instance)) | |
| 152 | + assertThat(TypeCodec.map(TypeCodec.varchar(), TypeCodec.varchar())) | |
| 157 | 153 | .accepts(map(varchar(), varchar())) |
| 158 | 154 | .accepts(map(varchar(), text())) |
| 159 | 155 | .accepts(map(text(), varchar())) |
| @@ -162,7 +158,7 @@ | ||
| 162 | 158 | TupleType t2 = new TupleType(newArrayList(text(), varchar()), V3, new CodecRegistry()); |
| 163 | 159 | TupleType t3 = new TupleType(newArrayList(varchar(), text()), V3, new CodecRegistry()); |
| 164 | 160 | TupleType t4 = new TupleType(newArrayList(text(), text()), V3, new CodecRegistry()); |
| 165 | - assertThat(new TypeCodec.TupleCodec(t1)) | |
| 161 | + assertThat(TypeCodec.tuple(t1)) | |
| 166 | 162 | .accepts(t2) |
| 167 | 163 | .accepts(t3) |
| 168 | 164 | .accepts(t4); |
| @@ -170,7 +166,7 @@ | ||
| 170 | 166 | UserType u2 = new UserType("ks", "table", newArrayList(new Field("f1", text()), new Field("f2", varchar())), V3, new CodecRegistry()); |
| 171 | 167 | UserType u3 = new UserType("ks", "table", newArrayList(new Field("f1", varchar()), new Field("f2", text())), V3, new CodecRegistry()); |
| 172 | 168 | UserType u4 = new UserType("ks", "table", newArrayList(new Field("f1", text()), new Field("f2", text())), V3, new CodecRegistry()); |
| 173 | - assertThat(new UDTCodec(u1)) | |
| 169 | + assertThat(TypeCodec.userType(u1)) | |
| 174 | 170 | .accepts(u2) |
| 175 | 171 | .accepts(u3) |
| 176 | 172 | .accepts(u4); |
| @@ -178,7 +174,7 @@ | ||
| 178 | 174 | |
| 179 | 175 | @Test(groups = "unit") |
| 180 | 176 | public void test_enum() { |
| 181 | - TypeCodec.EnumStringCodec<FooBarQix> codec = new TypeCodec.EnumStringCodec<FooBarQix>(FooBarQix.class); | |
| 177 | + EnumStringCodec<FooBarQix> codec = new EnumStringCodec<FooBarQix>(FooBarQix.class); | |
| 182 | 178 | assertThat(codec) |
| 183 | 179 | .canSerialize(FooBarQix.FOO); |
| 184 | 180 | } |
| @@ -191,7 +187,10 @@ | ||
| 191 | 187 | assertThat(codecRegistry.codecFor(cint(), A.class)).isNotNull().isSameAs(aCodec); |
| 192 | 188 | // inheritance works: B is assignable to A |
| 193 | 189 | assertThat(codecRegistry.codecFor(cint(), B.class)).isNotNull().isSameAs(aCodec); |
| 194 | - assertThat(codecRegistry.codecFor(list(cint()), new TypeToken<List<A>>(){})).isNotNull().isEqualTo(new TypeCodec.ListCodec<A>(aCodec)); | |
| 190 | + TypeCodec<List<A>> expected = TypeCodec.list(aCodec); | |
| 191 | + TypeCodec<List<A>> actual = codecRegistry.codecFor(list(cint()), new TypeToken<List<A>>(){}); | |
| 192 | + assertThat(actual.getCqlType()).isEqualTo(expected.getCqlType()); | |
| 193 | + assertThat(actual.getJavaType()).isEqualTo(expected.getJavaType()); | |
| 195 | 194 | // cannot work: List<B> is not assignable to List<A> |
| 196 | 195 | try { |
| 197 | 196 | codecRegistry.codecFor(list(cint()), new TypeToken<List<B>>(){}); |
| @@ -215,7 +214,11 @@ | ||
| 215 | 214 | } catch (CodecNotFoundException e) { |
| 216 | 215 | // ok |
| 217 | 216 | } |
| 218 | - assertThat(codecRegistry.codecFor(list(cint()), new TypeToken<List<B>>(){})).isNotNull().isEqualTo(new TypeCodec.ListCodec<B>(bCodec)); | |
| 217 | + TypeCodec<List<B>> expectedB = TypeCodec.list(bCodec); | |
| 218 | + TypeCodec<List<B>> actualB = codecRegistry.codecFor(list(cint()), new TypeToken<List<B>>() { | |
| 219 | + }); | |
| 220 | + assertThat(actualB.getCqlType()).isEqualTo(expectedB.getCqlType()); | |
| 221 | + assertThat(actualB.getJavaType()).isEqualTo(expectedB.getJavaType()); | |
| 219 | 222 | } |
| 220 | 223 | |
| 221 | 224 | |
| @@ -240,7 +243,7 @@ | ||
| 240 | 243 | ), ProtocolVersion.NEWEST_SUPPORTED, codecRegistry); |
| 241 | 244 | UDTValue expected = udt.newValue(); |
| 242 | 245 | expected.setString("t", null); |
| 243 | - expected.setObject("i", null); | |
| 246 | + expected.setToNull("i"); | |
| 244 | 247 | expected.setList("l", null); |
| 245 | 248 | |
| 246 | 249 | UDTValue actual = codecRegistry.codecFor(udt, UDTValue.class).deserialize(ByteBuffer.allocate(0), ProtocolVersion.NEWEST_SUPPORTED); |
| @@ -251,10 +254,10 @@ | ||
| 251 | 254 | |
| 252 | 255 | private class ListVarcharToListListInteger extends TypeCodec<List<List<Integer>>> { |
| 253 | 256 | |
| 254 | - private final ListCodec<String> codec = new ListCodec<String>(VarcharCodec.instance); | |
| 257 | + private final TypeCodec<List<String>> codec = TypeCodec.list(TypeCodec.varchar()); | |
| 255 | 258 | |
| 256 | 259 | protected ListVarcharToListListInteger() { |
| 257 | - super(list(varchar()), listOf(listOf(Integer.class))); | |
| 260 | + super(DataType.list(DataType.varchar()), listOf(listOf(Integer.class))); | |
| 258 | 261 | } |
| 259 | 262 | |
| 260 | 263 | @Override |
| @@ -357,7 +360,7 @@ | ||
| 357 | 360 | class ACodec extends TypeCodec<A> { |
| 358 | 361 | |
| 359 | 362 | protected ACodec() { |
| 360 | - super(cint(), A.class); | |
| 363 | + super(DataType.cint(), A.class); | |
| 361 | 364 | } |
| 362 | 365 | |
| 363 | 366 | @Override |
| @@ -385,7 +388,7 @@ | ||
| 385 | 388 | class BCodec extends TypeCodec<B> { |
| 386 | 389 | |
| 387 | 390 | protected BCodec() { |
| 388 | - super(cint(), B.class); | |
| 391 | + super(DataType.cint(), B.class); | |
| 389 | 392 | } |
| 390 | 393 | |
| 391 | 394 | @Override |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecTupleIntegrationTest.java +4 −16
| @@ -69,11 +69,6 @@ | ||
| 69 | 69 | rows = session.execute(selectQuery, uuid); |
| 70 | 70 | row = rows.one(); |
| 71 | 71 | assertRow(row); |
| 72 | - // bound with setObject | |
| 73 | - session.execute(ps.bind().setUUID(0, uuid).setString(1, "John Doe").setObject(2, locationValue)); | |
| 74 | - rows = session.execute(selectQuery, uuid); | |
| 75 | - row = rows.one(); | |
| 76 | - assertRow(row); | |
| 77 | 72 | } |
| 78 | 73 | |
| 79 | 74 | @Test(groups = "short") |
| @@ -95,11 +90,6 @@ | ||
| 95 | 90 | rows = session.execute(selectQuery, uuid); |
| 96 | 91 | row = rows.one(); |
| 97 | 92 | assertPartialRow(row); |
| 98 | - // bound with setObject | |
| 99 | - session.execute(ps.bind().setUUID(0, uuid).setString(1, "John Doe").setObject(2, partialLocationValueInserted)); | |
| 100 | - rows = session.execute(selectQuery, uuid); | |
| 101 | - row = rows.one(); | |
| 102 | - assertPartialRow(row); | |
| 103 | 93 | } |
| 104 | 94 | |
| 105 | 95 | @Test(groups = "short") |
| @@ -114,7 +104,7 @@ | ||
| 114 | 104 | Session session = cluster.connect(keyspace); |
| 115 | 105 | setUpTupleTypes(cluster); |
| 116 | 106 | codecRegistry |
| 117 | - .register(new LocationCodec(new TypeCodec.TupleCodec(locationType))) | |
| 107 | + .register(new LocationCodec(TypeCodec.tuple(locationType))) | |
| 118 | 108 | ; |
| 119 | 109 | session.execute(insertQuery, uuid, "John Doe", locationValue); |
| 120 | 110 | ResultSet rows = session.execute(selectQuery, uuid); |
| @@ -150,7 +140,7 @@ | ||
| 150 | 140 | Session session = cluster.connect(keyspace); |
| 151 | 141 | setUpTupleTypes(cluster); |
| 152 | 142 | codecRegistry |
| 153 | - .register(new LocationCodec(new TypeCodec.TupleCodec(locationType))) | |
| 143 | + .register(new LocationCodec(TypeCodec.tuple(locationType))) | |
| 154 | 144 | ; |
| 155 | 145 | session.execute(insertQuery, uuid, "John Doe", partialLocationValueInserted); |
| 156 | 146 | ResultSet rows = session.execute(selectQuery, uuid); |
| @@ -211,7 +201,7 @@ | ||
| 211 | 201 | partialLocation = new Location(37.387224f, 0.0f); |
| 212 | 202 | } |
| 213 | 203 | |
| 214 | - static class LocationCodec extends TypeCodec.MappingCodec<Location, TupleValue> { | |
| 204 | + static class LocationCodec extends MappingCodec<Location, TupleValue> { | |
| 215 | 205 | |
| 216 | 206 | private final TupleType tupleType; |
| 217 | 207 | |
| @@ -251,9 +241,7 @@ | ||
| 251 | 241 | |
| 252 | 242 | Location location = (Location)o; |
| 253 | 243 | |
| 254 | - if (Float.compare(location.latitude, latitude) != 0) | |
| 255 | - return false; | |
| 256 | - return Float.compare(location.longitude, longitude) == 0; | |
| 244 | + return Float.compare(location.latitude, latitude) == 0 && Float.compare(location.longitude, longitude) == 0; | |
| 257 | 245 | |
| 258 | 246 | } |
| 259 | 247 |
modified driver-core/src/test/java/com/datastax/driver/core/TypeCodecUDTIntegrationTest.java +6 −11
| @@ -70,11 +70,6 @@ | ||
| 70 | 70 | rows = session.execute(selectQuery, uuid); |
| 71 | 71 | row = rows.one(); |
| 72 | 72 | assertRow(row); |
| 73 | - // bound with setObject | |
| 74 | - session.execute(ps.bind().setUUID(0, uuid).setString(1, "John Doe").setObject(2, addressValue)); | |
| 75 | - rows = session.execute(selectQuery, uuid); | |
| 76 | - row = rows.one(); | |
| 77 | - assertRow(row); | |
| 78 | 73 | } |
| 79 | 74 | |
| 80 | 75 | @Test(groups = "short") |
| @@ -88,8 +83,8 @@ | ||
| 88 | 83 | try { |
| 89 | 84 | Session session = cluster.connect(keyspace); |
| 90 | 85 | setUpUserTypes(cluster); |
| 91 | - TypeCodec<UDTValue> addressTypeCodec = new TypeCodec.UDTCodec(addressType); | |
| 92 | - TypeCodec<UDTValue> phoneTypeCodec = new TypeCodec.UDTCodec(phoneType); | |
| 86 | + TypeCodec<UDTValue> addressTypeCodec = TypeCodec.userType(addressType); | |
| 87 | + TypeCodec<UDTValue> phoneTypeCodec = TypeCodec.userType(phoneType); | |
| 93 | 88 | codecRegistry |
| 94 | 89 | .register(new AddressCodec(addressTypeCodec, Address.class)) |
| 95 | 90 | .register(new PhoneCodec(phoneTypeCodec, Phone.class)) |
| @@ -136,14 +131,14 @@ | ||
| 136 | 131 | .setSet("tags", phone1.tags); |
| 137 | 132 | UDTValue phone2Value = phoneType.newValue() |
| 138 | 133 | .setString("number", phone2.number) |
| 139 | - .setObject("tags", phone2.tags); | |
| 134 | + .setSet("tags", phone2.tags); | |
| 140 | 135 | addressValue = addressType.newValue() |
| 141 | 136 | .setString("street", address.street) |
| 142 | 137 | .setInt(1, address.zipcode) |
| 143 | 138 | .setList("phones", Lists.newArrayList(phone1Value, phone2Value)); |
| 144 | 139 | } |
| 145 | 140 | |
| 146 | - static class AddressCodec extends TypeCodec.MappingCodec<Address, UDTValue> { | |
| 141 | + static class AddressCodec extends MappingCodec<Address, UDTValue> { | |
| 147 | 142 | |
| 148 | 143 | private final UserType userType; |
| 149 | 144 | |
| @@ -159,11 +154,11 @@ | ||
| 159 | 154 | |
| 160 | 155 | @Override |
| 161 | 156 | protected UDTValue serialize(Address value) { |
| 162 | - return value == null ? null : userType.newValue().setString("street", value.street).setInt("zipcode", value.zipcode).setList("phones", value.phones); | |
| 157 | + return value == null ? null : userType.newValue().setString("street", value.street).setInt("zipcode", value.zipcode).setList("phones", value.phones, Phone.class); | |
| 163 | 158 | } |
| 164 | 159 | } |
| 165 | 160 | |
| 166 | - static class PhoneCodec extends TypeCodec.MappingCodec<Phone, UDTValue> { | |
| 161 | + static class PhoneCodec extends MappingCodec<Phone, UDTValue> { | |
| 167 | 162 | |
| 168 | 163 | private final UserType userType; |
| 169 | 164 |
modified driver-core/src/test/java/com/datastax/driver/core/utils/UUIDsTest.java +3 −4
| @@ -24,7 +24,6 @@ | ||
| 24 | 24 | import static org.testng.Assert.assertEquals; |
| 25 | 25 | import static org.testng.Assert.assertTrue; |
| 26 | 26 | |
| 27 | -import com.datastax.driver.core.DataType; | |
| 28 | 27 | import com.datastax.driver.core.ProtocolVersion; |
| 29 | 28 | import com.datastax.driver.core.TypeCodec; |
| 30 | 29 | |
| @@ -111,9 +110,9 @@ | ||
| 111 | 110 | } |
| 112 | 111 | |
| 113 | 112 | private static void assertWithin(UUID uuid, UUID lowerBound, UUID upperBound) { |
| 114 | - ByteBuffer uuidBytes = TypeCodec.UUIDCodec.instance.serialize(uuid, ProtocolVersion.V1); | |
| 115 | - ByteBuffer lb = TypeCodec.UUIDCodec.instance.serialize(lowerBound, ProtocolVersion.V1); | |
| 116 | - ByteBuffer ub = TypeCodec.UUIDCodec.instance.serialize(upperBound, ProtocolVersion.V1); | |
| 113 | + ByteBuffer uuidBytes = TypeCodec.uuid().serialize(uuid, ProtocolVersion.V1); | |
| 114 | + ByteBuffer lb = TypeCodec.uuid().serialize(lowerBound, ProtocolVersion.V1); | |
| 115 | + ByteBuffer ub = TypeCodec.uuid().serialize(upperBound, ProtocolVersion.V1); | |
| 117 | 116 | assertTrue(compareTimestampBytes(lb, uuidBytes) <= 0); |
| 118 | 117 | assertTrue(compareTimestampBytes(ub, uuidBytes) >= 0); |
| 119 | 118 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperCustomCodecTest.java +35 −6
| @@ -258,7 +258,7 @@ | ||
| 258 | 258 | @PartitionKey |
| 259 | 259 | private int i; |
| 260 | 260 | |
| 261 | - @Column(codec = TypeCodec.StringCodec.class) | |
| 261 | + @Column(codec = NoDefaultConstructorCodec.class) | |
| 262 | 262 | private long l; |
| 263 | 263 | |
| 264 | 264 | public int getI() { |
| @@ -345,7 +345,7 @@ | ||
| 345 | 345 | |
| 346 | 346 | @Query("update data1 set l = :l where i = :i") |
| 347 | 347 | void setL(@Param("i") int i, |
| 348 | - @Param(value = "l", codec = TypeCodec.StringCodec.class) long l); | |
| 348 | + @Param(value = "l", codec = NoDefaultConstructorCodec.class) long l); | |
| 349 | 349 | } |
| 350 | 350 | |
| 351 | 351 | @Accessor |
| @@ -469,12 +469,12 @@ | ||
| 469 | 469 | |
| 470 | 470 | @Override |
| 471 | 471 | public ByteBuffer serialize(CustomInt value, ProtocolVersion protocolVersion) throws InvalidTypeException { |
| 472 | - return TypeCodec.IntCodec.instance.serialize(value.value, protocolVersion); | |
| 472 | + return TypeCodec.cint().serialize(value.value, protocolVersion); | |
| 473 | 473 | } |
| 474 | 474 | |
| 475 | 475 | @Override |
| 476 | 476 | public CustomInt deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { |
| 477 | - Integer i = TypeCodec.IntCodec.instance.deserialize(bytes, protocolVersion); | |
| 477 | + Integer i = TypeCodec.cint().deserialize(bytes, protocolVersion); | |
| 478 | 478 | return new CustomInt(i); |
| 479 | 479 | } |
| 480 | 480 | |
| @@ -518,12 +518,12 @@ | ||
| 518 | 518 | |
| 519 | 519 | @Override |
| 520 | 520 | public ByteBuffer serialize(CustomLong value, ProtocolVersion protocolVersion) throws InvalidTypeException { |
| 521 | - return TypeCodec.BigintCodec.instance.serialize(value.value, protocolVersion); | |
| 521 | + return TypeCodec.bigint().serialize(value.value, protocolVersion); | |
| 522 | 522 | } |
| 523 | 523 | |
| 524 | 524 | @Override |
| 525 | 525 | public CustomLong deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { |
| 526 | - Long l = TypeCodec.BigintCodec.instance.deserialize(bytes, protocolVersion); | |
| 526 | + Long l = TypeCodec.bigint().deserialize(bytes, protocolVersion); | |
| 527 | 527 | return new CustomLong(l); |
| 528 | 528 | } |
| 529 | 529 | |
| @@ -555,4 +555,33 @@ | ||
| 555 | 555 | super(registry.codecFor(DataType.text(), String.class)); |
| 556 | 556 | } |
| 557 | 557 | } |
| 558 | + | |
| 559 | + private static class NoDefaultConstructorCodec extends TypeCodec<String> { | |
| 560 | + | |
| 561 | + public NoDefaultConstructorCodec(DataType cqlType, Class<String> javaClass) { | |
| 562 | + super(cqlType, javaClass); | |
| 563 | + } | |
| 564 | + | |
| 565 | + @Override | |
| 566 | + public ByteBuffer serialize(String value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 567 | + return null; | |
| 568 | + } | |
| 569 | + | |
| 570 | + @Override | |
| 571 | + public String deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 572 | + return null; | |
| 573 | + } | |
| 574 | + | |
| 575 | + @Override | |
| 576 | + public String parse(String value) throws InvalidTypeException { | |
| 577 | + return null; | |
| 578 | + } | |
| 579 | + | |
| 580 | + @Override | |
| 581 | + public String format(String value) throws InvalidTypeException { | |
| 582 | + return null; | |
| 583 | + } | |
| 584 | + | |
| 585 | + } | |
| 586 | + | |
| 558 | 587 | } |
modified driver-mapping/src/test/java/com/datastax/driver/mapping/MapperUDTTest.java +2 −2
| @@ -287,9 +287,9 @@ | ||
| 287 | 287 | |
| 288 | 288 | // Create a user. |
| 289 | 289 | session.execute("update users SET other_addresses['condo']={street: '101 Ocean Ln', city: 'Jacksonville, FL', zip_code: 89898, phones: {'8675309'}} " + |
| 290 | - " WHERE user_id=" + TypeCodec.UUIDCodec.instance.format(userId)); | |
| 290 | + " WHERE user_id=" + TypeCodec.uuid().format(userId)); | |
| 291 | 291 | session.execute("update users SET mainaddress={street: '42 Middle of Nowhere', city: 'Lake of the Woods', zip_code: 49553, phones: {'8675039'}} " + |
| 292 | - " WHERE user_Id=" + TypeCodec.UUIDCodec.instance.format(userId)); | |
| 292 | + " WHERE user_Id=" + TypeCodec.uuid().format(userId)); | |
| 293 | 293 | |
| 294 | 294 | // Get the user. |
| 295 | 295 | Row row = session.execute("select * from users where user_id=?", userId).one(); |
modified features/custom_codecs/README.md +91 −37
| @@ -61,53 +61,80 @@ | ||
| 61 | 61 | /** |
| 62 | 62 | * A simple Json codec. |
| 63 | 63 | */ |
| 64 | -public class JsonCodec<T> extends TypeCodec.StringParsingCodec<T> { | |
| 64 | +public class JsonCodec<T> extends TypeCodec<T> { | |
| 65 | 65 | |
| 66 | - private final ObjectMapper objectMapper; | |
| 66 | + private final ObjectMapper objectMapper = new ObjectMapper(); | |
| 67 | 67 | |
| 68 | - public JsonCodec(Class<T> javaType, ObjectMapper objectMapper) { | |
| 69 | - super(javaType); | |
| 70 | - this.objectMapper = objectMapper; | |
| 68 | + public JsonCodec(Class<T> javaType) { | |
| 69 | + super(DataType.varchar(), javaType); | |
| 71 | 70 | } |
| 72 | 71 | |
| 73 | 72 | @Override |
| 74 | - @SuppressWarnings("unchecked") | |
| 75 | - public T fromString(String value) { | |
| 73 | + public ByteBuffer serialize(T value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 76 | 74 | if (value == null) |
| 77 | 75 | return null; |
| 78 | 76 | try { |
| 79 | - return (T) objectMapper.readValue(value, getJavaType().getRawType()); | |
| 77 | + return ByteBuffer.wrap(objectMapper.writeValueAsBytes(value)); | |
| 78 | + } catch (JsonProcessingException e) { | |
| 79 | + throw new InvalidTypeException(e.getMessage(), e); | |
| 80 | + } | |
| 81 | + } | |
| 82 | + | |
| 83 | + @Override | |
| 84 | + @SuppressWarnings("unchecked") | |
| 85 | + public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 86 | + if (bytes == null) | |
| 87 | + return null; | |
| 88 | + try { | |
| 89 | + byte[] b = new byte[bytes.remaining()]; | |
| 90 | + bytes.duplicate().get(b); | |
| 91 | + return (T)objectMapper.readValue(b, toJacksonJavaType()); | |
| 80 | 92 | } catch (IOException e) { |
| 81 | 93 | throw new InvalidTypeException(e.getMessage(), e); |
| 82 | 94 | } |
| 83 | 95 | } |
| 84 | 96 | |
| 85 | 97 | @Override |
| 86 | - public String toString(T value) { | |
| 98 | + public String format(T value) throws InvalidTypeException { | |
| 87 | 99 | if (value == null) |
| 100 | + return NULL; | |
| 101 | + String json; | |
| 102 | + try { | |
| 103 | + json = objectMapper.writeValueAsString(value); | |
| 104 | + } catch (IOException e) { | |
| 105 | + throw new InvalidTypeException(e.getMessage(), e); | |
| 106 | + } | |
| 107 | + return '\'' + json.replace("\'", "''") + '\''; | |
| 108 | + } | |
| 109 | + | |
| 110 | + @Override | |
| 111 | + @SuppressWarnings("unchecked") | |
| 112 | + public T parse(String value) throws InvalidTypeException { | |
| 113 | + if (value == null || value.isEmpty() || value.equals(NULL)) | |
| 88 | 114 | return null; |
| 115 | + if (value.charAt(0) != '\'' || value.charAt(value.length() - 1) != '\'') | |
| 116 | + throw new InvalidTypeException("JSON strings must be enclosed by single quotes"); | |
| 117 | + String json = value.substring(1, value.length() - 1).replace("''", "'"); | |
| 89 | 118 | try { |
| 90 | - return objectMapper.writeValueAsString(value); | |
| 91 | - } catch (JsonProcessingException e) { | |
| 119 | + return (T)objectMapper.readValue(json, toJacksonJavaType()); | |
| 120 | + } catch (IOException e) { | |
| 92 | 121 | throw new InvalidTypeException(e.getMessage(), e); |
| 93 | 122 | } |
| 94 | 123 | } |
| 124 | + | |
| 125 | + protected JavaType toJacksonJavaType() { | |
| 126 | + return TypeFactory.defaultInstance().constructType(getJavaType().getType()); | |
| 127 | + } | |
| 128 | + | |
| 95 | 129 | } |
| 96 | 130 | ``` |
| 97 | 131 | |
| 98 | -As you can see, the driver already provides a convenient base class: [StringParsingCodec][StringParsingCodec]. | |
| 99 | - | |
| 100 | 132 | A few implementation guidelines: |
| 101 | 133 | |
| 102 | 134 | * Your codecs should be thread-safe, or better yet, immutable; |
| 103 | 135 | * Your codecs should be fast: do not forget that codecs are executed often and are usually very "hot" pieces of code; |
| 104 | 136 | * Your codecs should never block. |
| 105 | 137 | |
| 106 | -Note: the code above is an example and is not particularly efficient; | |
| 107 | -it suffers from the overhead of deserializing raw bytes into a String, | |
| 108 | -to only then parse the String into an object. | |
| 109 | -More advanced solutions that read the underlying byte stream directly are possible. | |
| 110 | - | |
| 111 | 138 | The second step is to register your codec with a `CodecRegistry` instance: |
| 112 | 139 | |
| 113 | 140 | ```java |
| @@ -242,32 +269,59 @@ | ||
| 242 | 269 | } |
| 243 | 270 | ``` |
| 244 | 271 | |
| 245 | -First of all, create a codec for this class: | |
| 272 | +First of all, create a codec that knows how to serialize this class; one possible solution | |
| 273 | +(but not necessarily the most efficient one) would be to convert `Address` instances into `UDTValue` | |
| 274 | +instances, then serialize them with another codec: | |
| 275 | + | |
| 246 | 276 | |
| 247 | 277 | ```java |
| 248 | -public class AddressCodec extends TypeCodec.MappingCodec<Address, UDTValue> { | |
| 278 | +public class AddressCodec extends TypeCodec<Address> { | |
| 249 | 279 | |
| 280 | + private final TypeCodec<UDTValue> innerCodec; | |
| 281 | + | |
| 250 | 282 | private final UserType userType; |
| 251 | 283 | |
| 252 | 284 | public AddressCodec(TypeCodec<UDTValue> innerCodec, Class<Address> javaType) { |
| 253 | - super(innerCodec, javaType); | |
| 254 | - userType = (UserType) innerCodec.getCqlType(); | |
| 285 | + super(innerCodec.getCqlType(), javaType); | |
| 286 | + this.innerCodec = innerCodec; | |
| 287 | + this.userType = (UserType)innerCodec.getCqlType(); | |
| 288 | + } | |
| 289 | + | |
| 290 | + @Override | |
| 291 | + public ByteBuffer serialize(Address value, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 292 | + return innerCodec.serialize(toUDTValue(value), protocolVersion); | |
| 255 | 293 | } |
| 256 | 294 | |
| 257 | 295 | @Override |
| 258 | - protected Address deserialize(UDTValue value) { | |
| 259 | - return value == null ? null : new Address(value.getString("street"), value.getInt("zipcode")); | |
| 296 | + public Address deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { | |
| 297 | + return toAddress(innerCodec.deserialize(bytes, protocolVersion)); | |
| 260 | 298 | } |
| 261 | 299 | |
| 262 | 300 | @Override |
| 263 | - protected UDTValue serialize(Address address) { | |
| 264 | - return address == null ? null : userType.newValue().setString("street", address.getStreet()).setInt("zipcode", address.getZipcode()); | |
| 301 | + public Address parse(String value) throws InvalidTypeException { | |
| 302 | + return value == null || value.isEmpty() || value.equals(NULL) ? null : toAddress(innerCodec.parse(value)); | |
| 303 | + } | |
| 304 | + | |
| 305 | + @Override | |
| 306 | + public String format(Address value) throws InvalidTypeException { | |
| 307 | + return value == null ? null : innerCodec.format(toUDTValue(value)); | |
| 308 | + } | |
| 309 | + | |
| 310 | + protected Address toAddress(UDTValue value) { | |
| 311 | + return value == null ? null : new Address( | |
| 312 | + value.getString("street"), | |
| 313 | + value.getInt("zipcode") | |
| 314 | + ); | |
| 315 | + } | |
| 316 | + | |
| 317 | + protected UDTValue toUDTValue(Address value) { | |
| 318 | + return value == null ? null : userType.newValue() | |
| 319 | + .setString("street", value.getStreet()) | |
| 320 | + .setInt("zipcode", value.getZipcode()); | |
| 265 | 321 | } |
| 266 | 322 | } |
| 267 | 323 | ``` |
| 268 | 324 | |
| 269 | -Again, the driver provides a convenient base class: [MappingCodec]. | |
| 270 | - | |
| 271 | 325 | Now (and only if you intend to use your own `CodecRegistry`), |
| 272 | 326 | create it and register it on your `Cluster` instance: |
| 273 | 327 | |
| @@ -284,6 +338,7 @@ | ||
| 284 | 338 | |
| 285 | 339 | ```java |
| 286 | 340 | UserType addressType = cluster.getMetadata().getKeyspace(...).getUserType("address"); |
| 341 | +TypeCodec<UDTValue> addressTypeCodec = codecRegistry.codecFor(addressType); | |
| 287 | 342 | ``` |
| 288 | 343 | |
| 289 | 344 | And finally, register your `AddressCodec` with the `CodecRegistry`. Note that |
| @@ -291,7 +346,7 @@ | ||
| 291 | 346 | user type values: |
| 292 | 347 | |
| 293 | 348 | ```java |
| 294 | -AddressCodec addressCodec = new AddressCodec(new TypeCodec.UDTCodec(addressType), Address.class); | |
| 349 | +AddressCodec addressCodec = new AddressCodec(addressTypeCodec, Address.class); | |
| 295 | 350 | codecRegistry.register(addressCodec); |
| 296 | 351 | ``` |
| 297 | 352 | |
| @@ -385,14 +440,13 @@ | ||
| 385 | 440 | |
| 386 | 441 | The following situations are exceptions where it is not possible to cache lookup results: |
| 387 | 442 | |
| 388 | -* With [SimpleStatement] instances and [BuiltStatement] instances (created via the Query Builder); in these places, the driver has | |
| 389 | - no way to determine the right CQL type to use, so it performs a best-effort heuristic to guess which codec to use. | |
| 390 | -* When using "ambiguous" methods of `BoundStatement` and `Row`, such as [setList()][setList], | |
| 391 | - [setSet()][setSet], [setMap()][setMap], [setObject()][setObject]; | |
| 392 | - these methods are "ambiguous" because they do not convey enough compile-time information about the object | |
| 393 | - to serialize, so the driver performs the same heuristic to guess which codec to use. | |
| 394 | - | |
| 395 | -Beware that in these cases the lookup performs in average 10x worse. If performance is a key factor for your application, | |
| 443 | +* With [SimpleStatement] instances; | |
| 444 | +* With [BuiltStatement] instances (created via the Query Builder). | |
| 445 | + | |
| 446 | +In these places, the driver has no way to determine the right CQL type to use, so it performs | |
| 447 | +a best-effort heuristic to guess which codec to use. | |
| 448 | + | |
| 449 | +Beware that in these cases, the lookup performs in average 10x worse. If performance is a key factor for your application, | |
| 396 | 450 | consider using prepared statements all the time, and avoid calling ambiguous methods (prefer `get()` and `set()`). |
| 397 | 451 | |
| 398 | 452 | [JAVA-721]: https://datastax-oss.atlassian.net/browse/JAVA-721 |
modified upgrade_guide/README.md +3 −0
| @@ -61,6 +61,9 @@ | ||
| 61 | 61 | * `TupleType of(DataType... types)`; users should now use `Metadata.newTupleType(DataType...)`. |
| 62 | 62 | |
| 63 | 63 | <p>The driver runtime behavior changes in the following situations:</p> |
| 64 | + * By default, the driver now returns _mutable_, _non thread-safe_ instances for CQL collection types; | |
| 65 | + This affects methods `getList`, `getSet`, `getMap`, `getObject` and `get` for all instances of | |
| 66 | + `GettableByIndexData` and `GettableByNameData` (`Row`, `BoundStatement`, `TupleValue` and `UDTValue`) | |
| 64 | 67 | * `RuntimeException`s thrown during serialization or deserialization might not be |
| 65 | 68 | the same ones as before, due to the newly-introduced `CodecNotFoundException` |
| 66 | 69 | and to the dynamic nature of codec search introduced by JAVA-721. |