|
| 1 | +/* |
| 2 | + * Copyright 2016 Google Inc. All Rights Reserved. |
| 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 | + * http://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.translate; |
| 18 | + |
| 19 | +import static com.google.common.base.MoreObjects.firstNonNull; |
| 20 | + |
| 21 | +import com.google.cloud.AuthCredentials; |
| 22 | +import com.google.cloud.HttpServiceOptions; |
| 23 | +import com.google.cloud.translate.spi.DefaultTranslateRpc; |
| 24 | +import com.google.cloud.translate.spi.TranslateRpc; |
| 25 | +import com.google.cloud.translate.spi.TranslateRpcFactory; |
| 26 | +import com.google.common.collect.ImmutableSet; |
| 27 | + |
| 28 | +import java.util.Locale; |
| 29 | +import java.util.Set; |
| 30 | + |
| 31 | +public class TranslateOptions extends |
| 32 | + HttpServiceOptions<Translate, TranslateRpc, TranslateOptions> { |
| 33 | + |
| 34 | + private static final long serialVersionUID = 5997441123713672886L; |
| 35 | + private static final Set<String> SCOPES = ImmutableSet.of(); |
| 36 | + |
| 37 | + private final String apiKey; |
| 38 | + private final String targetLanguage; |
| 39 | + |
| 40 | + public static class DefaultTranslateFactory implements TranslateFactory { |
| 41 | + |
| 42 | + private static final TranslateFactory INSTANCE = new DefaultTranslateFactory(); |
| 43 | + |
| 44 | + @Override |
| 45 | + public Translate create(TranslateOptions options) { |
| 46 | + return null; |
| 47 | + // todo(mziccard) uncomment as soon as TranslateImpl is implemented |
| 48 | + // return new TranslateImpl(options); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static class DefaultTranslateRpcFactory implements TranslateRpcFactory { |
| 53 | + |
| 54 | + private static final TranslateRpcFactory INSTANCE = new DefaultTranslateRpcFactory(); |
| 55 | + |
| 56 | + @Override |
| 57 | + public TranslateRpc create(TranslateOptions options) { |
| 58 | + return new DefaultTranslateRpc(options); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + public static class Builder extends |
| 63 | + HttpServiceOptions.Builder<Translate, TranslateRpc, TranslateOptions, Builder> { |
| 64 | + |
| 65 | + private final String apiKey; |
| 66 | + private String targetLanguage; |
| 67 | + |
| 68 | + private Builder(String apiKey) { |
| 69 | + this.apiKey = apiKey; |
| 70 | + } |
| 71 | + |
| 72 | + private Builder(TranslateOptions options) { |
| 73 | + super(options); |
| 74 | + this.apiKey = options.apiKey; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Sets project id. Setting a project id has no impact on the {@link Translate} service. |
| 79 | + * |
| 80 | + * @return the builder |
| 81 | + */ |
| 82 | + @Override |
| 83 | + public Builder projectId(String projectId) { |
| 84 | + super.projectId(projectId); |
| 85 | + return self(); |
| 86 | + } |
| 87 | + |
| 88 | + /** |
| 89 | + * Sets the service authentication credentials. Setting credentials has no impact on the |
| 90 | + * {@link Translate} service. |
| 91 | + * |
| 92 | + * @return the builder |
| 93 | + */ |
| 94 | + public Builder authCredentials(AuthCredentials authCredentials) { |
| 95 | + super.authCredentials(authCredentials); |
| 96 | + return self(); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Sets the code for the default target language. If not set, english ({@code en}) is used. |
| 101 | + * |
| 102 | + * @return the builder |
| 103 | + */ |
| 104 | + public Builder targetLanguage(String targetLanguage) { |
| 105 | + this.targetLanguage = targetLanguage; |
| 106 | + return self(); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public TranslateOptions build() { |
| 111 | + return new TranslateOptions(this); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + private TranslateOptions(Builder builder) { |
| 116 | + super(TranslateFactory.class, TranslateRpcFactory.class, builder); |
| 117 | + this.apiKey = builder.apiKey; |
| 118 | + this.targetLanguage = firstNonNull(builder.targetLanguage, Locale.ENGLISH.getLanguage()); |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + protected TranslateFactory defaultServiceFactory() { |
| 123 | + return DefaultTranslateFactory.INSTANCE; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + protected TranslateRpcFactory defaultRpcFactory() { |
| 128 | + return DefaultTranslateRpcFactory.INSTANCE; |
| 129 | + } |
| 130 | + |
| 131 | + @Override |
| 132 | + protected boolean projectIdRequired() { |
| 133 | + return false; |
| 134 | + } |
| 135 | + |
| 136 | + @Override |
| 137 | + protected Set<String> scopes() { |
| 138 | + return SCOPES; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Returns the API key, to be used used to send requests. |
| 143 | + */ |
| 144 | + public String apiKey() { |
| 145 | + return apiKey; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Returns the code for the default target language. |
| 150 | + */ |
| 151 | + public String targetLanguage() { |
| 152 | + return targetLanguage; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Returns a default {@code TranslateOptions} instance given an API key. For instructions on |
| 157 | + * how to get an API key see <a href="https://cloud.google.com/translate/v2/quickstart">Translate |
| 158 | + * quickstart</a>. |
| 159 | + */ |
| 160 | + public static TranslateOptions defaultInstance(String apiKey) { |
| 161 | + return builder(apiKey).build(); |
| 162 | + } |
| 163 | + |
| 164 | + @SuppressWarnings("unchecked") |
| 165 | + @Override |
| 166 | + public Builder toBuilder() { |
| 167 | + return new Builder(this); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public int hashCode() { |
| 172 | + return baseHashCode(); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public boolean equals(Object obj) { |
| 177 | + return obj instanceof TranslateOptions && baseEquals((TranslateOptions) obj); |
| 178 | + } |
| 179 | + |
| 180 | + /** |
| 181 | + * Creates a builder for {@code TranslateOptions} objects given an api key. For instructions on |
| 182 | + * how to get an API key see <a href="https://cloud.google.com/translate/v2/quickstart">Translate |
| 183 | + * quickstart</a>. |
| 184 | + */ |
| 185 | + public static Builder builder(String apiKey) { |
| 186 | + return new Builder(apiKey); |
| 187 | + } |
| 188 | +} |
0 commit comments