Elffuss BraidHub

kikocis/java-driver public

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