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