Elffuss BraidHub

kikocis/java-driver public

a3674bcf4e77 · 3.8 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 java.nio.ByteBuffer;
19import java.util.List;
20import java.util.Map;
21
22import com.google.common.collect.Lists;
23
24import com.datastax.driver.core.*;
25import com.datastax.driver.core.exceptions.InvalidTypeException;
26
27/**
28 * Serializes a class annotated with {@code @UDT} to the corresponding CQL user-defined type.
29 */
30class MappedUDTCodec<T> extends TypeCodec.AbstractUDTCodec<T> {
31 private final UserType cqlUserType;
32 private final Class<T> udtClass;
33 private final Map<String, ColumnMapper<T>> columnMappers;
34 private final CodecRegistry codecRegistry;
35
36 public MappedUDTCodec(UserType cqlUserType, Class<T> udtClass, Map<String, ColumnMapper<T>> columnMappers, MappingManager mappingManager) {
37 super(cqlUserType, udtClass);
38 this.cqlUserType = cqlUserType;
39 this.udtClass = udtClass;
40 this.columnMappers = columnMappers;
41 this.codecRegistry = mappingManager.getSession().getCluster().getConfiguration().getCodecRegistry();
42 }
43
44 @Override
45 protected T newInstance() {
46 try {
47 return udtClass.newInstance();
48 } catch (Exception e) {
49 throw new IllegalArgumentException("Error creating instance of @UDT-annotated class " + udtClass, e);
50 }
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}