Elffuss BraidHub

kikocis/java-driver public

49e1ebe6c225 · 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 @Override
50 protected ByteBuffer serializeField(T source, String fieldName, ProtocolVersion protocolVersion) {
51 // The parent class passes lowercase names unquoted, but in our internal map of mappers they are always quoted
52 if (!fieldName.startsWith("\""))
53 fieldName = Metadata.quote(fieldName);
54
55 ColumnMapper<T> columnMapper = columnMappers.get(fieldName);
56
57 if (columnMapper == null)
58 return null;
59
60 Object value = columnMapper.getValue(source);
61
62 TypeCodec<Object> codec = columnMapper.getCustomCodec();
63 if (codec == null)
64 codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType());
65
66 return codec.serialize(value, protocolVersion);
67 }
68
69 @Override
70 protected T deserializeAndSetField(ByteBuffer input, T target, String fieldName, ProtocolVersion protocolVersion) {
71 if (!fieldName.startsWith("\""))
72 fieldName = Metadata.quote(fieldName);
73
74 ColumnMapper<T> columnMapper = columnMappers.get(fieldName);
75 if (columnMapper != null) {
76 TypeCodec<Object> codec = columnMapper.getCustomCodec();
77 if (codec == null)
78 codec = codecRegistry.codecFor(cqlUserType.getFieldType(columnMapper.getColumnName()), columnMapper.getJavaType());
79 columnMapper.setValue(target, codec.deserialize(input, protocolVersion));
80 }
81 return target;
82 }
83
84 @Override
85 protected String formatField(T source, String fieldName) {
86 // This codec implementation is internal-use only, and its format method is never used
87 throw new UnsupportedOperationException();
88 }
89
90 @Override
91 protected T parseAndSetField(String input, T target, String fieldName) {
92 // This codec implementation is internal-use only, and its parse method is never used
93 throw new UnsupportedOperationException();
94 }
95}