Skip to content

Commit 7ec0064

Browse files
authored
Add Language class and tests (#1155)
1 parent d4008a4 commit 7ec0064

File tree

2 files changed

+148
-0
lines changed

2 files changed

+148
-0
lines changed
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.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+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
import static org.junit.Assert.assertNull;
21+
22+
import com.google.api.services.translate.model.LanguagesResource;
23+
24+
import org.junit.Test;
25+
26+
public class LanguageTest {
27+
28+
private static final String CODE = "en";
29+
private static final String NAME = "English";
30+
private static final LanguagesResource LANGUAGE_PB =
31+
new LanguagesResource().setLanguage(CODE).setName(NAME);
32+
private static final Language LANGUAGE = Language.fromPb(LANGUAGE_PB);
33+
34+
@Test
35+
public void testFromPb() {
36+
assertEquals(CODE, LANGUAGE.code());
37+
assertEquals(NAME, LANGUAGE.name());
38+
Language language = Language.fromPb(new LanguagesResource().setLanguage(CODE));
39+
assertEquals(CODE, language.code());
40+
assertNull(language.name());
41+
compareLanguage(LANGUAGE, Language.fromPb(LANGUAGE_PB));
42+
}
43+
44+
private void compareLanguage(Language expected, Language value) {
45+
assertEquals(expected, value);
46+
assertEquals(expected.name(), value.name());
47+
assertEquals(expected.code(), value.code());
48+
assertEquals(expected.hashCode(), value.hashCode());
49+
assertEquals(expected.toString(), value.toString());
50+
}
51+
}

0 commit comments

Comments
 (0)