Elffuss BraidHub

kikocis/java-driver public

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