Elffuss BraidHub

kikocis/java-driver public

dd37f80f3022 · 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 com.datastax.driver.core.ConsistencyLevel;
19
20import java.util.ArrayList;
21import java.util.List;
22
23import static com.datastax.driver.core.querybuilder.QueryBuilder.quote;
24
25class EntityMapper<T> {
26
27 private final Class<T> entityClass;
28 private final String keyspace;
29 private final String table;
30
31 final ConsistencyLevel writeConsistency;
32 final ConsistencyLevel readConsistency;
33
34 final List<PropertyMapper> partitionKeys = new ArrayList<PropertyMapper>();
35 final List<PropertyMapper> clusteringColumns = new ArrayList<PropertyMapper>();
36
37 final List<PropertyMapper> allColumns = new ArrayList<PropertyMapper>();
38
39 EntityMapper(Class<T> entityClass, String keyspace, String table, ConsistencyLevel writeConsistency, ConsistencyLevel readConsistency) {
40 this.entityClass = entityClass;
41 this.keyspace = keyspace;
42 this.table = table;
43 this.writeConsistency = writeConsistency;
44 this.readConsistency = readConsistency;
45 }
46
47 String getKeyspace() {
48 return quote(keyspace);
49 }
50
51 String getTable() {
52 return quote(table);
53 }
54
55 int primaryKeySize() {
56 return partitionKeys.size() + clusteringColumns.size();
57 }
58
59 PropertyMapper getPrimaryKeyColumn(int i) {
60 return i < partitionKeys.size() ? partitionKeys.get(i) : clusteringColumns.get(i - partitionKeys.size());
61 }
62
63 void addColumns(List<PropertyMapper> pks, List<PropertyMapper> ccs, List<PropertyMapper> rgs) {
64 partitionKeys.addAll(pks);
65 clusteringColumns.addAll(ccs);
66 allColumns.addAll(pks);
67 allColumns.addAll(ccs);
68 allColumns.addAll(rgs);
69 }
70
71 T newEntity() {
72 return ReflectionUtils.newInstance(entityClass);
73 }
74
75}