Elffuss BraidHub

kikocis/java-driver public

80a57d05fed4 · 4.8 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.*;
19
20import java.util.*;
21
22/**
23 * A {@code ResultSet} mapped to an entity class.
24 */
25public class Result<T> implements Iterable<T> {
26
27 private final ResultSet rs;
28 private final EntityMapper<T> mapper;
29 private final ProtocolVersion protocolVersion;
30 private final boolean useAlias;
31
32 Result(ResultSet rs, EntityMapper<T> mapper, ProtocolVersion protocolVersion) {
33 this(rs, mapper, protocolVersion, false);
34 }
35
36 Result(ResultSet rs, EntityMapper<T> mapper, ProtocolVersion protocolVersion, boolean useAlias) {
37 this.rs = rs;
38 this.mapper = mapper;
39 this.protocolVersion = protocolVersion;
40 this.useAlias = useAlias;
41 }
42
43 private T map(Row row) {
44 T entity = mapper.newEntity();
45 for (ColumnMapper<T> cm : mapper.allColumns()) {
46 String name = cm.getAlias() != null && this.useAlias ? cm.getAlias() : cm.getColumnName();
47 if (!row.getColumnDefinitions().contains(name))
48 continue;
49
50 Object value;
51 TypeCodec<Object> customCodec = cm.getCustomCodec();
52 if (customCodec != null)
53 value = row.get(name, customCodec);
54 else
55 value = row.get(name, cm.getJavaType());
56
57 if (shouldSetValue(value)) {
58 cm.setValue(entity, value);
59 }
60 }
61 return entity;
62 }
63
64 private static boolean shouldSetValue(Object value) {
65 if (value == null)
66 return false;
67 if (value instanceof Collection)
68 return !((Collection) value).isEmpty();
69 if (value instanceof Map)
70 return !((Map) value).isEmpty();
71 return true;
72 }
73
74 /**
75 * Test whether this mapped result set has more results.
76 *
77 * @return whether this mapped result set has more results.
78 */
79 public boolean isExhausted() {
80 return rs.isExhausted();
81 }
82
83 /**
84 * Returns the next result (i.e. the entity corresponding to the next row
85 * in the result set).
86 *
87 * @return the next result in this mapped result set or null if it is exhausted.
88 */
89 public T one() {
90 Row row = rs.one();
91 return row == null ? null : map(row);
92 }
93
94 /**
95 * Returns all the remaining results (entities) in this mapped result set
96 * as a list.
97 *
98 * @return a list containing the remaining results of this mapped result
99 * set. The returned list is empty if and only the result set is exhausted.
100 */
101 public List<T> all() {
102 List<Row> rows = rs.all();
103 List<T> entities = new ArrayList<T>(rows.size());
104 for (Row row : rows) {
105 entities.add(map(row));
106 }
107 return entities;
108 }
109
110 /**
111 * An iterator over the entities of this mapped result set.
112 * <p/>
113 * The {@link Iterator#next} method is equivalent to calling {@link #one}.
114 * So this iterator will consume results and after a full iteration, the
115 * mapped result set (and underlying {@code ResultSet}) will be empty.
116 * <p/>
117 * The returned iterator does not support the {@link Iterator#remove} method.
118 *
119 * @return an iterator that will consume and return the remaining rows of
120 * this mapped result set.
121 */
122 @Override
123 public Iterator<T> iterator() {
124 return new Iterator<T>() {
125 private final Iterator<Row> rowIterator = rs.iterator();
126
127 @Override
128 public boolean hasNext() {
129 return rowIterator.hasNext();
130 }
131
132 @Override
133 public T next() {
134 return map(rowIterator.next());
135 }
136
137 @Override
138 public void remove() {
139 throw new UnsupportedOperationException();
140 }
141 };
142 }
143
144 /**
145 * Returns information on the execution of this query.
146 * <p/>
147 * The returned object includes basic information such as the queried hosts,
148 * but also the Cassandra query trace if tracing was enabled for the query.
149 *
150 * @return the execution info for this query.
151 */
152 public ExecutionInfo getExecutionInfo() {
153 return rs.getExecutionInfo();
154 }
155}