Elffuss BraidHub

kikocis/java-driver public

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