Skip to content

Commit 9df4005

Browse files
committed
Make BaseTableInfo abstract and other minor fixes
1 parent 338ede1 commit 9df4005

4 files changed

Lines changed: 83 additions & 114 deletions

File tree

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BaseTableInfo.java

Lines changed: 39 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,8 @@
2222
import com.google.api.client.util.Data;
2323
import com.google.api.services.bigquery.model.Streamingbuffer;
2424
import com.google.api.services.bigquery.model.Table;
25-
import com.google.api.services.bigquery.model.ViewDefinition;
2625
import com.google.common.base.Function;
2726
import com.google.common.base.MoreObjects;
28-
import com.google.common.collect.Lists;
2927

3028
import java.io.Serializable;
3129
import java.math.BigInteger;
@@ -38,7 +36,7 @@
3836
*
3937
* @see <a href="https://cloud.google.com/bigquery/docs/tables">Managing Tables</a>
4038
*/
41-
public class BaseTableInfo implements Serializable {
39+
public abstract class BaseTableInfo implements Serializable {
4240

4341
static final Function<Table, BaseTableInfo> FROM_PB_FUNCTION =
4442
new Function<Table, BaseTableInfo>() {
@@ -167,7 +165,7 @@ static StreamingBuffer fromPb(Streamingbuffer streamingBufferPb) {
167165
private final Long expirationTime;
168166
private final Long lastModifiedTime;
169167

170-
public static class Builder<T extends BaseTableInfo, B extends Builder<T, B>> {
168+
public static abstract class Builder<T extends BaseTableInfo, B extends Builder<T, B>> {
171169

172170
private String etag;
173171
private String id;
@@ -201,6 +199,28 @@ protected Builder(BaseTableInfo tableInfo) {
201199
this.lastModifiedTime = tableInfo.lastModifiedTime;
202200
}
203201

202+
protected Builder(Table tablePb) {
203+
this.type = Type.valueOf(tablePb.getType());
204+
this.tableId = TableId.fromPb(tablePb.getTableReference());
205+
if (tablePb.getSchema() != null) {
206+
this.schema(Schema.fromPb(tablePb.getSchema()));
207+
}
208+
if (tablePb.getLastModifiedTime() != null) {
209+
this.lastModifiedTime(tablePb.getLastModifiedTime().longValue());
210+
}
211+
if (tablePb.getNumRows() != null) {
212+
this.numRows(tablePb.getNumRows().longValue());
213+
}
214+
this.description = tablePb.getDescription();
215+
this.expirationTime = tablePb.getExpirationTime();
216+
this.friendlyName = tablePb.getFriendlyName();
217+
this.creationTime = tablePb.getCreationTime();
218+
this.etag = tablePb.getEtag();
219+
this.id = tablePb.getId();
220+
this.numBytes = tablePb.getNumBytes();
221+
this.selfLink = tablePb.getSelfLink();
222+
}
223+
204224
@SuppressWarnings("unchecked")
205225
protected B self() {
206226
return (B) this;
@@ -288,12 +308,9 @@ public B schema(Schema schema) {
288308
}
289309

290310
/**
291-
* Creates a {@code BaseTableInfo} object.
311+
* Creates an object.
292312
*/
293-
@SuppressWarnings("unchecked")
294-
public T build() {
295-
return (T) new BaseTableInfo(this);
296-
}
313+
public abstract T build();
297314
}
298315

299316
protected BaseTableInfo(Builder builder) {
@@ -408,11 +425,9 @@ public Long lastModifiedTime() {
408425
}
409426

410427
/**
411-
* Returns a builder for the {@code BaseTableInfo} object.
428+
* Returns a builder for the object.
412429
*/
413-
public Builder toBuilder() {
414-
return new Builder(this);
415-
}
430+
public abstract Builder toBuilder();
416431

417432
protected MoreObjects.ToStringHelper toStringHelper() {
418433
return MoreObjects.toStringHelper(this)
@@ -458,6 +473,7 @@ Table toPb() {
458473
if (schema != null) {
459474
tablePb.setSchema(schema.toPb());
460475
}
476+
tablePb.setType(type.name());
461477
tablePb.setCreationTime(creationTime);
462478
tablePb.setDescription(description);
463479
tablePb.setEtag(etag);
@@ -471,71 +487,16 @@ Table toPb() {
471487

472488
@SuppressWarnings("unchecked")
473489
static <T extends BaseTableInfo> T fromPb(Table tablePb) {
474-
Builder builder;
475-
TableId tableId = TableId.fromPb(tablePb.getTableReference());
476-
Schema schema = tablePb.getSchema() != null ? Schema.fromPb(tablePb.getSchema()) : null;
477-
if (Objects.equals(tablePb.getType(), Type.VIEW.name()) || tablePb.getView() != null) {
478-
ViewDefinition viewPb = tablePb.getView();
479-
ViewInfo.Builder viewBuilder = ViewInfo.builder(tableId, viewPb.getQuery());
480-
if (tablePb.getView().getUserDefinedFunctionResources() != null) {
481-
viewBuilder.userDefinedFunctions(Lists.transform(viewPb.getUserDefinedFunctionResources(),
482-
UserDefinedFunction.FROM_PB_FUNCTION));
483-
}
484-
builder = viewBuilder;
485-
} else if (Objects.equals(tablePb.getType(), Type.EXTERNAL.name())
486-
|| tablePb.getExternalDataConfiguration() != null) {
487-
ExternalTableInfo.Builder externalBuilder = ExternalTableInfo.builder(tableId,
488-
ExternalDataConfiguration.fromPb(tablePb.getExternalDataConfiguration()));
489-
if (tablePb.getStreamingBuffer() != null) {
490-
externalBuilder.streamingBuffer(StreamingBuffer.fromPb(tablePb.getStreamingBuffer()));
491-
}
492-
builder = externalBuilder;
493-
} else if (Objects.equals(tablePb.getType(), Type.TABLE.name()) || schema != null) {
494-
TableInfo.Builder tableBuilder = TableInfo.builder(tableId, schema);
495-
if (tablePb.getLocation() != null) {
496-
tableBuilder.location(tablePb.getLocation());
497-
}
498-
if (tablePb.getStreamingBuffer() != null) {
499-
tableBuilder.streamingBuffer(StreamingBuffer.fromPb(tablePb.getStreamingBuffer()));
500-
}
501-
builder = tableBuilder;
502-
} else {
503-
// This is for incomplete tables returned by bigquery.listTables
504-
builder = new Builder().tableId(tableId);
505-
}
506-
if (schema != null) {
507-
builder.schema(schema);
508-
}
509-
if (tablePb.getDescription() != null) {
510-
builder.description(tablePb.getDescription());
511-
}
512-
if (tablePb.getExpirationTime() != null) {
513-
builder.expirationTime(tablePb.getExpirationTime());
514-
}
515-
if (tablePb.getFriendlyName() != null) {
516-
builder.friendlyName(tablePb.getFriendlyName());
517-
}
518-
if (tablePb.getLastModifiedTime() != null) {
519-
builder.lastModifiedTime(tablePb.getLastModifiedTime().longValue());
520-
}
521-
if (tablePb.getNumRows() != null) {
522-
builder.numRows(tablePb.getNumRows().longValue());
523-
}
524-
if (tablePb.getCreationTime() != null) {
525-
builder.creationTime(tablePb.getCreationTime());
526-
}
527-
if (tablePb.getEtag() != null) {
528-
builder.etag(tablePb.getEtag());
529-
}
530-
if (tablePb.getId() != null) {
531-
builder.id(tablePb.getId());
532-
}
533-
if (tablePb.getNumBytes() != null) {
534-
builder.numBytes(tablePb.getNumBytes());
535-
}
536-
if (tablePb.getSelfLink() != null) {
537-
builder.selfLink(tablePb.getSelfLink());
490+
switch (Type.valueOf(tablePb.getType())) {
491+
case TABLE:
492+
return (T) TableInfo.fromPb(tablePb);
493+
case VIEW:
494+
return (T) ViewInfo.fromPb(tablePb);
495+
case EXTERNAL:
496+
return (T) ExternalTableInfo.fromPb(tablePb);
497+
default:
498+
// never reached
499+
throw new IllegalArgumentException("Format " + tablePb.getType() + " is not supported");
538500
}
539-
return (T) builder.build();
540501
}
541502
}

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ExternalTableInfo.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@
2121
import com.google.api.services.bigquery.model.Table;
2222
import com.google.common.base.MoreObjects;
2323

24-
import java.util.Objects;
25-
2624
/**
2725
* Google BigQuery External Table information. BigQuery's external tables are tables whose data
2826
* reside outside of BigQuery but can be queried as normal BigQuery tables. External tables are
@@ -51,9 +49,15 @@ private Builder(ExternalTableInfo tableInfo) {
5149
this.streamingBuffer = tableInfo.streamingBuffer;
5250
}
5351

54-
@Override
55-
protected Builder self() {
56-
return this;
52+
protected Builder(Table tablePb) {
53+
super(tablePb);
54+
if (tablePb.getExternalDataConfiguration() != null) {
55+
this.configuration =
56+
ExternalDataConfiguration.fromPb(tablePb.getExternalDataConfiguration());
57+
}
58+
if (tablePb.getStreamingBuffer() != null) {
59+
this.streamingBuffer = StreamingBuffer.fromPb(tablePb.getStreamingBuffer());
60+
}
5761
}
5862

5963
/**
@@ -83,7 +87,7 @@ public ExternalTableInfo build() {
8387

8488
private ExternalTableInfo(Builder builder) {
8589
super(builder);
86-
this.configuration = checkNotNull(builder.configuration);
90+
this.configuration = builder.configuration;
8791
this.streamingBuffer = builder.streamingBuffer;
8892
}
8993

@@ -121,12 +125,6 @@ protected MoreObjects.ToStringHelper toStringHelper() {
121125
.add("streamingBuffer", streamingBuffer);
122126
}
123127

124-
@Override
125-
public boolean equals(Object obj) {
126-
return obj instanceof ExternalTableInfo
127-
&& Objects.equals(toPb(), ((ExternalTableInfo) obj).toPb());
128-
}
129-
130128
@Override
131129
Table toPb() {
132130
Table tablePb = super.toPb();
@@ -156,4 +154,9 @@ public static Builder builder(TableId tableId, ExternalDataConfiguration configu
156154
public static ExternalTableInfo of(TableId table, ExternalDataConfiguration configuration) {
157155
return builder(table, configuration).build();
158156
}
157+
158+
@SuppressWarnings("unchecked")
159+
static ExternalTableInfo fromPb(Table tablePb) {
160+
return new Builder(tablePb).build();
161+
}
159162
}

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/TableInfo.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import com.google.api.services.bigquery.model.Table;
2020
import com.google.common.base.MoreObjects;
2121

22-
import java.util.Objects;
23-
2422
/**
2523
* A Google BigQuery Table information. A BigQuery table is a standard, two-dimensional table with
2624
* individual records organized in rows, and a data type assigned to each column (also called a
@@ -49,9 +47,12 @@ private Builder(TableInfo tableInfo) {
4947
this.streamingBuffer = tableInfo.streamingBuffer;
5048
}
5149

52-
@Override
53-
protected Builder self() {
54-
return this;
50+
protected Builder(Table tablePb) {
51+
super(tablePb);
52+
this.location = tablePb.getLocation();
53+
if (tablePb.getStreamingBuffer() != null) {
54+
this.streamingBuffer = StreamingBuffer.fromPb(tablePb.getStreamingBuffer());
55+
}
5556
}
5657

5758
Builder location(String location) {
@@ -119,7 +120,7 @@ public static BaseTableInfo of(TableId tableId, Schema schema) {
119120
}
120121

121122
/**
122-
* Returns a builder for the {@code ExternalTableInfo} object.
123+
* Returns a builder for the {@code TableInfo} object.
123124
*/
124125
@Override
125126
public Builder toBuilder() {
@@ -133,12 +134,6 @@ protected MoreObjects.ToStringHelper toStringHelper() {
133134
.add("streamingBuffer", streamingBuffer);
134135
}
135136

136-
@Override
137-
public boolean equals(Object obj) {
138-
return obj instanceof TableInfo
139-
&& Objects.equals(toPb(), ((TableInfo) obj).toPb());
140-
}
141-
142137
@Override
143138
Table toPb() {
144139
Table tablePb = super.toPb();
@@ -148,4 +143,9 @@ Table toPb() {
148143
}
149144
return tablePb;
150145
}
146+
147+
@SuppressWarnings("unchecked")
148+
static TableInfo fromPb(Table tablePb) {
149+
return new Builder(tablePb).build();
150+
}
151151
}

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/ViewInfo.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.google.common.collect.Lists;
2626

2727
import java.util.List;
28-
import java.util.Objects;
2928

3029
/**
3130
* Google BigQuery View Table information. BigQuery's views are logical views, not materialized
@@ -54,9 +53,16 @@ private Builder(ViewInfo viewInfo) {
5453
this.userDefinedFunctions = viewInfo.userDefinedFunctions;
5554
}
5655

57-
@Override
58-
protected Builder self() {
59-
return this;
56+
protected Builder(Table tablePb) {
57+
super(tablePb);
58+
ViewDefinition viewPb = tablePb.getView();
59+
if (viewPb != null) {
60+
this.query = viewPb.getQuery();
61+
if (viewPb.getUserDefinedFunctionResources() != null) {
62+
this.userDefinedFunctions = Lists.transform(viewPb.getUserDefinedFunctionResources(),
63+
UserDefinedFunction.FROM_PB_FUNCTION);
64+
}
65+
}
6066
}
6167

6268
/**
@@ -100,7 +106,7 @@ public ViewInfo build() {
100106

101107
private ViewInfo(Builder builder) {
102108
super(builder);
103-
this.query = checkNotNull(builder.query);
109+
this.query = builder.query;
104110
this.userDefinedFunctions = builder.userDefinedFunctions;
105111
}
106112

@@ -137,16 +143,10 @@ protected MoreObjects.ToStringHelper toStringHelper() {
137143
.add("userDefinedFunctions", userDefinedFunctions);
138144
}
139145

140-
@Override
141-
public boolean equals(Object obj) {
142-
return obj instanceof ViewInfo && Objects.equals(toPb(), ((ViewInfo) obj).toPb());
143-
}
144-
145146
@Override
146147
Table toPb() {
147148
Table tablePb = super.toPb();
148-
ViewDefinition viewDefinition = new ViewDefinition()
149-
.setQuery(query);
149+
ViewDefinition viewDefinition = new ViewDefinition().setQuery(query);
150150
if (userDefinedFunctions != null) {
151151
viewDefinition.setUserDefinedFunctionResources(Lists.transform(userDefinedFunctions,
152152
UserDefinedFunction.TO_PB_FUNCTION));
@@ -226,4 +226,9 @@ public static ViewInfo of(TableId table, String query, List<UserDefinedFunction>
226226
public static ViewInfo of(TableId table, String query, UserDefinedFunction... functions) {
227227
return builder(table, query, functions).build();
228228
}
229+
230+
@SuppressWarnings("unchecked")
231+
static ViewInfo fromPb(Table tablePb) {
232+
return new Builder(tablePb).build();
233+
}
229234
}

0 commit comments

Comments
 (0)