|
| 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 | +} |
0 commit comments