|
| 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.TranslationsResource; |
| 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 translation. Objects of this class contain the translated text and the source |
| 28 | + * language's code. Source language's code can be the one provided by the user (if any) or the one |
| 29 | + * detected by the Google Translate service. |
| 30 | + * |
| 31 | + * <a href="https://cloud.google.com/translate/v2/translating-text-with-rest">Translating Text</a> |
| 32 | + */ |
| 33 | +public class Translation implements Serializable { |
| 34 | + |
| 35 | + private static final long serialVersionUID = 2556017420486245581L; |
| 36 | + static final Function<TranslationsResource, Translation> FROM_PB_FUNCTION = |
| 37 | + new Function<TranslationsResource, Translation>() { |
| 38 | + @Override |
| 39 | + public Translation apply(TranslationsResource translationPb) { |
| 40 | + return Translation.fromPb(translationPb); |
| 41 | + } |
| 42 | + }; |
| 43 | + |
| 44 | + private final String translatedText; |
| 45 | + private final String sourceLanguage; |
| 46 | + |
| 47 | + private Translation(String translatedText, String sourceLanguage) { |
| 48 | + this.translatedText = translatedText; |
| 49 | + this.sourceLanguage = sourceLanguage; |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Returns the translated text. |
| 54 | + */ |
| 55 | + public String translatedText() { |
| 56 | + return translatedText; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Returns the language code of the source text. If no source language was provided this value is |
| 61 | + * the source language as detected by the Google Translate service. |
| 62 | + */ |
| 63 | + public String sourceLanguage() { |
| 64 | + return sourceLanguage; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public String toString() { |
| 69 | + return MoreObjects.toStringHelper(this) |
| 70 | + .add("translatedText", translatedText) |
| 71 | + .add("sourceLanguage", sourceLanguage) |
| 72 | + .toString(); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public final int hashCode() { |
| 77 | + return Objects.hash(translatedText, sourceLanguage); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public final boolean equals(Object obj) { |
| 82 | + if (obj == this) { |
| 83 | + return true; |
| 84 | + } |
| 85 | + if (obj == null || !obj.getClass().equals(Translation.class)) { |
| 86 | + return false; |
| 87 | + } |
| 88 | + Translation other = (Translation) obj; |
| 89 | + return Objects.equals(translatedText, other.translatedText) |
| 90 | + && Objects.equals(sourceLanguage, other.sourceLanguage); |
| 91 | + } |
| 92 | + |
| 93 | + static Translation fromPb(TranslationsResource translationPb) { |
| 94 | + return new Translation(translationPb.getTranslatedText(), |
| 95 | + translationPb.getDetectedSourceLanguage()); |
| 96 | + } |
| 97 | +} |
0 commit comments