blob: 98f63ac2a1b06e7dbbad4b7faf354c419b668767 [file] [log] [blame]
Tony Mak608b1ae2019-08-13 20:02:00 +01001/*
Tony Mak8cd7ba62019-10-15 15:29:22 +01002 * Copyright (C) 2018 The Android Open Source Project
Tony Mak608b1ae2019-08-13 20:02:00 +01003 *
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
17package com.android.textclassifier.ulp.database;
18
19import androidx.annotation.IntDef;
20import androidx.annotation.NonNull;
Tony Mak608b1ae2019-08-13 20:02:00 +010021import androidx.room.ColumnInfo;
22import androidx.room.Entity;
Tony Mak8cd7ba62019-10-15 15:29:22 +010023import com.google.common.base.Preconditions;
Tony Mak608b1ae2019-08-13 20:02:00 +010024import java.lang.annotation.Retention;
25import java.lang.annotation.RetentionPolicy;
26
27/**
28 * Class defining Room database entity.
29 *
30 * <p>Represents information about number of signals received from one of defined source types in
31 * specified language.
32 */
33@Entity(
Tony Mak8cd7ba62019-10-15 15:29:22 +010034 tableName = "language_signal_infos",
35 primaryKeys = {"languageTag", "source"})
Tony Mak608b1ae2019-08-13 20:02:00 +010036public final class LanguageSignalInfo {
37
Tony Mak8cd7ba62019-10-15 15:29:22 +010038 /** The source of the signal */
39 @Retention(RetentionPolicy.SOURCE)
40 @IntDef({SUGGEST_CONVERSATION_ACTIONS, CLASSIFY_TEXT})
41 public @interface Source {}
Tony Mak608b1ae2019-08-13 20:02:00 +010042
Tony Mak8cd7ba62019-10-15 15:29:22 +010043 public static final int SUGGEST_CONVERSATION_ACTIONS = 0;
44 public static final int CLASSIFY_TEXT = 1;
Tony Mak608b1ae2019-08-13 20:02:00 +010045
Tony Mak8cd7ba62019-10-15 15:29:22 +010046 @NonNull
47 @ColumnInfo(name = "languageTag")
48 private final String languageTag;
Tony Mak608b1ae2019-08-13 20:02:00 +010049
Tony Mak8cd7ba62019-10-15 15:29:22 +010050 @ColumnInfo(name = "source")
51 private final int source;
Tony Mak608b1ae2019-08-13 20:02:00 +010052
Tony Mak8cd7ba62019-10-15 15:29:22 +010053 @ColumnInfo(name = "count")
54 private final int count;
Tony Mak608b1ae2019-08-13 20:02:00 +010055
Tony Mak8cd7ba62019-10-15 15:29:22 +010056 public LanguageSignalInfo(String languageTag, @Source int source, int count) {
57 this.languageTag = Preconditions.checkNotNull(languageTag);
58 this.source = source;
59 this.count = count;
60 }
61
62 public String getLanguageTag() {
63 return languageTag;
64 }
65
66 @Source
67 public int getSource() {
68 return source;
69 }
70
71 public int getCount() {
72 return count;
73 }
74
75 @Override
76 public String toString() {
77 String src = "OTHER";
78 if (source == SUGGEST_CONVERSATION_ACTIONS) {
79 src = "SUGGEST_CONVERSATION_ACTIONS";
80 } else if (source == CLASSIFY_TEXT) {
81 src = "CLASSIFY_TEXT";
Tony Mak608b1ae2019-08-13 20:02:00 +010082 }
83
Tony Mak8cd7ba62019-10-15 15:29:22 +010084 return languageTag + "_" + src + ": " + count;
85 }
Tony Mak608b1ae2019-08-13 20:02:00 +010086
Tony Mak8cd7ba62019-10-15 15:29:22 +010087 @Override
88 public int hashCode() {
89 int result = languageTag.hashCode();
90 result = 31 * result + Integer.hashCode(source);
91 result = 31 * result + Integer.hashCode(count);
92 return result;
93 }
Tony Mak608b1ae2019-08-13 20:02:00 +010094
Tony Mak8cd7ba62019-10-15 15:29:22 +010095 @Override
96 public boolean equals(Object obj) {
97 if (this == obj) {
98 return true;
Tony Mak608b1ae2019-08-13 20:02:00 +010099 }
Tony Mak8cd7ba62019-10-15 15:29:22 +0100100 if (obj == null || obj.getClass() != getClass()) {
101 return false;
Tony Mak608b1ae2019-08-13 20:02:00 +0100102 }
Tony Mak8cd7ba62019-10-15 15:29:22 +0100103 LanguageSignalInfo info = (LanguageSignalInfo) obj;
104 return languageTag.equals(info.getLanguageTag())
105 && source == info.getSource()
106 && count == info.getCount();
107 }
Tony Mak608b1ae2019-08-13 20:02:00 +0100108}