Skip to content

Commit 7082ff8

Browse files
authored
Add Translate.listSupportedLanguages method and tests (#1158)
1 parent 7ec0064 commit 7082ff8

File tree

6 files changed

+410
-4
lines changed

6 files changed

+410
-4
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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.Preconditions.checkNotNull;
20+
21+
import com.google.cloud.translate.spi.TranslateRpc;
22+
import com.google.common.base.MoreObjects;
23+
24+
import java.io.Serializable;
25+
import java.util.Objects;
26+
27+
/**
28+
* Base class for Translate operation option.
29+
*/
30+
abstract class Option implements Serializable {
31+
32+
private static final long serialVersionUID = 8546712558603322394L;
33+
34+
private final TranslateRpc.Option rpcOption;
35+
private final Object value;
36+
37+
Option(TranslateRpc.Option rpcOption, Object value) {
38+
this.rpcOption = checkNotNull(rpcOption);
39+
this.value = value;
40+
}
41+
42+
TranslateRpc.Option rpcOption() {
43+
return rpcOption;
44+
}
45+
46+
Object value() {
47+
return value;
48+
}
49+
50+
@Override
51+
public boolean equals(Object obj) {
52+
if (!(obj instanceof Option)) {
53+
return false;
54+
}
55+
Option other = (Option) obj;
56+
return Objects.equals(rpcOption, other.rpcOption)
57+
&& Objects.equals(value, other.value);
58+
}
59+
60+
@Override
61+
public int hashCode() {
62+
return Objects.hash(rpcOption, value);
63+
}
64+
65+
@Override
66+
public String toString() {
67+
return MoreObjects.toStringHelper(this)
68+
.add("name", rpcOption.value())
69+
.add("value", value)
70+
.toString();
71+
}
72+
}

gcloud-java-translate/src/main/java/com/google/cloud/translate/Translate.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,57 @@
1717
package com.google.cloud.translate;
1818

1919
import com.google.cloud.Service;
20+
import com.google.cloud.translate.spi.TranslateRpc;
21+
22+
import java.util.List;
2023

2124
/**
2225
* An interface for Google Translate.
2326
*
2427
* @see <a href="https://cloud.google.com/translate/docs">Google Translate</a>
2528
*/
2629
public interface Translate extends Service<TranslateOptions> {
30+
31+
/**
32+
* Class for specifying supported language listing options.
33+
*/
34+
class LanguageListOption extends Option {
35+
36+
private static final long serialVersionUID = 1982978040516658597L;
37+
38+
private LanguageListOption(TranslateRpc.Option rpcOption, String value) {
39+
super(rpcOption, value);
40+
}
41+
42+
/**
43+
* Returns an option for setting the target language. If this option is not provided, the value
44+
* returned by {@link TranslateOptions#targetLanguage()} is used. When provided, the returned
45+
* {@link Language#name()} will be in the language specified by the {@code targetLanguage} code.
46+
*
47+
* @param targetLanguage the target language code
48+
*/
49+
public static LanguageListOption targetLanguage(String targetLanguage) {
50+
return new LanguageListOption(TranslateRpc.Option.TARGET_LANGUAGE, targetLanguage);
51+
}
52+
}
53+
54+
/**
55+
* Returns the list of languages supported by Google Translate. If
56+
* {@link LanguageListOption#targetLanguage(String)} is provided, {@link Language#name()} values
57+
* are localized according to the provided target language. If no such option is passed,
58+
* {@link Language#name()} values are localized according to
59+
* {@link TranslateOptions#targetLanguage()}.
60+
*
61+
* <p>Example of listing supported languages, localized according to
62+
* {@link TranslateOptions#targetLanguage()}:
63+
* <pre> {@code
64+
* List<Language> languages = translate.listSupportedLanguages();
65+
* }</pre>
66+
* Or according to another target language:
67+
* <pre> {@code
68+
* List<Language> languages = translate.listSupportedLanguages(
69+
* LanguageListOption.targetLanguage("es"));
70+
* }</pre>
71+
*/
72+
List<Language> listSupportedLanguages(LanguageListOption... options);
2773
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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.cloud.RetryHelper.runWithRetries;
20+
import static com.google.common.base.Preconditions.checkArgument;
21+
22+
import com.google.api.services.translate.model.LanguagesResource;
23+
import com.google.cloud.BaseService;
24+
import com.google.cloud.RetryHelper.RetryHelperException;
25+
import com.google.cloud.translate.spi.TranslateRpc;
26+
import com.google.common.collect.Lists;
27+
import com.google.common.collect.Maps;
28+
29+
import java.util.List;
30+
import java.util.Map;
31+
import java.util.concurrent.Callable;
32+
33+
final class TranslateImpl extends BaseService<TranslateOptions> implements Translate {
34+
35+
private final TranslateRpc translateRpc;
36+
37+
TranslateImpl(TranslateOptions options) {
38+
super(options);
39+
translateRpc = options.rpc();
40+
}
41+
42+
@Override
43+
public List<Language> listSupportedLanguages(final LanguageListOption... options) {
44+
try {
45+
return Lists.transform(runWithRetries(new Callable<List<LanguagesResource>>() {
46+
@Override
47+
public List<LanguagesResource> call() {
48+
return translateRpc.listSupportedLanguages(optionMap(options));
49+
}
50+
}, options().retryParams(), EXCEPTION_HANDLER, options().clock()), Language.FROM_PB_FUNCTION);
51+
} catch (RetryHelperException e) {
52+
throw TranslateException.translateAndThrow(e);
53+
}
54+
}
55+
56+
private Map<TranslateRpc.Option, ?> optionMap(Option... options) {
57+
Map<TranslateRpc.Option, Object> optionMap = Maps.newEnumMap(TranslateRpc.Option.class);
58+
for (Option option : options) {
59+
Object prev = optionMap.put(option.rpcOption(), option.value());
60+
checkArgument(prev == null, "Duplicate option %s", option);
61+
}
62+
return optionMap;
63+
}
64+
}

gcloud-java-translate/src/main/java/com/google/cloud/translate/TranslateOptions.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,7 @@ public static class DefaultTranslateFactory implements TranslateFactory {
4343

4444
@Override
4545
public Translate create(TranslateOptions options) {
46-
return null;
47-
// todo(mziccard) uncomment as soon as TranslateImpl is implemented
48-
// return new TranslateImpl(options);
46+
return new TranslateImpl(options);
4947
}
5048
}
5149

@@ -174,7 +172,13 @@ public int hashCode() {
174172

175173
@Override
176174
public boolean equals(Object obj) {
177-
return obj instanceof TranslateOptions && baseEquals((TranslateOptions) obj);
175+
if (!(obj instanceof TranslateOptions)) {
176+
return false;
177+
}
178+
TranslateOptions options = (TranslateOptions) obj;
179+
return baseEquals(options)
180+
&& apiKey.equals(options.apiKey)
181+
&& targetLanguage.equals(options.targetLanguage);
178182
}
179183

180184
/**
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertNotEquals;
21+
import static org.junit.Assert.assertNull;
22+
23+
import com.google.cloud.translate.spi.TranslateRpc;
24+
25+
import org.junit.Rule;
26+
import org.junit.Test;
27+
import org.junit.rules.ExpectedException;
28+
29+
public class OptionTest {
30+
31+
private static final TranslateRpc.Option RPC_OPTION = TranslateRpc.Option.SOURCE_LANGUAGE;
32+
private static final String VALUE = "some value";
33+
private static final String OTHER_VALUE = "another value";
34+
private static final Option OPTION = new Option(RPC_OPTION, VALUE) {};
35+
private static final Option OPTION_EQUALS = new Option(RPC_OPTION, VALUE) {};
36+
private static final Option OPTION_NOT_EQUALS = new Option(RPC_OPTION, OTHER_VALUE) {};
37+
38+
@Rule
39+
public ExpectedException thrown = ExpectedException.none();
40+
41+
@Test
42+
public void testEquals() {
43+
assertEquals(OPTION, OPTION_EQUALS);
44+
assertNotEquals(OPTION, OPTION_NOT_EQUALS);
45+
}
46+
47+
@Test
48+
public void testHashCode() {
49+
assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode());
50+
}
51+
52+
@Test
53+
public void testConstructor() {
54+
assertEquals(RPC_OPTION, OPTION.rpcOption());
55+
assertEquals(VALUE, OPTION.value());
56+
Option option = new Option(RPC_OPTION, null) {};
57+
assertEquals(RPC_OPTION, option.rpcOption());
58+
assertNull(option.value());
59+
thrown.expect(NullPointerException.class);
60+
new Option(null, VALUE) {};
61+
}
62+
}

0 commit comments

Comments
 (0)