Elffuss BraidHub

kikocis/java-driver public

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