Elffuss BraidHub

kikocis/java-driver public

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