Elffuss BraidHub

kikocis/java-driver public

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