Skip to content

Commit 1f9ed21

Browse files
authored
Add Detection class and tests (#1161)
1 parent 7082ff8 commit 1f9ed21

2 files changed

Lines changed: 139 additions & 0 deletions

File tree

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.DetectionsResourceItems;
20+
import com.google.common.base.MoreObjects;
21+
22+
import java.io.Serializable;
23+
import java.util.Objects;
24+
25+
/**
26+
* Information about a language detection. Objects of this class contain the detected language and
27+
* possibly a confidence level.
28+
*
29+
* <a href="https://cloud.google.com/translate/v2/detecting-language-with-rest">Detecting Language
30+
* </a>
31+
*/
32+
public class Detection implements Serializable {
33+
34+
private static final long serialVersionUID = 5767106557994900916L;
35+
36+
private final String language;
37+
private final Float confidence;
38+
39+
private Detection(String language, Float confidence) {
40+
this.language = language;
41+
this.confidence = confidence;
42+
}
43+
44+
/**
45+
* Returns the code of the detected language.
46+
*
47+
* @see <a href="https://cloud.google.com/translate/v2/translate-reference#supported_languages">
48+
* Supported Languages</a>
49+
*/
50+
public String language() {
51+
return language;
52+
}
53+
54+
/**
55+
* Returns an optional confidence value in the interval [0,1]. The closer this value is to 1, the
56+
* higher the confidence level for the language detection. Note that this value is not always
57+
* available.
58+
*/
59+
public float confidence() {
60+
return confidence;
61+
}
62+
63+
@Override
64+
public String toString() {
65+
return MoreObjects.toStringHelper(this)
66+
.add("language", language)
67+
.add("confidence", confidence)
68+
.toString();
69+
}
70+
71+
@Override
72+
public final int hashCode() {
73+
return Objects.hash(language, confidence);
74+
}
75+
76+
@Override
77+
public final boolean equals(Object obj) {
78+
if (obj == this) {
79+
return true;
80+
}
81+
if (obj == null || !obj.getClass().equals(Detection.class)) {
82+
return false;
83+
}
84+
Detection other = (Detection) obj;
85+
return Objects.equals(language, other.language)
86+
&& Objects.equals(confidence, other.confidence);
87+
}
88+
89+
static Detection fromPb(DetectionsResourceItems detectionPb) {
90+
return new Detection(detectionPb.getLanguage(), detectionPb.getConfidence());
91+
}
92+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.DetectionsResourceItems;
22+
23+
import org.junit.Test;
24+
25+
public class DetectionTest {
26+
27+
private static final String LANGUAGE = "en";
28+
private static final float CONFIDENCE = 0.42F;
29+
private static final DetectionsResourceItems DETECTION_PB =
30+
new DetectionsResourceItems().setLanguage(LANGUAGE).setConfidence(CONFIDENCE);
31+
private static final Detection DETECTION = Detection.fromPb(DETECTION_PB);
32+
33+
@Test
34+
public void testFromPb() {
35+
assertEquals(LANGUAGE, DETECTION.language());
36+
assertEquals(CONFIDENCE, DETECTION.confidence(), 0);
37+
compareDetection(DETECTION, Detection.fromPb(DETECTION_PB));
38+
}
39+
40+
private void compareDetection(Detection expected, Detection value) {
41+
assertEquals(expected, value);
42+
assertEquals(expected.language(), value.language());
43+
assertEquals(expected.confidence(), value.confidence(), 0);
44+
assertEquals(expected.hashCode(), value.hashCode());
45+
assertEquals(expected.toString(), value.toString());
46+
}
47+
}

0 commit comments

Comments
 (0)