From c42c5383166be3e78ab7f7e2bfa21f4905528578 Mon Sep 17 00:00:00 2001 From: Marco Ziccardi Date: Mon, 15 Aug 2016 09:55:33 +0200 Subject: [PATCH] Add Detection class and tests --- .../com/google/cloud/translate/Detection.java | 92 +++++++++++++++++++ .../google/cloud/translate/DetectionTest.java | 47 ++++++++++ 2 files changed, 139 insertions(+) create mode 100644 gcloud-java-translate/src/main/java/com/google/cloud/translate/Detection.java create mode 100644 gcloud-java-translate/src/test/java/com/google/cloud/translate/DetectionTest.java diff --git a/gcloud-java-translate/src/main/java/com/google/cloud/translate/Detection.java b/gcloud-java-translate/src/main/java/com/google/cloud/translate/Detection.java new file mode 100644 index 000000000000..8b924e5e9173 --- /dev/null +++ b/gcloud-java-translate/src/main/java/com/google/cloud/translate/Detection.java @@ -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. + * + * Detecting Language + * + */ +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 + * Supported Languages + */ + 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()); + } +} diff --git a/gcloud-java-translate/src/test/java/com/google/cloud/translate/DetectionTest.java b/gcloud-java-translate/src/test/java/com/google/cloud/translate/DetectionTest.java new file mode 100644 index 000000000000..9300fae3e85d --- /dev/null +++ b/gcloud-java-translate/src/test/java/com/google/cloud/translate/DetectionTest.java @@ -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()); + } +}