Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* 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.Preconditions.checkNotNull;

import com.google.cloud.translate.spi.TranslateRpc;
import com.google.common.base.MoreObjects;

import java.io.Serializable;
import java.util.Objects;

/**
* Base class for Translate operation option.
*/
abstract class Option implements Serializable {

private static final long serialVersionUID = 8546712558603322394L;

private final TranslateRpc.Option rpcOption;
private final Object value;

Option(TranslateRpc.Option rpcOption, Object value) {
this.rpcOption = checkNotNull(rpcOption);
this.value = value;
}

TranslateRpc.Option rpcOption() {
return rpcOption;
}

Object value() {
return value;
}

@Override
public boolean equals(Object obj) {
if (!(obj instanceof Option)) {
return false;
}
Option other = (Option) obj;
return Objects.equals(rpcOption, other.rpcOption)
&& Objects.equals(value, other.value);
}

@Override
public int hashCode() {
return Objects.hash(rpcOption, value);
}

@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("name", rpcOption.value())
.add("value", value)
.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,57 @@
package com.google.cloud.translate;

import com.google.cloud.Service;
import com.google.cloud.translate.spi.TranslateRpc;

import java.util.List;

/**
* An interface for Google Translate.
*
* @see <a href="https://cloud.google.com/translate/docs">Google Translate</a>
*/
public interface Translate extends Service<TranslateOptions> {

/**
* Class for specifying supported language listing options.
*/
class LanguageListOption extends Option {

private static final long serialVersionUID = 1982978040516658597L;

private LanguageListOption(TranslateRpc.Option rpcOption, String value) {
super(rpcOption, value);
}

/**
* Returns an option for setting the target language. If this option is not provided, the value
* returned by {@link TranslateOptions#targetLanguage()} is used. When provided, the returned
* {@link Language#name()} will be in the language specified by the {@code targetLanguage} code.
*
* @param targetLanguage the target language code
*/
public static LanguageListOption targetLanguage(String targetLanguage) {
return new LanguageListOption(TranslateRpc.Option.TARGET_LANGUAGE, targetLanguage);
}
}

/**
* Returns the list of languages supported by Google Translate. If
* {@link LanguageListOption#targetLanguage(String)} is provided, {@link Language#name()} values
* are localized according to the provided target language. If no such option is passed,
* {@link Language#name()} values are localized according to
* {@link TranslateOptions#targetLanguage()}.
*
* <p>Example of listing supported languages, localized according to
* {@link TranslateOptions#targetLanguage()}:
* <pre> {@code
* List<Language> languages = translate.listSupportedLanguages();
* }</pre>
* Or according to another target language:
* <pre> {@code
* List<Language> languages = translate.listSupportedLanguages(
* LanguageListOption.targetLanguage("es"));
* }</pre>
*/
List<Language> listSupportedLanguages(LanguageListOption... options);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* 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.cloud.RetryHelper.runWithRetries;
import static com.google.common.base.Preconditions.checkArgument;

import com.google.api.services.translate.model.LanguagesResource;
import com.google.cloud.BaseService;
import com.google.cloud.RetryHelper.RetryHelperException;
import com.google.cloud.translate.spi.TranslateRpc;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

final class TranslateImpl extends BaseService<TranslateOptions> implements Translate {

private final TranslateRpc translateRpc;

TranslateImpl(TranslateOptions options) {
super(options);
translateRpc = options.rpc();
}

@Override
public List<Language> listSupportedLanguages(final LanguageListOption... options) {
try {
return Lists.transform(runWithRetries(new Callable<List<LanguagesResource>>() {
@Override
public List<LanguagesResource> call() {
return translateRpc.listSupportedLanguages(optionMap(options));
}
}, options().retryParams(), EXCEPTION_HANDLER, options().clock()), Language.FROM_PB_FUNCTION);
} catch (RetryHelperException e) {
throw TranslateException.translateAndThrow(e);
}
}

private Map<TranslateRpc.Option, ?> optionMap(Option... options) {
Map<TranslateRpc.Option, Object> optionMap = Maps.newEnumMap(TranslateRpc.Option.class);
for (Option option : options) {
Object prev = optionMap.put(option.rpcOption(), option.value());
checkArgument(prev == null, "Duplicate option %s", option);
}
return optionMap;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ public static class DefaultTranslateFactory implements TranslateFactory {

@Override
public Translate create(TranslateOptions options) {
return null;
// todo(mziccard) uncomment as soon as TranslateImpl is implemented
// return new TranslateImpl(options);
return new TranslateImpl(options);
}
}

Expand Down Expand Up @@ -174,7 +172,13 @@ public int hashCode() {

@Override
public boolean equals(Object obj) {
return obj instanceof TranslateOptions && baseEquals((TranslateOptions) obj);
if (!(obj instanceof TranslateOptions)) {
return false;
}
TranslateOptions options = (TranslateOptions) obj;
return baseEquals(options)
&& apiKey.equals(options.apiKey)
&& targetLanguage.equals(options.targetLanguage);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull;

import com.google.cloud.translate.spi.TranslateRpc;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

public class OptionTest {

private static final TranslateRpc.Option RPC_OPTION = TranslateRpc.Option.SOURCE_LANGUAGE;
private static final String VALUE = "some value";
private static final String OTHER_VALUE = "another value";
private static final Option OPTION = new Option(RPC_OPTION, VALUE) {};
private static final Option OPTION_EQUALS = new Option(RPC_OPTION, VALUE) {};
private static final Option OPTION_NOT_EQUALS = new Option(RPC_OPTION, OTHER_VALUE) {};

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testEquals() {
assertEquals(OPTION, OPTION_EQUALS);
assertNotEquals(OPTION, OPTION_NOT_EQUALS);
}

@Test
public void testHashCode() {
assertEquals(OPTION.hashCode(), OPTION_EQUALS.hashCode());
}

@Test
public void testConstructor() {
assertEquals(RPC_OPTION, OPTION.rpcOption());
assertEquals(VALUE, OPTION.value());
Option option = new Option(RPC_OPTION, null) {};
assertEquals(RPC_OPTION, option.rpcOption());
assertNull(option.value());
thrown.expect(NullPointerException.class);
new Option(null, VALUE) {};
}
}
Loading