From 608cba1b65cc54c63ee347c282247a26d2be5b90 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 15 Aug 2016 10:31:03 +0200 Subject: [PATCH] Add Translation class and tests --- .../google/cloud/translate/Translation.java | 97 +++++++++++++++++++ .../cloud/translate/TranslationTest.java | 48 +++++++++ 2 files changed, 145 insertions(+) create mode 100644 gcloud-java-translate/src/main/java/com/google/cloud/translate/Translation.java create mode 100644 gcloud-java-translate/src/test/java/com/google/cloud/translate/TranslationTest.java diff --git a/gcloud-java-translate/src/main/java/com/google/cloud/translate/Translation.java b/gcloud-java-translate/src/main/java/com/google/cloud/translate/Translation.java new file mode 100644 index 000000000000..ebf9db4835a2 --- /dev/null +++ b/gcloud-java-translate/src/main/java/com/google/cloud/translate/Translation.java @@ -0,0 +1,97 @@ +/* + * 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 com.google.api.services.translate.model.TranslationsResource; +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; + +import java.io.Serializable; +import java.util.Objects; + +/** + * Information about a translation. Objects of this class contain the translated text and the source + * language's code. Source language's code can be the one provided by the user (if any) or the one + * detected by the Google Translate service. + * + * Translating Text + */ +public class Translation implements Serializable { + + private static final long serialVersionUID = 2556017420486245581L; + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public Translation apply(TranslationsResource translationPb) { + return Translation.fromPb(translationPb); + } + }; + + private final String translatedText; + private final String sourceLanguage; + + private Translation(String translatedText, String sourceLanguage) { + this.translatedText = translatedText; + this.sourceLanguage = sourceLanguage; + } + + /** + * Returns the translated text. + */ + public String translatedText() { + return translatedText; + } + + /** + * Returns the language code of the source text. If no source language was provided this value is + * the source language as detected by the Google Translate service. + */ + public String sourceLanguage() { + return sourceLanguage; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("translatedText", translatedText) + .add("sourceLanguage", sourceLanguage) + .toString(); + } + + @Override + public final int hashCode() { + return Objects.hash(translatedText, sourceLanguage); + } + + @Override + public final boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || !obj.getClass().equals(Translation.class)) { + return false; + } + Translation other = (Translation) obj; + return Objects.equals(translatedText, other.translatedText) + && Objects.equals(sourceLanguage, other.sourceLanguage); + } + + static Translation fromPb(TranslationsResource translationPb) { + return new Translation(translationPb.getTranslatedText(), + translationPb.getDetectedSourceLanguage()); + } +} diff --git a/gcloud-java-translate/src/test/java/com/google/cloud/translate/TranslationTest.java b/gcloud-java-translate/src/test/java/com/google/cloud/translate/TranslationTest.java new file mode 100644 index 000000000000..efb819f81362 --- /dev/null +++ b/gcloud-java-translate/src/test/java/com/google/cloud/translate/TranslationTest.java @@ -0,0 +1,48 @@ +/* + * 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 com.google.api.services.translate.model.TranslationsResource; + +import org.junit.Test; + +public class TranslationTest { + + private static final String TRANSLATED_TEXT = "Hello world"; + private static final String SOURCE_LANGUAGE = "en"; + private static final TranslationsResource TRANSLATION_PB = new TranslationsResource() + .setTranslatedText(TRANSLATED_TEXT) + .setDetectedSourceLanguage(SOURCE_LANGUAGE); + private static final Translation TRANSLATION = Translation.fromPb(TRANSLATION_PB); + + @Test + public void testFromPb() { + assertEquals(TRANSLATED_TEXT, TRANSLATION.translatedText()); + assertEquals(SOURCE_LANGUAGE, TRANSLATION.sourceLanguage()); + compareTranslation(TRANSLATION, Translation.fromPb(TRANSLATION_PB)); + } + + private void compareTranslation(Translation expected, Translation value) { + assertEquals(expected, value); + assertEquals(expected.translatedText(), value.translatedText()); + assertEquals(expected.sourceLanguage(), value.sourceLanguage()); + assertEquals(expected.hashCode(), value.hashCode()); + assertEquals(expected.toString(), value.toString()); + } +}