2020import static com .google .common .base .Preconditions .checkNotNull ;
2121
2222import com .google .api .client .util .Data ;
23+ import com .google .api .core .BetaApi ;
2324import com .google .api .services .bigquery .model .Table ;
2425import com .google .common .base .Function ;
2526import com .google .common .base .MoreObjects ;
26-
2727import java .io .Serializable ;
2828import java .math .BigInteger ;
29+ import java .util .Map ;
2930import java .util .Objects ;
3031
3132/**
@@ -65,6 +66,7 @@ public Table apply(TableInfo tableInfo) {
6566 private final Long lastModifiedTime ;
6667 private final TableDefinition definition ;
6768 private final EncryptionConfiguration encryptionConfiguration ;
69+ private final Labels labels ;
6870
6971 /**
7072 * A builder for {@code TableInfo} objects.
@@ -100,7 +102,6 @@ public abstract static class Builder {
100102
101103 abstract Builder setSelfLink (String selfLink );
102104
103-
104105 /**
105106 * Sets the table identity.
106107 */
@@ -114,6 +115,19 @@ public abstract static class Builder {
114115 */
115116 public abstract Builder setDefinition (TableDefinition definition );
116117
118+ /**
119+ * Sets the labels applied to this table.
120+ *
121+ * <p>Unstable, because labels are <a
122+ * href="https://cloud.google.com/bigquery/docs/reference/rest/v2/tables">experimental</a>.
123+ *
124+ * <p>When used with {@link BigQuery#update(TableInfo, TableOption...)}, setting {@code labels}
125+ * to {@code null} removes all labels; otherwise all keys that are mapped to {@code null} values
126+ * are removed and other keys are updated to their respective values.
127+ */
128+ @ BetaApi
129+ public abstract Builder setLabels (Map <String , String > labels );
130+
117131 /**
118132 * Creates a {@code TableInfo} object.
119133 */
@@ -135,6 +149,7 @@ static class BuilderImpl extends Builder {
135149 private Long lastModifiedTime ;
136150 private TableDefinition definition ;
137151 private EncryptionConfiguration encryptionConfiguration ;
152+ private Labels labels = Labels .ZERO ;
138153
139154 BuilderImpl () {}
140155
@@ -150,6 +165,7 @@ static class BuilderImpl extends Builder {
150165 this .lastModifiedTime = tableInfo .lastModifiedTime ;
151166 this .definition = tableInfo .definition ;
152167 this .encryptionConfiguration = tableInfo .encryptionConfiguration ;
168+ this .labels = tableInfo .labels ;
153169 }
154170
155171 BuilderImpl (Table tablePb ) {
@@ -169,6 +185,7 @@ static class BuilderImpl extends Builder {
169185 this .encryptionConfiguration =
170186 new EncryptionConfiguration .Builder (tablePb .getEncryptionConfiguration ()).build ();
171187 }
188+ this .labels = Labels .fromPb (tablePb .getLabels ());
172189 }
173190
174191 @ Override
@@ -244,6 +261,12 @@ public Builder setEncryptionConfiguration(EncryptionConfiguration configuration)
244261 }
245262
246263
264+ @ Override
265+ public Builder setLabels (Map <String , String > labels ) {
266+ this .labels = Labels .fromUser (labels );
267+ return this ;
268+ }
269+
247270 @ Override
248271 public TableInfo build () {
249272 return new TableInfo (this );
@@ -262,6 +285,7 @@ public TableInfo build() {
262285 this .lastModifiedTime = builder .lastModifiedTime ;
263286 this .definition = builder .definition ;
264287 this .encryptionConfiguration = builder .encryptionConfiguration ;
288+ labels = builder .labels ;
265289 }
266290
267291
@@ -351,6 +375,17 @@ public <T extends TableDefinition> T getDefinition() {
351375 return (T ) definition ;
352376 }
353377
378+ /**
379+ * Return a map for labels applied to the table.
380+ *
381+ * <p>Unstable, because labels are <a
382+ * href="https://cloud.google.com/bigquery/docs/reference/rest/v2/tables">experimental</a>.
383+ */
384+ @ BetaApi
385+ public Map <String , String > getLabels () {
386+ return labels .userMap ();
387+ }
388+
354389 /**
355390 * Returns a builder for the table object.
356391 */
@@ -372,6 +407,7 @@ public String toString() {
372407 .add ("lastModifiedTime" , lastModifiedTime )
373408 .add ("definition" , definition )
374409 .add ("encryptionConfiguration" , encryptionConfiguration )
410+ .add ("labels" , labels )
375411 .toString ();
376412 }
377413
@@ -429,6 +465,7 @@ Table toPb() {
429465 if (encryptionConfiguration != null ) {
430466 tablePb .setEncryptionConfiguration (encryptionConfiguration .toPb ());
431467 }
468+ tablePb .setLabels (labels .toPb ());
432469 return tablePb ;
433470 }
434471
0 commit comments