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