Elffuss BraidHub

kikocis/java-driver public

1a7349a254eb · 3.8 KiBRawPermalink
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 */
16package com.datastax.driver.extras.codecs;
17
18import java.nio.ByteBuffer;
19
20import com.google.common.reflect.TypeToken;
21
22import com.datastax.driver.core.ProtocolVersion;
23import com.datastax.driver.core.TypeCodec;
24import com.datastax.driver.core.exceptions.InvalidTypeException;
25
26/**
27 * An abstract {@link TypeCodec} that stores JAVA objects as serialized strings.
28 * This can serve as a base for codecs dealing with XML or JSON formats.
29 * <p>
30 * This codec can be seen as a convenience base class to help
31 * implementing Java-to-XML or Java-to-JSON mappings, but it comes
32 * with a performance penalty: each Java object is serialized
33 * in two steps: first to a String, and then to a {@link ByteBuffer},
34 * which means that each serialization actually incurs in two potentially
35 * expensive operations being carried.
36 * <p>
37 * If you are using an XML or JSON library that supports writing Java objects
38 * directly to {@link ByteBuffer}s, consider writing your own codec instead of
39 * using this one.
40 *
41 * @param <T> The Java type this codec serializes from and deserializes to.
42 */
43public abstract class ParsingCodec<T> extends TypeCodec<T> {
44
45 private final TypeCodec<String> innerCodec;
46
47 public ParsingCodec(Class<T> javaType) {
48 this(TypeToken.of(javaType));
49 }
50
51 public ParsingCodec(TypeToken<T> javaType) {
52 this(TypeCodec.varchar(), javaType);
53 }
54
55 public ParsingCodec(TypeCodec<String> innerCodec, Class<T> javaType) {
56 this(innerCodec, TypeToken.of(javaType));
57 }
58
59 public ParsingCodec(TypeCodec<String> innerCodec, TypeToken<T> javaType) {
60 super(innerCodec.getCqlType(), javaType);
61 this.innerCodec = innerCodec;
62 }
63
64 @Override
65 public ByteBuffer serialize(T value, ProtocolVersion protocolVersion) throws InvalidTypeException {
66 return value == null ? null : innerCodec.serialize(toString(value), protocolVersion);
67 }
68
69 @Override
70 public T deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException {
71 String value = innerCodec.deserialize(bytes, protocolVersion);
72 return value == null ? null : fromString(value);
73 }
74
75 @Override
76 public String format(T value) throws InvalidTypeException {
77 return value == null ? null : innerCodec.format(toString(value));
78 }
79
80 @Override
81 public T parse(String value) throws InvalidTypeException {
82 return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : fromString(innerCodec.parse(value));
83 }
84
85 /**
86 * Return the String representation of the given object;
87 * no special CQL quoting should be applied here.
88 * Null values should be accepted; in most cases, implementors
89 * should return null for null inputs.
90 *
91 * @param value the value to convert into a string
92 * @return the String representation of the given object
93 */
94 protected abstract String toString(T value);
95
96 /**
97 * Parse the given string into an object;
98 * no special CQL unquoting should be applied here.
99 * Null values should be accepted; in most cases, implementors
100 * should return null for null inputs.
101 *
102 * @param value the string to parse
103 * @return the parsed object.
104 */
105 protected abstract T fromString(String value);
106
107}