From f6defed0cbb7fdc87fe950ff97a9246e4fcad579 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Thu, 11 Aug 2016 18:59:55 +0200 Subject: [PATCH] Add Language class and tests --- .../com/google/cloud/translate/Language.java | 97 +++++++++++++++++++ .../google/cloud/translate/LanguageTest.java | 51 ++++++++++ 2 files changed, 148 insertions(+) create mode 100644 gcloud-java-translate/src/main/java/com/google/cloud/translate/Language.java create mode 100644 gcloud-java-translate/src/test/java/com/google/cloud/translate/LanguageTest.java diff --git a/gcloud-java-translate/src/main/java/com/google/cloud/translate/Language.java b/gcloud-java-translate/src/main/java/com/google/cloud/translate/Language.java new file mode 100644 index 000000000000..4847b5ebb540 --- /dev/null +++ b/gcloud-java-translate/src/main/java/com/google/cloud/translate/Language.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.LanguagesResource; +import com.google.common.base.Function; +import com.google.common.base.MoreObjects; + +import java.io.Serializable; +import java.util.Objects; + +/** + * Information about a language supported by Google Translate. Objects of this class contain + * language's code and the language name. + * + * @see + * Discovering Supported Languages + * @see + * Supported Languages + */ +public class Language implements Serializable { + + private static final long serialVersionUID = 5205240279371907020L; + static final Function FROM_PB_FUNCTION = + new Function() { + @Override + public Language apply(LanguagesResource languagePb) { + return Language.fromPb(languagePb); + } + }; + + private final String code; + private final String name; + + private Language(String code, String name) { + this.code = code; + this.name = name; + } + + /** + * Returns the code of the language. + */ + public String code() { + return code; + } + + /** + * Returns the name of the language. + */ + public String name() { + return name; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("code", code) + .add("name", name) + .toString(); + } + + @Override + public final int hashCode() { + return Objects.hash(code, name); + } + + @Override + public final boolean equals(Object obj) { + if (obj == this) { + return true; + } + if (obj == null || !obj.getClass().equals(Language.class)) { + return false; + } + Language other = (Language) obj; + return Objects.equals(code, other.code) + && Objects.equals(name, other.name); + } + + static Language fromPb(LanguagesResource languagePb) { + return new Language(languagePb.getLanguage(), languagePb.getName()); + } +} diff --git a/gcloud-java-translate/src/test/java/com/google/cloud/translate/LanguageTest.java b/gcloud-java-translate/src/test/java/com/google/cloud/translate/LanguageTest.java new file mode 100644 index 000000000000..4f6849270de0 --- /dev/null +++ b/gcloud-java-translate/src/test/java/com/google/cloud/translate/LanguageTest.java @@ -0,0 +1,51 @@ +/* + * 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.assertNull; + +import com.google.api.services.translate.model.LanguagesResource; + +import org.junit.Test; + +public class LanguageTest { + + private static final String CODE = "en"; + private static final String NAME = "English"; + private static final LanguagesResource LANGUAGE_PB = + new LanguagesResource().setLanguage(CODE).setName(NAME); + private static final Language LANGUAGE = Language.fromPb(LANGUAGE_PB); + + @Test + public void testFromPb() { + assertEquals(CODE, LANGUAGE.code()); + assertEquals(NAME, LANGUAGE.name()); + Language language = Language.fromPb(new LanguagesResource().setLanguage(CODE)); + assertEquals(CODE, language.code()); + assertNull(language.name()); + compareLanguage(LANGUAGE, Language.fromPb(LANGUAGE_PB)); + } + + private void compareLanguage(Language expected, Language value) { + assertEquals(expected, value); + assertEquals(expected.name(), value.name()); + assertEquals(expected.code(), value.code()); + assertEquals(expected.hashCode(), value.hashCode()); + assertEquals(expected.toString(), value.toString()); + } +}