| 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.ConsistencyLevel; |
| 19 | |
| 20 | import java.lang.reflect.Field; |
| 21 | import java.util.ArrayList; |
| 22 | import java.util.List; |
| 23 | import java.util.concurrent.atomic.AtomicInteger; |
| 24 | |
| 25 | import static com.datastax.driver.core.querybuilder.QueryBuilder.quote; |
| 26 | |
| 27 | abstract class EntityMapper<T> { |
| 28 | |
| 29 | final Class<T> entityClass; |
| 30 | private final String keyspace; |
| 31 | private final String table; |
| 32 | |
| 33 | final ConsistencyLevel writeConsistency; |
| 34 | final ConsistencyLevel readConsistency; |
| 35 | |
| 36 | final List<ColumnMapper<T>> partitionKeys = new ArrayList<ColumnMapper<T>>(); |
| 37 | final List<ColumnMapper<T>> clusteringColumns = new ArrayList<ColumnMapper<T>>(); |
| 38 | final List<ColumnMapper<T>> regularColumns = new ArrayList<ColumnMapper<T>>(); |
| 39 | |
| 40 | private final List<ColumnMapper<T>> allColumns = new ArrayList<ColumnMapper<T>>(); |
| 41 | |
| 42 | protected EntityMapper(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency) { |
| 43 | this.entityClass = entityClass; |
| 44 | this.keyspace = keyspace; |
| 45 | this.table = table; |
| 46 | this.writeConsistency = writeConsistency; |
| 47 | this.readConsistency = readConsistency; |
| 48 | } |
| 49 | |
| 50 | String getKeyspace() { |
| 51 | return quote(keyspace); |
| 52 | } |
| 53 | |
| 54 | String getTable() { |
| 55 | return quote(table); |
| 56 | } |
| 57 | |
| 58 | int primaryKeySize() { |
| 59 | return partitionKeys.size() + clusteringColumns.size(); |
| 60 | } |
| 61 | |
| 62 | ColumnMapper<T> getPrimaryKeyColumn(int i) { |
| 63 | return i < partitionKeys.size() ? partitionKeys.get(i) : clusteringColumns.get(i - partitionKeys.size()); |
| 64 | } |
| 65 | |
| 66 | void addColumns(List<ColumnMapper<T>> pks, List<ColumnMapper<T>> ccs, List<ColumnMapper<T>> rgs) { |
| 67 | partitionKeys.addAll(pks); |
| 68 | allColumns.addAll(pks); |
| 69 | |
| 70 | clusteringColumns.addAll(ccs); |
| 71 | allColumns.addAll(ccs); |
| 72 | |
| 73 | addColumns(rgs); |
| 74 | } |
| 75 | |
| 76 | void addColumns(List<ColumnMapper<T>> rgs) { |
| 77 | regularColumns.addAll(rgs); |
| 78 | allColumns.addAll(rgs); |
| 79 | } |
| 80 | |
| 81 | abstract T newEntity(); |
| 82 | |
| 83 | List<ColumnMapper<T>> allColumns() { |
| 84 | return allColumns; |
| 85 | } |
| 86 | |
| 87 | interface Factory { |
| 88 | public <T> EntityMapper<T> create(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency); |
| 89 | |
| 90 | public <T> ColumnMapper<T> createColumnMapper(Class<T> componentClass, Field field, int position, MappingManager mappingManager, AtomicInteger columnCounter); |
| 91 | } |
| 92 | } |