Elffuss BraidHub

kikocis/java-driver public

0960065b032a · 2.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.PreparedStatement;
19import com.google.common.util.concurrent.Futures;
20import com.google.common.util.concurrent.ListenableFuture;
21
22import java.util.ArrayList;
23import java.util.List;
24
25abstract class AccessorMapper<T> {
26
27 public final Class<T> daoClass;
28 protected final List<MethodMapper> methods;
29
30 protected AccessorMapper(Class<T> daoClass, List<MethodMapper> methods) {
31 this.daoClass = daoClass;
32 this.methods = methods;
33 }
34
35 abstract T createProxy();
36
37 public void prepare(MappingManager manager) {
38 List<ListenableFuture<PreparedStatement>> statements = new ArrayList<ListenableFuture<PreparedStatement>>(methods.size());
39
40 for (MethodMapper method : methods)
41 statements.add(manager.getSession().prepareAsync(method.queryString));
42
43 try {
44 List<PreparedStatement> preparedStatements = Futures.allAsList(statements).get();
45 for (int i = 0; i < methods.size(); i++)
46 methods.get(i).prepare(manager, preparedStatements.get(i));
47 } catch (Exception e) {
48 throw new RuntimeException("Error preparing queries for accessor " + daoClass.getSimpleName(), e);
49 }
50 }
51
52 interface Factory {
53 public <T> AccessorMapper<T> create(Class<T> daoClass, List<MethodMapper> methods);
54 }
55}