Elffuss BraidHub

kikocis/java-driver public

3.1.x · 4.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.mapping;
17
18import com.datastax.driver.core.CodecRegistry;
19import com.datastax.driver.core.ProtocolVersion;
20import com.datastax.driver.core.TypeCodec;
21import com.datastax.driver.core.UserType;
22
23import java.nio.ByteBuffer;
24import java.util.Map;
25
26/**
27 * Serializes a class annotated with {@code @UDT} to the corresponding CQL user-defined type.
28 */
29class MappedUDTCodec<T> extends TypeCodec.AbstractUDTCodec<T> {
30 private final UserType cqlUserType;
31 private final Class<T> udtClass;
32 private final Map<String, PropertyMapper> columnMappers;
33 private final CodecRegistry codecRegistry;
34
35 MappedUDTCodec(UserType cqlUserType, Class<T> udtClass, Map<String, PropertyMapper> columnMappers, MappingManager mappingManager) {
36 super(cqlUserType, udtClass);
37 this.cqlUserType = cqlUserType;
38 this.udtClass = udtClass;
39 this.columnMappers = columnMappers;
40 this.codecRegistry = mappingManager.getSession().getCluster().getConfiguration().getCodecRegistry();
41 }
42
43 @Override
44 protected T newInstance() {
45 return ReflectionUtils.newInstance(udtClass);
46 }
47
48 Class<T> getUdtClass() {
49 return udtClass;
50 }
51
52 @Override
53 protected ByteBuffer serializeField(T source, String fieldName, ProtocolVersion protocolVersion) {
54 PropertyMapper propertyMapper = columnMappers.get(fieldName);
55
56 if (propertyMapper == null)
57 return null;
58
59 Object value = propertyMapper.getValue(source);
60
61 TypeCodec<Object> codec = propertyMapper.customCodec;
62 if (codec == null)
63 codec = codecRegistry.codecFor(cqlUserType.getFieldType(propertyMapper.columnName), propertyMapper.javaType);
64
65 return codec.serialize(value, protocolVersion);
66 }
67
68 @Override
69 protected T deserializeAndSetField(ByteBuffer input, T target, String fieldName, ProtocolVersion protocolVersion) {
70 PropertyMapper propertyMapper = columnMappers.get(fieldName);
71 if (propertyMapper != null) {
72 TypeCodec<Object> codec = propertyMapper.customCodec;
73 if (codec == null)
74 codec = codecRegistry.codecFor(cqlUserType.getFieldType(propertyMapper.columnName), propertyMapper.javaType);
75 propertyMapper.setValue(target, codec.deserialize(input, protocolVersion));
76 }
77 return target;
78 }
79
80 @Override
81 protected String formatField(T source, String fieldName) {
82 PropertyMapper propertyMapper = columnMappers.get(fieldName);
83 if (propertyMapper == null)
84 return null;
85 Object value = propertyMapper.getValue(source);
86 TypeCodec<Object> codec = propertyMapper.customCodec;
87 if (codec == null)
88 codec = codecRegistry.codecFor(cqlUserType.getFieldType(propertyMapper.columnName), propertyMapper.javaType);
89 return codec.format(value);
90 }
91
92 @Override
93 protected T parseAndSetField(String input, T target, String fieldName) {
94 PropertyMapper propertyMapper = columnMappers.get(fieldName);
95 if (propertyMapper != null) {
96 TypeCodec<Object> codec = propertyMapper.customCodec;
97 if (codec == null)
98 codec = codecRegistry.codecFor(cqlUserType.getFieldType(propertyMapper.columnName), propertyMapper.javaType);
99 propertyMapper.setValue(target, codec.parse(input));
100 }
101 return target;
102 }
103}