1 /* 2 * Copyright 2016 Google LLC 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 com.google.api.services.translate.model.LanguagesResource; 20 import com.google.common.base.Function; 21 import com.google.common.base.MoreObjects; 22 import java.io.Serializable; 23 import java.util.Objects; 24 25 /** 26 * Information about a language supported by Google Translation. Objects of this class contain the 27 * language's code and the language name. 28 * 29 * @see <a href="https://cloud.google.com/translate/v2/discovering-supported-languages-with-rest"> 30 * Discovering Supported Languages</a> 31 * @see <a href="https://cloud.google.com/translate/docs/languages">Supported Languages</a> 32 */ 33 public class Language implements Serializable { 34 35 private static final long serialVersionUID = 5205240279371907020L; 36 static final Function<LanguagesResource, Language> FROM_PB_FUNCTION = 37 new Function<LanguagesResource, Language>() { 38 @Override 39 public Language apply(LanguagesResource languagePb) { 40 return Language.fromPb(languagePb); 41 } 42 }; 43 44 private final String code; 45 private final String name; 46 Language(String code, String name)47 private Language(String code, String name) { 48 this.code = code; 49 this.name = name; 50 } 51 52 /** Returns the code of the language. */ getCode()53 public String getCode() { 54 return code; 55 } 56 57 /** Returns the name of the language. */ getName()58 public String getName() { 59 return name; 60 } 61 62 @Override toString()63 public String toString() { 64 return MoreObjects.toStringHelper(this).add("code", code).add("name", name).toString(); 65 } 66 67 @Override hashCode()68 public final int hashCode() { 69 return Objects.hash(code, name); 70 } 71 72 @Override equals(Object obj)73 public final boolean equals(Object obj) { 74 if (obj == this) { 75 return true; 76 } 77 if (obj == null || !obj.getClass().equals(Language.class)) { 78 return false; 79 } 80 Language other = (Language) obj; 81 return Objects.equals(code, other.code) && Objects.equals(name, other.name); 82 } 83 fromPb(LanguagesResource languagePb)84 static Language fromPb(LanguagesResource languagePb) { 85 return new Language(languagePb.getLanguage(), languagePb.getName()); 86 } 87 } 88