|
| 1 | +/* |
| 2 | + * Copyright 2018 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 | +package com.google.cloud.bigtable.data.v2.models; |
| 17 | + |
| 18 | +import com.google.api.pathtemplate.PathTemplate; |
| 19 | +import com.google.api.resourcenames.ResourceName; |
| 20 | +import com.google.api.resourcenames.ResourceNameType; |
| 21 | +import com.google.common.base.Preconditions; |
| 22 | +import com.google.common.collect.ImmutableMap; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | + |
| 27 | +// Copied from com.google.bigtable.admin.v2 |
| 28 | +// TODO: figure out how to unify admin & data resource names |
| 29 | +/** Typesafe representation of a fully qualified Bigtable instance name */ |
| 30 | +public class InstanceName implements ResourceName { |
| 31 | + private static final PathTemplate PATH_TEMPLATE = |
| 32 | + PathTemplate.createWithoutUrlEncoding("projects/{project}/instances/{instance}"); |
| 33 | + |
| 34 | + private volatile Map<String, String> fieldValuesMap; |
| 35 | + |
| 36 | + private final String project; |
| 37 | + private final String instance; |
| 38 | + |
| 39 | + public String getProject() { |
| 40 | + return project; |
| 41 | + } |
| 42 | + |
| 43 | + public String getInstance() { |
| 44 | + return instance; |
| 45 | + } |
| 46 | + |
| 47 | + public static Builder newBuilder() { |
| 48 | + return new Builder(); |
| 49 | + } |
| 50 | + |
| 51 | + public Builder toBuilder() { |
| 52 | + return new Builder(this); |
| 53 | + } |
| 54 | + |
| 55 | + private InstanceName(Builder builder) { |
| 56 | + project = Preconditions.checkNotNull(builder.getProject()); |
| 57 | + instance = Preconditions.checkNotNull(builder.getInstance()); |
| 58 | + } |
| 59 | + |
| 60 | + public static InstanceName of(String project, String instance) { |
| 61 | + return newBuilder() |
| 62 | + .setProject(project) |
| 63 | + .setInstance(instance) |
| 64 | + .build(); |
| 65 | + } |
| 66 | + |
| 67 | + public static String format(String project, String instance) { |
| 68 | + return newBuilder() |
| 69 | + .setProject(project) |
| 70 | + .setInstance(instance) |
| 71 | + .build() |
| 72 | + .toString(); |
| 73 | + } |
| 74 | + |
| 75 | + public static InstanceName parse(String formattedString) { |
| 76 | + if (formattedString.isEmpty()) { |
| 77 | + return null; |
| 78 | + } |
| 79 | + Map<String, String> matchMap = |
| 80 | + PATH_TEMPLATE.validatedMatch(formattedString, "InstanceName.parse: formattedString not in valid format"); |
| 81 | + return of(matchMap.get("project"), matchMap.get("instance")); |
| 82 | + } |
| 83 | + |
| 84 | + public static List<InstanceName> parseList(List<String> formattedStrings) { |
| 85 | + List<InstanceName> list = new ArrayList<>(formattedStrings.size()); |
| 86 | + for (String formattedString : formattedStrings) { |
| 87 | + list.add(parse(formattedString)); |
| 88 | + } |
| 89 | + return list; |
| 90 | + } |
| 91 | + |
| 92 | + public static List<String> toStringList(List<InstanceName> values) { |
| 93 | + List<String> list = new ArrayList<String>(values.size()); |
| 94 | + for (InstanceName value : values) { |
| 95 | + if (value == null) { |
| 96 | + list.add(""); |
| 97 | + } else { |
| 98 | + list.add(value.toString()); |
| 99 | + } |
| 100 | + } |
| 101 | + return list; |
| 102 | + } |
| 103 | + |
| 104 | + public static boolean isParsableFrom(String formattedString) { |
| 105 | + return PATH_TEMPLATE.matches(formattedString); |
| 106 | + } |
| 107 | + |
| 108 | + public Map<String, String> getFieldValuesMap() { |
| 109 | + if (fieldValuesMap == null) { |
| 110 | + synchronized (this) { |
| 111 | + if (fieldValuesMap == null) { |
| 112 | + ImmutableMap.Builder<String, String> fieldMapBuilder = ImmutableMap.builder(); |
| 113 | + fieldMapBuilder.put("project", project); |
| 114 | + fieldMapBuilder.put("instance", instance); |
| 115 | + fieldValuesMap = fieldMapBuilder.build(); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + return fieldValuesMap; |
| 120 | + } |
| 121 | + |
| 122 | + public String getFieldValue(String fieldName) { |
| 123 | + return getFieldValuesMap().get(fieldName); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @deprecated This method is only present to satisfy the ResourceName interface. |
| 128 | + */ |
| 129 | + @Deprecated |
| 130 | + public ResourceNameType getType() { |
| 131 | + throw new UnsupportedOperationException("InstanceName.getType() not supported"); |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public String toString() { |
| 136 | + return PATH_TEMPLATE.instantiate("project", project, "instance", instance); |
| 137 | + } |
| 138 | + |
| 139 | + /** Builder for InstanceName. */ |
| 140 | + public static class Builder { |
| 141 | + |
| 142 | + private String project; |
| 143 | + private String instance; |
| 144 | + |
| 145 | + public String getProject() { |
| 146 | + return project; |
| 147 | + } |
| 148 | + |
| 149 | + public String getInstance() { |
| 150 | + return instance; |
| 151 | + } |
| 152 | + |
| 153 | + public Builder setProject(String project) { |
| 154 | + this.project = project; |
| 155 | + return this; |
| 156 | + } |
| 157 | + |
| 158 | + public Builder setInstance(String instance) { |
| 159 | + this.instance = instance; |
| 160 | + return this; |
| 161 | + } |
| 162 | + |
| 163 | + private Builder() { |
| 164 | + } |
| 165 | + |
| 166 | + private Builder(InstanceName instanceName) { |
| 167 | + project = instanceName.project; |
| 168 | + instance = instanceName.instance; |
| 169 | + } |
| 170 | + |
| 171 | + public InstanceName build() { |
| 172 | + return new InstanceName(this); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public boolean equals(Object o) { |
| 178 | + if (o == this) { |
| 179 | + return true; |
| 180 | + } |
| 181 | + if (o instanceof InstanceName) { |
| 182 | + InstanceName that = (InstanceName) o; |
| 183 | + return (this.project.equals(that.project)) |
| 184 | + && (this.instance.equals(that.instance)); |
| 185 | + } |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + @Override |
| 190 | + public int hashCode() { |
| 191 | + int h = 1; |
| 192 | + h *= 1000003; |
| 193 | + h ^= project.hashCode(); |
| 194 | + h *= 1000003; |
| 195 | + h ^= instance.hashCode(); |
| 196 | + return h; |
| 197 | + } |
| 198 | +} |
| 199 | + |
0 commit comments