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