|
48 | 48 | import com.datastax.oss.driver.internal.core.type.codec.UdtCodec; |
49 | 49 | import com.datastax.oss.driver.internal.core.type.codec.UuidCodec; |
50 | 50 | import com.datastax.oss.driver.internal.core.type.codec.VarIntCodec; |
51 | | -import com.datastax.oss.driver.internal.core.type.codec.ZonedTimestampCodec; |
52 | 51 | import com.datastax.oss.driver.shaded.guava.common.base.Charsets; |
53 | 52 | import com.datastax.oss.driver.shaded.guava.common.base.Preconditions; |
54 | 53 | import edu.umd.cs.findbugs.annotations.NonNull; |
|
60 | 59 | import java.time.LocalDate; |
61 | 60 | import java.time.LocalTime; |
62 | 61 | import java.time.ZoneId; |
63 | | -import java.time.ZoneOffset; |
64 | 62 | import java.time.ZonedDateTime; |
65 | 63 | import java.util.List; |
66 | 64 | import java.util.Map; |
67 | 65 | import java.util.Set; |
68 | 66 | import java.util.UUID; |
69 | 67 |
|
70 | | -/** Constants and factory methods to obtain type codec instances. */ |
| 68 | +/** |
| 69 | + * Constants and factory methods to obtain instances of the driver's default type codecs. |
| 70 | + * |
| 71 | + * <p>See also {@link ExtraTypeCodecs} for additional codecs that you can register with your session |
| 72 | + * to handle different type mappings. |
| 73 | + */ |
71 | 74 | public class TypeCodecs { |
72 | 75 |
|
| 76 | + /** The default codec that maps CQL type {@code boolean} to Java's {@code boolean}. */ |
73 | 77 | public static final PrimitiveBooleanCodec BOOLEAN = new BooleanCodec(); |
| 78 | + |
| 79 | + /** The default codec that maps CQL type {@code tinyint} to Java's {@code byte}. */ |
74 | 80 | public static final PrimitiveByteCodec TINYINT = new TinyIntCodec(); |
| 81 | + |
| 82 | + /** The default codec that maps CQL type {@code double} to Java's {@code double}. */ |
75 | 83 | public static final PrimitiveDoubleCodec DOUBLE = new DoubleCodec(); |
| 84 | + |
| 85 | + /** The default codec that maps CQL type {@code counter} to Java's {@code long}. */ |
76 | 86 | public static final PrimitiveLongCodec COUNTER = new CounterCodec(); |
| 87 | + |
| 88 | + /** The default codec that maps CQL type {@code float} to Java's {@code float}. */ |
77 | 89 | public static final PrimitiveFloatCodec FLOAT = new FloatCodec(); |
| 90 | + |
| 91 | + /** The default codec that maps CQL type {@code int} to Java's {@code int}. */ |
78 | 92 | public static final PrimitiveIntCodec INT = new IntCodec(); |
| 93 | + |
| 94 | + /** The default codec that maps CQL type {@code bigint} to Java's {@code long}. */ |
79 | 95 | public static final PrimitiveLongCodec BIGINT = new BigIntCodec(); |
| 96 | + |
| 97 | + /** The default codec that maps CQL type {@code smallint} to Java's {@code short}. */ |
80 | 98 | public static final PrimitiveShortCodec SMALLINT = new SmallIntCodec(); |
81 | | - public static final TypeCodec<Instant> TIMESTAMP = new TimestampCodec(); |
82 | 99 |
|
83 | 100 | /** |
84 | | - * A codec that handles Apache Cassandra(R)'s timestamp type and maps it to Java's {@link |
85 | | - * ZonedDateTime}, using the system's {@linkplain ZoneId#systemDefault() default time zone} as its |
86 | | - * source of time zone information. |
| 101 | + * The default codec that maps CQL type {@code timestamp} to Java's {@link Instant}, using the |
| 102 | + * system's default time zone to parse and format CQL literals. |
| 103 | + * |
| 104 | + * <p>This codec uses the system's {@linkplain ZoneId#systemDefault() default time zone} as its |
| 105 | + * source of time zone information when formatting values as CQL literals, or parsing CQL literals |
| 106 | + * that do not have any time zone indication. Note that this only applies to the {@link |
| 107 | + * TypeCodec#format(Object)} and {@link TypeCodec#parse(String)} methods; regular encoding and |
| 108 | + * decoding, like setting a value on a bound statement or reading a column from a row, are not |
| 109 | + * affected by the time zone. |
87 | 110 | * |
88 | | - * <p>Note that Apache Cassandra(R)'s timestamp type does not store any time zone; this codec is |
89 | | - * provided merely as a convenience for users that need to deal with zoned timestamps in their |
90 | | - * applications. |
| 111 | + * <p>If you need a different time zone, consider other codecs in {@link ExtraTypeCodecs}, or call |
| 112 | + * {@link ExtraTypeCodecs#timestampAt(ZoneId)} instead. |
91 | 113 | * |
92 | | - * @see #ZONED_TIMESTAMP_UTC |
93 | | - * @see #zonedTimestampAt(ZoneId) |
| 114 | + * @see ExtraTypeCodecs#TIMESTAMP_UTC |
| 115 | + * @see ExtraTypeCodecs#timestampAt(ZoneId) |
94 | 116 | */ |
95 | | - public static final TypeCodec<ZonedDateTime> ZONED_TIMESTAMP_SYSTEM = new ZonedTimestampCodec(); |
| 117 | + public static final TypeCodec<Instant> TIMESTAMP = new TimestampCodec(); |
| 118 | + |
| 119 | + /** The default codec that maps CQL type {@code date} to Java's {@link LocalDate}. */ |
| 120 | + public static final TypeCodec<LocalDate> DATE = new DateCodec(); |
| 121 | + |
| 122 | + /** The default codec that maps CQL type {@code time} to Java's {@link LocalTime}. */ |
| 123 | + public static final TypeCodec<LocalTime> TIME = new TimeCodec(); |
96 | 124 |
|
97 | 125 | /** |
98 | | - * A codec that handles Apache Cassandra(R)'s timestamp type and maps it to Java's {@link |
99 | | - * ZonedDateTime}, using {@link ZoneOffset#UTC} as its source of time zone information. |
| 126 | + * The default codec that maps CQL type {@code blob} to Java's {@link ByteBuffer}. |
| 127 | + * |
| 128 | + * <p>If you are looking for a codec mapping CQL type {@code blob} to the Java type {@code |
| 129 | + * byte[]}, you should use {@link ExtraTypeCodecs#BLOB_TO_ARRAY} instead. |
100 | 130 | * |
101 | | - * <p>Note that Apache Cassandra(R)'s timestamp type does not store any time zone; this codec is |
102 | | - * provided merely as a convenience for users that need to deal with zoned timestamps in their |
103 | | - * applications. |
| 131 | + * <p>If you are looking for a codec mapping CQL type {@code list<tinyint>} to the Java type |
| 132 | + * {@code byte[]}, you should use {@link ExtraTypeCodecs#BYTE_LIST_TO_ARRAY} instead. |
104 | 133 | * |
105 | | - * @see #ZONED_TIMESTAMP_SYSTEM |
106 | | - * @see #zonedTimestampAt(ZoneId) |
| 134 | + * @see ExtraTypeCodecs#BLOB_TO_ARRAY |
| 135 | + * @see ExtraTypeCodecs#BYTE_LIST_TO_ARRAY |
107 | 136 | */ |
108 | | - public static final TypeCodec<ZonedDateTime> ZONED_TIMESTAMP_UTC = |
109 | | - new ZonedTimestampCodec(ZoneOffset.UTC); |
110 | | - |
111 | | - public static final TypeCodec<LocalDate> DATE = new DateCodec(); |
112 | | - public static final TypeCodec<LocalTime> TIME = new TimeCodec(); |
113 | 137 | public static final TypeCodec<ByteBuffer> BLOB = new BlobCodec(); |
| 138 | + |
| 139 | + /** The default codec that maps CQL type {@code text} to Java's {@link String}. */ |
114 | 140 | public static final TypeCodec<String> TEXT = new StringCodec(DataTypes.TEXT, Charsets.UTF_8); |
| 141 | + /** The default codec that maps CQL type {@code ascii} to Java's {@link String}. */ |
115 | 142 | public static final TypeCodec<String> ASCII = new StringCodec(DataTypes.ASCII, Charsets.US_ASCII); |
| 143 | + /** The default codec that maps CQL type {@code varint} to Java's {@link BigInteger}. */ |
116 | 144 | public static final TypeCodec<BigInteger> VARINT = new VarIntCodec(); |
| 145 | + /** The default codec that maps CQL type {@code decimal} to Java's {@link BigDecimal}. */ |
117 | 146 | public static final TypeCodec<BigDecimal> DECIMAL = new DecimalCodec(); |
| 147 | + /** The default codec that maps CQL type {@code uuid} to Java's {@link UUID}. */ |
118 | 148 | public static final TypeCodec<UUID> UUID = new UuidCodec(); |
| 149 | + /** The default codec that maps CQL type {@code timeuuid} to Java's {@link UUID}. */ |
119 | 150 | public static final TypeCodec<UUID> TIMEUUID = new TimeUuidCodec(); |
| 151 | + /** The default codec that maps CQL type {@code inet} to Java's {@link InetAddress}. */ |
120 | 152 | public static final TypeCodec<InetAddress> INET = new InetCodec(); |
| 153 | + /** The default codec that maps CQL type {@code duration} to the driver's {@link CqlDuration}. */ |
121 | 154 | public static final TypeCodec<CqlDuration> DURATION = new CqlDurationCodec(); |
122 | 155 |
|
| 156 | + /** |
| 157 | + * Builds a new codec that maps a CQL custom type to Java's {@link ByteBuffer}. |
| 158 | + * |
| 159 | + * @param cqlType the fully-qualified name of the custom type. |
| 160 | + */ |
123 | 161 | @NonNull |
124 | 162 | public static TypeCodec<ByteBuffer> custom(@NonNull DataType cqlType) { |
125 | 163 | Preconditions.checkArgument(cqlType instanceof CustomType, "cqlType must be a custom type"); |
126 | 164 | return new CustomCodec((CustomType) cqlType); |
127 | 165 | } |
128 | 166 |
|
| 167 | + /** |
| 168 | + * Builds a new codec that maps a CQL list to a Java list, using the given codec to map each |
| 169 | + * element. |
| 170 | + */ |
129 | 171 | @NonNull |
130 | 172 | public static <T> TypeCodec<List<T>> listOf(@NonNull TypeCodec<T> elementCodec) { |
131 | 173 | return new ListCodec<>(DataTypes.listOf(elementCodec.getCqlType()), elementCodec); |
132 | 174 | } |
133 | 175 |
|
| 176 | + /** |
| 177 | + * Builds a new codec that maps a CQL set to a Java set, using the given codec to map each |
| 178 | + * element. |
| 179 | + */ |
134 | 180 | @NonNull |
135 | 181 | public static <T> TypeCodec<Set<T>> setOf(@NonNull TypeCodec<T> elementCodec) { |
136 | 182 | return new SetCodec<>(DataTypes.setOf(elementCodec.getCqlType()), elementCodec); |
137 | 183 | } |
138 | 184 |
|
| 185 | + /** |
| 186 | + * Builds a new codec that maps a CQL map to a Java map, using the given codecs to map each key |
| 187 | + * and value. |
| 188 | + */ |
139 | 189 | @NonNull |
140 | 190 | public static <K, V> TypeCodec<Map<K, V>> mapOf( |
141 | 191 | @NonNull TypeCodec<K> keyCodec, @NonNull TypeCodec<V> valueCodec) { |
142 | 192 | return new MapCodec<>( |
143 | 193 | DataTypes.mapOf(keyCodec.getCqlType(), valueCodec.getCqlType()), keyCodec, valueCodec); |
144 | 194 | } |
145 | 195 |
|
| 196 | + /** |
| 197 | + * Builds a new codec that maps a CQL tuple to the driver's {@link TupleValue}, for the given type |
| 198 | + * definition. |
| 199 | + * |
| 200 | + * <p>Note that the components of a {@link TupleValue} are stored in their encoded form. They are |
| 201 | + * encoded/decoded on the fly when you set or get them, using the codec registry. |
| 202 | + */ |
146 | 203 | @NonNull |
147 | 204 | public static TypeCodec<TupleValue> tupleOf(@NonNull TupleType cqlType) { |
148 | 205 | return new TupleCodec(cqlType); |
149 | 206 | } |
150 | 207 |
|
| 208 | + /** |
| 209 | + * Builds a new codec that maps a CQL user defined type to the driver's {@link UdtValue}, for the |
| 210 | + * given type definition. |
| 211 | + * |
| 212 | + * <p>Note that the fields of a {@link UdtValue} are stored in their encoded form. They are |
| 213 | + * encoded/decoded on the fly when you set or get them, using the codec registry. |
| 214 | + */ |
151 | 215 | @NonNull |
152 | 216 | public static TypeCodec<UdtValue> udtOf(@NonNull UserDefinedType cqlType) { |
153 | 217 | return new UdtCodec(cqlType); |
154 | 218 | } |
155 | 219 |
|
156 | 220 | /** |
157 | | - * Returns a codec that handles Apache Cassandra(R)'s timestamp type and maps it to Java's {@link |
158 | | - * ZonedDateTime}, using the supplied {@link ZoneId} as its source of time zone information. |
| 221 | + * An alias for {@link ExtraTypeCodecs#ZONED_TIMESTAMP_SYSTEM}. |
159 | 222 | * |
160 | | - * <p>Note that Apache Cassandra(R)'s timestamp type does not store any time zone; the codecs |
161 | | - * created by this method are provided merely as a convenience for users that need to deal with |
162 | | - * zoned timestamps in their applications. |
| 223 | + * <p>This exists for historical reasons: the constant was originally defined in this class, but |
| 224 | + * technically it belongs to {@link ExtraTypeCodecs} because this is not a built-in mapping. |
| 225 | + */ |
| 226 | + public static final TypeCodec<ZonedDateTime> ZONED_TIMESTAMP_SYSTEM = |
| 227 | + ExtraTypeCodecs.ZONED_TIMESTAMP_SYSTEM; |
| 228 | + |
| 229 | + /** |
| 230 | + * An alias for {@link ExtraTypeCodecs#ZONED_TIMESTAMP_UTC}. |
| 231 | + * |
| 232 | + * <p>This exists for historical reasons: the constant was originally defined in this class, but |
| 233 | + * technically it belongs to {@link ExtraTypeCodecs} because this is not a built-in mapping. |
| 234 | + */ |
| 235 | + public static final TypeCodec<ZonedDateTime> ZONED_TIMESTAMP_UTC = |
| 236 | + ExtraTypeCodecs.ZONED_TIMESTAMP_UTC; |
| 237 | + |
| 238 | + /** |
| 239 | + * An alias for {@link ExtraTypeCodecs#zonedTimestampAt(ZoneId)}. |
163 | 240 | * |
164 | | - * @see #ZONED_TIMESTAMP_SYSTEM |
165 | | - * @see #ZONED_TIMESTAMP_UTC |
| 241 | + * <p>This exists for historical reasons: the method was originally defined in this class, but |
| 242 | + * technically it belongs to {@link ExtraTypeCodecs} because this is not a built-in mapping. |
166 | 243 | */ |
167 | 244 | @NonNull |
168 | 245 | public static TypeCodec<ZonedDateTime> zonedTimestampAt(@NonNull ZoneId timeZone) { |
169 | | - return new ZonedTimestampCodec(timeZone); |
| 246 | + return ExtraTypeCodecs.zonedTimestampAt(timeZone); |
170 | 247 | } |
171 | 248 | } |
0 commit comments