Elffuss BraidHub

kikocis/java-driver public

e677d783d957 · 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 com.datastax.driver.core.Metadata;
19import com.datastax.driver.core.TypeCodec;
20import com.google.common.reflect.TypeToken;
21
22import java.lang.reflect.Field;
23import java.util.concurrent.atomic.AtomicInteger;
24
25abstract class ColumnMapper<T> {
26
27 public enum Kind {PARTITION_KEY, CLUSTERING_COLUMN, REGULAR, COMPUTED}
28
29 ;
30
31 private final String columnName;
32 private final String alias;
33 protected final String fieldName;
34 /**
35 * The type of the Java field in the mapped class
36 */
37 protected final TypeToken<Object> fieldType;
38 protected final Kind kind;
39 protected final int position;
40 protected final TypeCodec<Object> customCodec;
41
42 @SuppressWarnings("unchecked")
43 protected ColumnMapper(Field field, int position, AtomicInteger columnCounter) {
44 this.columnName = AnnotationParser.columnName(field);
45 this.alias = (columnCounter != null)
46 ? AnnotationParser.newAlias(field, columnCounter.incrementAndGet())
47 : null;
48 this.fieldName = field.getName();
49 this.fieldType = (TypeToken<Object>) TypeToken.of(field.getGenericType());
50 this.kind = AnnotationParser.kind(field);
51 this.position = position;
52 this.customCodec = AnnotationParser.customCodec(field);
53 }
54
55 public abstract Object getValue(T entity);
56
57 public abstract void setValue(T entity, Object value);
58
59 public String getColumnName() {
60 return kind == Kind.COMPUTED
61 ? columnName
62 : Metadata.quote(columnName);
63 }
64
65 public String getAlias() {
66 return alias;
67 }
68
69 public TypeCodec<Object> getCustomCodec() {
70 return customCodec;
71 }
72
73 /**
74 * The Java type of this column.
75 */
76 public TypeToken<Object> getJavaType() {
77 return fieldType;
78 }
79}