Elffuss BraidHub

kikocis/java-driver public

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