Skip to content

Commit d24ba1a

Browse files
authored
---
yaml --- r: 7509 b: refs/heads/tswast-patch-1 c: 1f9ed21 h: refs/heads/master i: 7507: a24522e
1 parent cf9626b commit d24ba1a

3 files changed

Lines changed: 140 additions & 1 deletion

File tree

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ refs/tags/v0.18.0: 9d193c4c4b9d1c6f21515dd8e50836b9194ec9bb
5757
refs/tags/v0.19.0: e67b56e4d8dad5f9a7b38c9b2107c23c828f2ed5
5858
refs/tags/v0.20.0: 839f7fb7156535146aa1cb2c5aadd8d375d854e8
5959
refs/tags/v0.20.1: 370471f437f1f4f68a11e068df5cd6bf39edb1fa
60-
refs/heads/tswast-patch-1: 7082ff87d1ae63728e45445c7045fddef7ee6081
60+
refs/heads/tswast-patch-1: 1f9ed217e383515f59e3f61dba0f869664fbe071
6161
refs/heads/pubsub-streaming-pull: 19262b752ee874eb2ca3b950eb2aef44d5a5267b
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)