Skip to content

Commit 608cba1

Browse files
committed
Add Translation class and tests
1 parent 7082ff8 commit 608cba1

2 files changed

Lines changed: 145 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
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+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 static org.junit.Assert.assertEquals;
20+
21+
import com.google.api.services.translate.model.TranslationsResource;
22+
23+
import org.junit.Test;
24+
25+
public class TranslationTest {
26+
27+
private static final String TRANSLATED_TEXT = "Hello world";
28+
private static final String SOURCE_LANGUAGE = "en";
29+
private static final TranslationsResource TRANSLATION_PB = new TranslationsResource()
30+
.setTranslatedText(TRANSLATED_TEXT)
31+
.setDetectedSourceLanguage(SOURCE_LANGUAGE);
32+
private static final Translation TRANSLATION = Translation.fromPb(TRANSLATION_PB);
33+
34+
@Test
35+
public void testFromPb() {
36+
assertEquals(TRANSLATED_TEXT, TRANSLATION.translatedText());
37+
assertEquals(SOURCE_LANGUAGE, TRANSLATION.sourceLanguage());
38+
compareTranslation(TRANSLATION, Translation.fromPb(TRANSLATION_PB));
39+
}
40+
41+
private void compareTranslation(Translation expected, Translation value) {
42+
assertEquals(expected, value);
43+
assertEquals(expected.translatedText(), value.translatedText());
44+
assertEquals(expected.sourceLanguage(), value.sourceLanguage());
45+
assertEquals(expected.hashCode(), value.hashCode());
46+
assertEquals(expected.toString(), value.toString());
47+
}
48+
}

0 commit comments

Comments
 (0)