| 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 maps a Java Pojo to another Java object |
| 27 | * that can in turn be serialized into a CQL type. |
| 28 | * This can serve as a base for libraries dealing with Pojo mappings. |
| 29 | * <p/> |
| 30 | * This codec can be seen as a convenience base class for libraries |
| 31 | * dealing with Pojo-to-Pojo mappings, but it comes |
| 32 | * with a performance penalty: each Java object is serialized |
| 33 | * in two steps: first to an intermediate object, 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 such operations are really expensive, and your mapping library is capable |
| 38 | * of serializing objects directly to {@link ByteBuffer}s, |
| 39 | * consider writing your own codec instead of using this one. |
| 40 | * |
| 41 | * @param <O> The "outer" (user-facing) Java type |
| 42 | * @param <I> The "inner" (Cassandra-facing, intermediary) Java type |
| 43 | */ |
| 44 | public abstract class MappingCodec<O, I> extends TypeCodec<O> { |
| 45 | |
| 46 | protected final TypeCodec<I> innerCodec; |
| 47 | |
| 48 | public MappingCodec(TypeCodec<I> innerCodec, Class<O> javaType) { |
| 49 | this(innerCodec, TypeToken.of(javaType)); |
| 50 | } |
| 51 | |
| 52 | public MappingCodec(TypeCodec<I> innerCodec, TypeToken<O> javaType) { |
| 53 | super(innerCodec.getCqlType(), javaType); |
| 54 | this.innerCodec = innerCodec; |
| 55 | } |
| 56 | |
| 57 | @Override |
| 58 | public ByteBuffer serialize(O value, ProtocolVersion protocolVersion) throws InvalidTypeException { |
| 59 | return innerCodec.serialize(serialize(value), protocolVersion); |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public O deserialize(ByteBuffer bytes, ProtocolVersion protocolVersion) throws InvalidTypeException { |
| 64 | return deserialize(innerCodec.deserialize(bytes, protocolVersion)); |
| 65 | } |
| 66 | |
| 67 | @Override |
| 68 | public O parse(String value) throws InvalidTypeException { |
| 69 | return value == null || value.isEmpty() || value.equalsIgnoreCase("NULL") ? null : deserialize(innerCodec.parse(value)); |
| 70 | } |
| 71 | |
| 72 | @Override |
| 73 | public String format(O value) throws InvalidTypeException { |
| 74 | return value == null ? null : innerCodec.format(serialize(value)); |
| 75 | } |
| 76 | |
| 77 | protected abstract O deserialize(I value); |
| 78 | |
| 79 | protected abstract I serialize(O value); |
| 80 | |
| 81 | } |