Elffuss BraidHub

kikocis/java-driver public

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