Elffuss BraidHub

kikocis/java-driver public

b4554bb54c38 · 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.google.common.reflect.TypeToken;
22
23import com.datastax.driver.core.Metadata;
24import com.datastax.driver.core.TypeCodec;
25
26abstract class ColumnMapper<T> {
27
28 public enum Kind {PARTITION_KEY, CLUSTERING_COLUMN, REGULAR, COMPUTED};
29
30 private final String columnName;
31 private final String alias;
32 protected final String fieldName;
33 /** The type of the Java field in the mapped class */
34 protected final TypeToken<Object> fieldType;
35 protected final Kind kind;
36 protected final int position;
37 protected final TypeCodec<Object> customCodec;
38
39 @SuppressWarnings("unchecked")
40 protected ColumnMapper(Field field, 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.fieldType = (TypeToken<Object>)TypeToken.of(field.getGenericType());
47 this.kind = AnnotationParser.kind(field);
48 this.position = position;
49 this.customCodec = AnnotationParser.customCodec(field);
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 : Metadata.quote(columnName);
60 }
61
62 public String getAlias() {
63 return alias;
64 }
65
66 public TypeCodec<Object> getCustomCodec() {
67 return customCodec;
68 }
69
70 /**
71 * The Java type of this column.
72 */
73 public TypeToken<Object> getJavaType() {
74 return fieldType;
75 }
76}