-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Detection class and tests (#1161)
- Loading branch information
Showing
2 changed files
with
139 additions
and
0 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
gcloud-java-translate/src/main/java/com/google/cloud/translate/Detection.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.translate; | ||
|
||
import com.google.api.services.translate.model.DetectionsResourceItems; | ||
import com.google.common.base.MoreObjects; | ||
|
||
import java.io.Serializable; | ||
import java.util.Objects; | ||
|
||
/** | ||
* Information about a language detection. Objects of this class contain the detected language and | ||
* possibly a confidence level. | ||
* | ||
* <a href="https://cloud.google.com/translate/v2/detecting-language-with-rest">Detecting Language | ||
* </a> | ||
*/ | ||
public class Detection implements Serializable { | ||
|
||
private static final long serialVersionUID = 5767106557994900916L; | ||
|
||
private final String language; | ||
private final Float confidence; | ||
|
||
private Detection(String language, Float confidence) { | ||
this.language = language; | ||
this.confidence = confidence; | ||
} | ||
|
||
/** | ||
* Returns the code of the detected language. | ||
* | ||
* @see <a href="https://cloud.google.com/translate/v2/translate-reference#supported_languages"> | ||
* Supported Languages</a> | ||
*/ | ||
public String language() { | ||
return language; | ||
} | ||
|
||
/** | ||
* Returns an optional confidence value in the interval [0,1]. The closer this value is to 1, the | ||
* higher the confidence level for the language detection. Note that this value is not always | ||
* available. | ||
*/ | ||
public float confidence() { | ||
return confidence; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return MoreObjects.toStringHelper(this) | ||
.add("language", language) | ||
.add("confidence", confidence) | ||
.toString(); | ||
} | ||
|
||
@Override | ||
public final int hashCode() { | ||
return Objects.hash(language, confidence); | ||
} | ||
|
||
@Override | ||
public final boolean equals(Object obj) { | ||
if (obj == this) { | ||
return true; | ||
} | ||
if (obj == null || !obj.getClass().equals(Detection.class)) { | ||
return false; | ||
} | ||
Detection other = (Detection) obj; | ||
return Objects.equals(language, other.language) | ||
&& Objects.equals(confidence, other.confidence); | ||
} | ||
|
||
static Detection fromPb(DetectionsResourceItems detectionPb) { | ||
return new Detection(detectionPb.getLanguage(), detectionPb.getConfidence()); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
gcloud-java-translate/src/test/java/com/google/cloud/translate/DetectionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2016 Google Inc. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.google.cloud.translate; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import com.google.api.services.translate.model.DetectionsResourceItems; | ||
|
||
import org.junit.Test; | ||
|
||
public class DetectionTest { | ||
|
||
private static final String LANGUAGE = "en"; | ||
private static final float CONFIDENCE = 0.42F; | ||
private static final DetectionsResourceItems DETECTION_PB = | ||
new DetectionsResourceItems().setLanguage(LANGUAGE).setConfidence(CONFIDENCE); | ||
private static final Detection DETECTION = Detection.fromPb(DETECTION_PB); | ||
|
||
@Test | ||
public void testFromPb() { | ||
assertEquals(LANGUAGE, DETECTION.language()); | ||
assertEquals(CONFIDENCE, DETECTION.confidence(), 0); | ||
compareDetection(DETECTION, Detection.fromPb(DETECTION_PB)); | ||
} | ||
|
||
private void compareDetection(Detection expected, Detection value) { | ||
assertEquals(expected, value); | ||
assertEquals(expected.language(), value.language()); | ||
assertEquals(expected.confidence(), value.confidence(), 0); | ||
assertEquals(expected.hashCode(), value.hashCode()); | ||
assertEquals(expected.toString(), value.toString()); | ||
} | ||
} |