|
| 1 | +/* |
| 2 | + * Copyright 2024 Google LLC |
| 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 | + * https://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 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.bigtable.admin.v2.models; |
| 18 | + |
| 19 | +import com.google.api.core.InternalApi; |
| 20 | +import com.google.bigtable.admin.v2.SchemaBundleName; |
| 21 | +import com.google.common.base.Objects; |
| 22 | +import com.google.common.base.Preconditions; |
| 23 | +import javax.annotation.Nonnull; |
| 24 | + |
| 25 | +/** |
| 26 | + * A class that wraps the {@link com.google.bigtable.admin.v2.SchemaBundle} protocol buffer object. |
| 27 | + */ |
| 28 | +public final class SchemaBundle { |
| 29 | + private final com.google.bigtable.admin.v2.SchemaBundle proto; |
| 30 | + private final SchemaBundleName schemaBundleName; |
| 31 | + |
| 32 | + /** |
| 33 | + * Wraps the protobuf. This method is considered an internal implementation detail and not meant |
| 34 | + * to be used by applications. |
| 35 | + */ |
| 36 | + @InternalApi |
| 37 | + public static SchemaBundle fromProto(@Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { |
| 38 | + return new SchemaBundle(proto); |
| 39 | + } |
| 40 | + |
| 41 | + private SchemaBundle(@Nonnull com.google.bigtable.admin.v2.SchemaBundle proto) { |
| 42 | + Preconditions.checkNotNull(proto); |
| 43 | + Preconditions.checkArgument(!proto.getName().isEmpty(), "SchemaBundle must have a name"); |
| 44 | + Preconditions.checkArgument( |
| 45 | + proto.hasProtoSchema(), "Schemabundle must have a proto_schema field"); |
| 46 | + this.proto = proto; |
| 47 | + this.schemaBundleName = SchemaBundleName.parse(proto.getName()); |
| 48 | + } |
| 49 | + |
| 50 | + /** Gets the schema bundle's id. */ |
| 51 | + public String getId() { |
| 52 | + //noinspection ConstantConditions |
| 53 | + return schemaBundleName.getSchemaBundle(); |
| 54 | + } |
| 55 | + |
| 56 | + /** Gets the id of the table that owns this schema bundle. */ |
| 57 | + public String getTableId() { |
| 58 | + //noinspection ConstantConditions |
| 59 | + return schemaBundleName.getTable(); |
| 60 | + } |
| 61 | + |
| 62 | + /** Gets the proto schema of this schema bundle. */ |
| 63 | + public com.google.protobuf.ByteString getProtoSchema() { |
| 64 | + if (proto.hasProtoSchema()) { |
| 65 | + return proto.getProtoSchema().getProtoDescriptors(); |
| 66 | + } |
| 67 | + throw new IllegalStateException("This SchemaBundle doesn't have a valid type specified"); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Creates the request protobuf. This method is considered an internal implementation detail and |
| 72 | + * not meant to be used by applications. |
| 73 | + */ |
| 74 | + @InternalApi |
| 75 | + public com.google.bigtable.admin.v2.SchemaBundle toProto() { |
| 76 | + return proto; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public boolean equals(Object o) { |
| 81 | + if (this == o) { |
| 82 | + return true; |
| 83 | + } |
| 84 | + if (o == null || getClass() != o.getClass()) { |
| 85 | + return false; |
| 86 | + } |
| 87 | + SchemaBundle that = (SchemaBundle) o; |
| 88 | + return Objects.equal(proto, that.proto); |
| 89 | + } |
| 90 | + |
| 91 | + @Override |
| 92 | + public int hashCode() { |
| 93 | + return Objects.hashCode(proto); |
| 94 | + } |
| 95 | +} |
0 commit comments