Elffuss BraidHub

kikocis/java-driver public

bce90754a53a · 6.1 KiBRawPermalink
1/*
2 * Copyright (C) 2012-2014 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.*;
20import java.util.Map.Entry;
21
22import com.google.common.collect.Maps;
23import com.google.common.collect.Sets;
24
25import com.datastax.driver.core.*;
26
27/**
28 * An object handling the mapping of a particular class to a UDT.
29 * <p>
30 * A {@code UDTMapper} object is obtained from a {@code MappingManager} using the
31 * {@link MappingManager#udtMapper} method.
32 * </p>
33 */
34public class UDTMapper<T> {
35 // UDTs are always serialized with the v3 protocol
36 private static final ProtocolVersion UDT_PROTOCOL_VERSION = ProtocolVersion.V3;
37
38 private final EntityMapper<T> entityMapper;
39 private final UserType userType;
40
41 UDTMapper(EntityMapper<T> entityMapper, Session session) {
42 this.entityMapper = entityMapper;
43
44 String keyspace = entityMapper.getKeyspace();
45 String udt = entityMapper.getTable();
46 userType = session.getCluster().getMetadata().getKeyspace(keyspace).getUserType(udt);
47 if (userType == null) {
48 throw new IllegalArgumentException(String.format("Type \"%s\" does not exist in keyspace \"%s\"", udt, keyspace));
49 }
50 }
51
52 /**
53 * Converts a {@link UDTValue} (returned by the core API) to the mapped
54 * class.
55 *
56 * @param v the {@code UDTValue}.
57 * @return an instance of the mapped class.
58 *
59 * @throws IllegalArgumentException if the {@code UDTValue} is not of the
60 * type indicated in the mapped class's {@code @UDT} annotation.
61 */
62 public T fromUDT(UDTValue v) {
63 if (!v.getType().equals(userType)) {
64 String message = String.format("UDT conversion mismatch: expected type %s, got %s",
65 userType, v.getType());
66 throw new IllegalArgumentException(message);
67 }
68 return toEntity(v);
69 }
70
71 /**
72 * Converts a mapped class to a {@link UDTValue}.
73 *
74 * @param entity an instance of the mapped class.
75 *
76 * @return the corresponding {@code UDTValue}.
77 */
78 public UDTValue toUDT(T entity) {
79 UDTValue udtValue = userType.newValue();
80 for (ColumnMapper<T> cm : entityMapper.allColumns()) {
81 Object value = cm.getValue(entity);
82 udtValue.setBytesUnsafe(cm.getColumnName(), value == null ? null : cm.getDataType().serialize(value, UDT_PROTOCOL_VERSION));
83 }
84 return udtValue;
85 }
86
87 UserType getUserType() {
88 return userType;
89 }
90
91 List<UDTValue> toUDTValues(List<T> entities) {
92 if (entities == null)
93 return null;
94
95 List<UDTValue> udtValues = new ArrayList<UDTValue>(entities.size());
96 for (T entity : entities) {
97 UDTValue udtValue = toUDT(entity);
98 udtValues.add(udtValue);
99 }
100 return udtValues;
101 }
102
103 Set<UDTValue> toUDTValues(Set<T> entities) {
104 if (entities == null)
105 return null;
106
107 Set<UDTValue> udtValues = Sets.newHashSetWithExpectedSize(entities.size());
108 for (T entity : entities) {
109 UDTValue udtValue = toUDT(entity);
110 udtValues.add(udtValue);
111 }
112 return udtValues;
113 }
114
115 /*
116 * Map conversion methods are static because they use two mappers, either of
117 * which (but not both) may be null.
118 *
119 * This reflects the fact that the map datatype can use UDTs as keys, or
120 * values, or both.
121 */
122 static <K, V> Map<Object, Object> toUDTValues(Map<K, V> entities, UDTMapper<K> keyMapper, UDTMapper<V> valueMapper) {
123 assert keyMapper != null || valueMapper != null;
124 if (entities == null)
125 return null;
126
127 Map<Object, Object> udtValues = Maps.newHashMapWithExpectedSize(entities.size());
128 for (Entry<K, V> entry : entities.entrySet()) {
129 Object key = (keyMapper == null) ? entry.getKey() : keyMapper.toUDT(entry.getKey());
130 Object value = (valueMapper == null) ? entry.getValue() : valueMapper.toUDT(entry.getValue());
131 udtValues.put(key, value);
132 }
133 return udtValues;
134 }
135
136 T toEntity(UDTValue udtValue) {
137 T entity = entityMapper.newEntity();
138 for (ColumnMapper<T> cm : entityMapper.allColumns()) {
139 ByteBuffer bytes = udtValue.getBytesUnsafe(cm.getColumnName());
140 if (bytes != null)
141 cm.setValue(entity, cm.getDataType().deserialize(bytes, UDT_PROTOCOL_VERSION));
142 }
143 return entity;
144 }
145
146 List<T> toEntities(List<UDTValue> udtValues) {
147 List<T> entities = new ArrayList<T>(udtValues.size());
148 for (UDTValue udtValue : udtValues) {
149 entities.add(toEntity(udtValue));
150 }
151 return entities;
152 }
153
154 Set<T> toEntities(Set<UDTValue> udtValues) {
155 Set<T> entities = Sets.newHashSetWithExpectedSize(udtValues.size());
156 for (UDTValue udtValue : udtValues) {
157 entities.add(toEntity(udtValue));
158 }
159 return entities;
160 }
161
162 @SuppressWarnings("unchecked")
163 static <K, V> Map<K, V> toEntities(Map<Object, Object> udtValues, UDTMapper<K> keyMapper, UDTMapper<V> valueMapper) {
164 Map<K, V> entities = Maps.newHashMapWithExpectedSize(udtValues.size());
165 for (Entry<Object, Object> entry : udtValues.entrySet()) {
166 K key = (keyMapper == null) ? (K) entry.getKey() : keyMapper.toEntity((UDTValue) entry.getKey());
167 V value = (valueMapper == null) ? (V) entry.getValue() : valueMapper.toEntity((UDTValue)entry.getValue());
168 entities.put(key, value);
169 }
170 return entities;
171 }
172}