Elffuss BraidHub

kikocis/java-driver public

ceb8052ff8a9 · 2.4 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.lang.reflect.Field;
19import java.util.concurrent.atomic.AtomicInteger;
20
21import com.datastax.driver.core.DataType;
22
23import static com.datastax.driver.core.querybuilder.QueryBuilder.quote;
24
25abstract class ColumnMapper<T> {
26
27 public enum Kind {PARTITION_KEY, CLUSTERING_COLUMN, REGULAR, COMPUTED};
28
29 private final String columnName;
30 private final String alias;
31 protected final String fieldName;
32 protected final Class<?> javaType;
33 // Note: dataType is not guaranteed to be exact. Typically, it will be uuid even if the underlying
34 // type is timeuuid. Currently, this is not a problem, but we might allow some @Timeuuid annotation
35 // for the sake of validation (similarly, we'll always have text, never ascii).
36 protected final DataType dataType;
37 protected final Kind kind;
38 protected final int position;
39
40 protected ColumnMapper(Field field, DataType dataType, int position, AtomicInteger columnCounter) {
41 this.columnName = AnnotationParser.columnName(field);
42 this.alias = (columnCounter != null)
43 ? AnnotationParser.newAlias(field, columnCounter.incrementAndGet())
44 : null;
45 this.fieldName = field.getName();
46 this.javaType = field.getType();
47 this.dataType = dataType;
48 this.kind = AnnotationParser.kind(field);
49 this.position = position;
50 }
51
52 public abstract Object getValue(T entity);
53
54 public abstract void setValue(T entity, Object value);
55
56 public String getColumnName() {
57 return kind == Kind.COMPUTED
58 ? columnName
59 : quote(columnName);
60 }
61
62 public String getAlias() {
63 return alias;
64 }
65
66 public DataType getDataType() {
67 return dataType;
68 }
69
70}