2222import com .google .api .client .util .Data ;
2323import com .google .api .services .bigquery .model .Streamingbuffer ;
2424import com .google .api .services .bigquery .model .Table ;
25- import com .google .api .services .bigquery .model .ViewDefinition ;
2625import com .google .common .base .Function ;
2726import com .google .common .base .MoreObjects ;
28- import com .google .common .collect .Lists ;
2927
3028import java .io .Serializable ;
3129import java .math .BigInteger ;
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}
0 commit comments