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