Elffuss BraidHub

kikocis/java-driver public

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