| 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 | */ |
| 16 | package com.datastax.driver.mapping; |
| 17 | |
| 18 | import com.datastax.driver.core.DataType; |
| 19 | |
| 20 | import java.lang.reflect.Field; |
| 21 | import java.util.concurrent.atomic.AtomicInteger; |
| 22 | |
| 23 | import static com.datastax.driver.core.querybuilder.QueryBuilder.quote; |
| 24 | |
| 25 | abstract class ColumnMapper<T> { |
| 26 | |
| 27 | 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 | abstract Object getValue(T entity); |
| 53 | |
| 54 | abstract void setValue(T entity, Object value); |
| 55 | |
| 56 | String getColumnName() { |
| 57 | return kind == Kind.COMPUTED |
| 58 | ? columnName |
| 59 | : quote(columnName); |
| 60 | } |
| 61 | |
| 62 | String getAlias() { |
| 63 | return alias; |
| 64 | } |
| 65 | |
| 66 | DataType getDataType() { |
| 67 | return dataType; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Converts the given value to another object that |
| 72 | * can in turn be serialized. |
| 73 | * Some subclasses might return a different object from the given one |
| 74 | * if the original object is not serializable as is |
| 75 | * and requires some transformation, e.g. instances of |
| 76 | * classes annotated with {@link com.datastax.driver.mapping.annotations.UDT}. |
| 77 | */ |
| 78 | Object toSerializableValue(Object value) { |
| 79 | return value; |
| 80 | } |
| 81 | |
| 82 | } |