-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathTranslateOptions.java
278 lines (235 loc) · 7.72 KB
/
TranslateOptions.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright 2016 Google Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.translate;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.base.Preconditions.checkArgument;
import com.google.auth.Credentials;
import com.google.cloud.HttpServiceOptions;
import com.google.cloud.NoCredentials;
import com.google.cloud.translate.Translate.TranslateOption;
import com.google.cloud.translate.spi.DefaultTranslateRpc;
import com.google.cloud.translate.spi.TranslateRpc;
import com.google.cloud.translate.spi.TranslateRpcFactory;
import com.google.common.collect.ImmutableSet;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.Set;
public class TranslateOptions extends
HttpServiceOptions<Translate, TranslateRpc, TranslateOptions> {
private static final long serialVersionUID = -572597134540398216L;
private static final String DEFAULT_HOST = "https://translation.googleapis.com";
private static final String API_KEY_ENV_NAME = "GOOGLE_API_KEY";
private static final Set<String> SCOPES =
ImmutableSet.of("https://www.googleapis.com/auth/cloud-platform");
private final String apiKey;
private final String targetLanguage;
public static class DefaultTranslateFactory implements TranslateFactory {
private static final TranslateFactory INSTANCE = new DefaultTranslateFactory();
@Override
public Translate create(TranslateOptions options) {
return new TranslateImpl(options);
}
}
public static class DefaultTranslateRpcFactory implements TranslateRpcFactory {
private static final TranslateRpcFactory INSTANCE = new DefaultTranslateRpcFactory();
@Override
public TranslateRpc create(TranslateOptions options) {
return new DefaultTranslateRpc(options);
}
}
public static class Builder extends
HttpServiceOptions.Builder<Translate, TranslateRpc, TranslateOptions, Builder> {
private String apiKey;
private String targetLanguage;
private Builder() {}
private Builder(TranslateOptions options) {
super(options);
this.apiKey = options.apiKey;
}
/**
* Sets project id. Setting a project id has no impact on the {@link Translate} service.
*
* @return the builder
*/
@Override
@Deprecated
public Builder projectId(String projectId) {
return setProjectId(projectId);
}
/**
* Sets project id. Setting a project id has no impact on the {@link Translate} service.
*
* @return the builder
*/
@Override
public Builder setProjectId(String projectId) {
super.setProjectId(projectId);
return self();
}
/**
* Sets the API key used to issue requets. If not set, the API key is looked for in the
* {@code GOOGLE_API_KEY} environment variable. For instructions on how to get an API key see
* <a href="https://cloud.google.com/translate/v2/quickstart">Translate quickstart</a>.
*/
@Deprecated
public Builder apiKey(String apiKey) {
return setApiKey(apiKey);
}
/**
* Sets the API key used to issue requets. If not set, the API key is looked for in the
* {@code GOOGLE_API_KEY} environment variable. For instructions on how to get an API key see
* <a href="https://cloud.google.com/translate/v2/quickstart">Translate quickstart</a>.
*/
public Builder setApiKey(String apiKey) {
this.apiKey = apiKey;
return this;
}
/**
* Sets the code for the default target language. If not set, english ({@code en}) is used.
* {@link Translate#translate(List, TranslateOption...)} and
* {@link Translate#translate(String, TranslateOption...)} calls will use this
* value unless a {@link TranslateOption#targetLanguage(String)} option is explicitly
* provided.
*
* @return the builder
*/
@Deprecated
public Builder targetLanguage(String targetLanguage) {
return setTargetLanguage(targetLanguage);
}
/**
* Sets the code for the default target language. If not set, english ({@code en}) is used.
* {@link Translate#translate(List, TranslateOption...)} and
* {@link Translate#translate(String, TranslateOption...)} calls will use this
* value unless a {@link TranslateOption#targetLanguage(String)} option is explicitly
* provided.
*
* @return the builder
*/
public Builder setTargetLanguage(String targetLanguage) {
this.targetLanguage = targetLanguage;
return self();
}
@Override
public TranslateOptions build() {
return new TranslateOptions(this);
}
}
private TranslateOptions(Builder builder) {
super(TranslateFactory.class, TranslateRpcFactory.class, builder);
this.apiKey = builder.apiKey != null ? builder.apiKey : getDefaultApiKey();
this.targetLanguage = firstNonNull(builder.targetLanguage, Locale.ENGLISH.getLanguage());
}
@Override
protected TranslateFactory getDefaultServiceFactory() {
return DefaultTranslateFactory.INSTANCE;
}
@Override
protected TranslateRpcFactory getDefaultRpcFactory() {
return DefaultTranslateRpcFactory.INSTANCE;
}
@Override
protected boolean projectIdRequired() {
return false;
}
@Override
protected Set<String> getScopes() {
return SCOPES;
}
@Override
protected String getDefaultHost() {
return DEFAULT_HOST;
}
@Deprecated
protected String defaultApiKey() {
return getDefaultApiKey();
}
protected String getDefaultApiKey() {
return System.getProperty(API_KEY_ENV_NAME, System.getenv(API_KEY_ENV_NAME));
}
/**
* Returns the API key, to be used used to send requests.
*/
@Deprecated
public String apiKey() {
return getApiKey();
}
/**
* Returns the API key, to be used used to send requests.
*/
public String getApiKey() {
return apiKey;
}
/**
* Returns the code for the default target language.
*/
@Deprecated
public String targetLanguage() {
return getTargetLanguage();
}
/**
* Returns the code for the default target language.
*/
public String getTargetLanguage() {
return targetLanguage;
}
@SuppressWarnings("unchecked")
@Override
public Builder toBuilder() {
return new Builder(this);
}
@Override
public int hashCode() {
return baseHashCode();
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof TranslateOptions)) {
return false;
}
TranslateOptions options = (TranslateOptions) obj;
return baseEquals(options)
&& Objects.equals(apiKey, options.apiKey)
&& Objects.equals(targetLanguage, options.targetLanguage);
}
/**
* Returns a default {@code TranslateOptions} instance.
*/
@Deprecated
public static TranslateOptions defaultInstance() {
return getDefaultInstance();
}
/**
* Returns a default {@code TranslateOptions} instance.
*/
public static TranslateOptions getDefaultInstance() {
return newBuilder().build();
}
/**
* Returns a builder for {@code TranslateOptions} objects.
*/
@Deprecated
public static Builder builder() {
return newBuilder();
}
/**
* Returns a builder for {@code TranslateOptions} objects.
*/
public static Builder newBuilder() {
return new Builder();
}
}