-
Notifications
You must be signed in to change notification settings - Fork 119
/
TableResult.java
152 lines (126 loc) · 4.41 KB
/
TableResult.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Copyright 2018 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.bigquery;
import com.google.api.gax.paging.Page;
import com.google.auto.value.AutoValue;
import com.google.common.base.Function;
import com.google.common.base.MoreObjects;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import java.io.Serializable;
import java.util.Objects;
import javax.annotation.Nullable;
@AutoValue
public abstract class TableResult implements Page<FieldValueList>, Serializable {
private static final long serialVersionUID = 1L;
@AutoValue.Builder
public abstract static class Builder {
public abstract TableResult.Builder setSchema(Schema schema);
/**
* Sets the total number of rows in the complete result set, which can be more than the number
* of rows in the first page of results returned by {@link #getValues()}.
*/
public abstract TableResult.Builder setTotalRows(Long totalRows);
public abstract TableResult.Builder setJobId(JobId jobId);
public abstract TableResult.Builder setPageNoSchema(Page<FieldValueList> pageNoSchema);
public abstract TableResult.Builder setQueryId(String queryId);
/** Creates a @code TableResult} object. */
public abstract TableResult build();
}
public abstract Builder toBuilder();
public static Builder newBuilder() {
return new AutoValue_TableResult.Builder();
}
/** Returns the schema of the results. Null if the schema is not supplied. */
@Nullable
public abstract Schema getSchema();
public abstract long getTotalRows();
public abstract Page<FieldValueList> getPageNoSchema();
@Nullable
public abstract JobId getJobId();
@Nullable
public abstract String getQueryId();
@Override
public boolean hasNextPage() {
return getPageNoSchema().hasNextPage();
}
@Override
public String getNextPageToken() {
return getPageNoSchema().getNextPageToken();
}
@Override
public TableResult getNextPage() {
if (getPageNoSchema().hasNextPage()) {
return TableResult.newBuilder()
.setSchema(getSchema())
.setTotalRows(getTotalRows())
.setPageNoSchema(getPageNoSchema().getNextPage())
.setQueryId(getQueryId())
.build();
}
return null;
}
@Override
public Iterable<FieldValueList> iterateAll() {
return addSchema(getPageNoSchema().iterateAll());
}
@Override
public Iterable<FieldValueList> getValues() {
return addSchema(getPageNoSchema().getValues());
}
private Iterable<FieldValueList> addSchema(Iterable<FieldValueList> iter) {
if (getSchema() == null) {
return iter;
}
return Iterables.transform(
iter,
new Function<FieldValueList, FieldValueList>() {
@Override
public FieldValueList apply(FieldValueList list) {
return list.withSchema(getSchema().getFields());
}
});
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("rows", getValues())
.add("schema", getSchema())
.add("totalRows", getTotalRows())
.add("cursor", getNextPageToken())
.add("queryId", getQueryId())
.toString();
}
@Override
public final int hashCode() {
return Objects.hash(getPageNoSchema(), getSchema(), getTotalRows(), getQueryId());
}
@Override
public final boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (obj == null || !obj.getClass().equals(AutoValue_TableResult.class)) {
return false;
}
TableResult response = (TableResult) obj;
return Objects.equals(getNextPageToken(), response.getNextPageToken())
&& Iterators.elementsEqual(getValues().iterator(), response.getValues().iterator())
&& Objects.equals(getSchema(), response.getSchema())
&& getTotalRows() == response.getTotalRows()
&& getQueryId() == response.getQueryId();
}
}