Elffuss BraidHub

kikocis/java-driver public

d66ff12cdb1a · 3.7 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.mapping;
17
18import com.datastax.driver.core.*;
19
20import java.nio.ByteBuffer;
21import java.util.Map;
22
23/**
24 * Serializes a class annotated with {@code @UDT} to the corresponding CQL user-defined type.
25 */
26class MappedUDTCodec<T> extends TypeCodec.AbstractUDTCodec<T> {
27 private final UserType cqlUserType;
28 private final Class<T> udtClass;
29 private final Map<String, ColumnMapper<T>> columnMappers;
30 private final CodecRegistry codecRegistry;
31
32 public MappedUDTCodec(UserType cqlUserType, Class<T> udtClass, Map<String, ColumnMapper<T>> columnMappers, MappingManager mappingManager) {
33 super(cqlUserType, udtClass);
34 this.cqlUserType = cqlUserType;
35 this.udtClass = udtClass;
36 this.columnMappers = columnMappers;
37 this.codecRegistry = mappingManager.getSession().getCluster().getConfiguration().getCodecRegistry();
38 }
39
40 @Override
41 protected T newInstance() {
42 try {
43 return udtClass.newInstance();
44 } catch (Exception e) {
45 throw new IllegalArgumentException("Error creating instance of @UDT-annotated class " + udtClass, e);
46 }
47 }
48
49 Class<T> getUdtClass() {
50 return udtClass;
51 }
52
53 @Override
54 protected ByteBuffer serializeField(T source, String fieldName, ProtocolVersion protocolVersion) {
55 // The parent class passes lowercase names unquoted, but in our internal map of mappers they are always quoted
56 if (!fieldName.startsWith("\""))
57 fieldName = Metadata.quote(fieldName);
58
59 ColumnMapper<T> columnMapper = columnMappers.get(fieldName);
60
61 if (columnMapper == null)
62 return null;
63
64 Object value = columnMapper.getValue(source);
65
66 TypeCodec<Object> codec = columnMapper.getCustomCodec();
67 if (codec == null)
68 codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType());
69
70 return codec.serialize(value, protocolVersion);
71 }
72
73 @Override
74 protected T deserializeAndSetField(ByteBuffer input, T target, String fieldName, ProtocolVersion protocolVersion) {
75 if (!fieldName.startsWith("\""))
76 fieldName = Metadata.quote(fieldName);
77
78 ColumnMapper<T> columnMapper = columnMappers.get(fieldName);
79 if (columnMapper != null) {
80 TypeCodec<Object> codec = columnMapper.getCustomCodec();
81 if (codec == null)
82 codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType());
83 columnMapper.setValue(target, codec.deserialize(input, protocolVersion));
84 }
85 return target;
86 }
87
88 @Override
89 protected String formatField(T source, String fieldName) {
90 // This codec implementation is internal-use only, and its format method is never used
91 throw new UnsupportedOperationException();
92 }
93
94 @Override
95 protected T parseAndSetField(String input, T target, String fieldName) {
96 // This codec implementation is internal-use only, and its parse method is never used
97 throw new UnsupportedOperationException();
98 }
99}