Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 1 | /* |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 2 | * Copyright (C) 2018 The Android Open Source Project |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 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.android.textclassifier.ulp.database; |
| 18 | |
| 19 | import androidx.annotation.IntDef; |
| 20 | import androidx.annotation.NonNull; |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 21 | import androidx.room.ColumnInfo; |
| 22 | import androidx.room.Entity; |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 23 | import com.google.common.base.Preconditions; |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 24 | import java.lang.annotation.Retention; |
| 25 | import 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 Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 34 | tableName = "language_signal_infos", |
| 35 | primaryKeys = {"languageTag", "source"}) |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 36 | public final class LanguageSignalInfo { |
| 37 | |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 38 | /** The source of the signal */ |
| 39 | @Retention(RetentionPolicy.SOURCE) |
| 40 | @IntDef({SUGGEST_CONVERSATION_ACTIONS, CLASSIFY_TEXT}) |
| 41 | public @interface Source {} |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 42 | |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 43 | public static final int SUGGEST_CONVERSATION_ACTIONS = 0; |
| 44 | public static final int CLASSIFY_TEXT = 1; |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 45 | |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 46 | @NonNull |
| 47 | @ColumnInfo(name = "languageTag") |
| 48 | private final String languageTag; |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 49 | |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 50 | @ColumnInfo(name = "source") |
| 51 | private final int source; |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 52 | |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 53 | @ColumnInfo(name = "count") |
| 54 | private final int count; |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 55 | |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 56 | 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 Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 84 | return languageTag + "_" + src + ": " + count; |
| 85 | } |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 86 | |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 87 | @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 Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 94 | |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 95 | @Override |
| 96 | public boolean equals(Object obj) { |
| 97 | if (this == obj) { |
| 98 | return true; |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 99 | } |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 100 | if (obj == null || obj.getClass() != getClass()) { |
| 101 | return false; |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 102 | } |
Tony Mak | 8cd7ba6 | 2019-10-15 15:29:22 +0100 | [diff] [blame^] | 103 | LanguageSignalInfo info = (LanguageSignalInfo) obj; |
| 104 | return languageTag.equals(info.getLanguageTag()) |
| 105 | && source == info.getSource() |
| 106 | && count == info.getCount(); |
| 107 | } |
Tony Mak | 608b1ae | 2019-08-13 20:02:00 +0100 | [diff] [blame] | 108 | } |