1 /* 2 * Copyright (C) 2022 The Android Open Source Project 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.adservices.data.topics; 18 19 import android.annotation.NonNull; 20 import android.app.adservices.topics.TopicParcel; 21 22 import com.android.internal.annotations.Immutable; 23 24 import com.google.auto.value.AutoValue; 25 26 import java.util.Objects; 27 28 /** 29 * POJO Represents a Topic. 30 * 31 * @hide 32 */ 33 @Immutable 34 @AutoValue 35 public abstract class Topic { 36 37 private static final int DEFAULT_LOGGED_TOPIC = -1; 38 39 /** 40 * @return an Integer represents the topic details 41 */ getTopic()42 public abstract int getTopic(); 43 44 /** 45 * @return the taxonomy version number 46 */ getTaxonomyVersion()47 public abstract long getTaxonomyVersion(); 48 49 /** 50 * @return the model version number 51 */ getModelVersion()52 public abstract long getModelVersion(); 53 54 /** 55 * @return an Integer represents the logged topic 56 */ getLoggedTopic()57 public abstract int getLoggedTopic(); 58 59 /** 60 * @return generic builder 61 */ 62 @NonNull builder()63 public static Builder builder() { 64 return new AutoValue_Topic.Builder(); 65 } 66 67 /** 68 * Creates an instance of Topic 69 * 70 * @param topic topic details 71 * @param taxonomyVersion taxonomy version number 72 * @param modelVersion model version number 73 * @return an instance of Topic 74 */ 75 @NonNull create( int topic, long taxonomyVersion, long modelVersion)76 public static Topic create( 77 int topic, 78 long taxonomyVersion, 79 long modelVersion) { 80 return builder().setTopic(topic) 81 .setTaxonomyVersion(taxonomyVersion) 82 .setModelVersion(modelVersion) 83 .setLoggedTopic(DEFAULT_LOGGED_TOPIC).build(); 84 } 85 86 /** 87 * Create an instance of Topic with logged topic. 88 * 89 * @param topic topic details 90 * @param taxonomyVersion taxonomy version number 91 * @param modelVersion model version number 92 * @param loggedTopic logged topic details 93 * @return an instance of Topic 94 */ 95 // TODO(b/292013667): Clean the different create method in Topic 96 @NonNull create( int topic, long taxonomyVersion, long modelVersion, int loggedTopic)97 public static Topic create( 98 int topic, 99 long taxonomyVersion, 100 long modelVersion, 101 int loggedTopic) { 102 return builder().setTopic(topic) 103 .setTaxonomyVersion(taxonomyVersion) 104 .setModelVersion(modelVersion) 105 .setLoggedTopic(loggedTopic).build(); 106 } 107 108 /** 109 * Builder for {@link Topic} 110 */ 111 @AutoValue.Builder 112 public abstract static class Builder { 113 /** Set Topic */ setTopic(int topic)114 public abstract Builder setTopic(int topic); 115 116 /** Set Taxonomy Version */ setTaxonomyVersion(long taxonomyVersion)117 public abstract Builder setTaxonomyVersion(long taxonomyVersion); 118 119 /** Set Model Version */ setModelVersion(long modelVersion)120 public abstract Builder setModelVersion(long modelVersion); 121 122 /** Set Logged Topic */ setLoggedTopic(int loggedTopic)123 public abstract Builder setLoggedTopic(int loggedTopic); 124 125 /** Build a Topic instance */ 126 @NonNull build()127 public abstract Topic build(); 128 } 129 130 /** 131 * Return the parcel for this topic. 132 * 133 * @hide 134 */ 135 @NonNull convertTopicToTopicParcel()136 public TopicParcel convertTopicToTopicParcel() { 137 return new TopicParcel.Builder() 138 .setTopicId(getTopic()) 139 .setTaxonomyVersion(getTaxonomyVersion()) 140 .setModelVersion(getModelVersion()) 141 .build(); 142 } 143 144 /** 145 * The definition of two topics are equal only based on 146 * their topic, taxonomyVersion and modelVersion. 147 */ 148 @Override equals(Object object)149 public boolean equals(Object object) { 150 // If the object is compared with itself then return true 151 if (object == this) { 152 return true; 153 } 154 155 // Check if object is an instance of Topic 156 if (!(object instanceof Topic)) { 157 return false; 158 } 159 160 // typecast object to Topic so that we can compare data members 161 Topic topic = (Topic) object; 162 163 // Compare the data members and return accordingly 164 return getTopic() == topic.getTopic() 165 && getModelVersion() == topic.getModelVersion() 166 && getTaxonomyVersion() == topic.getTaxonomyVersion(); 167 } 168 169 @Override hashCode()170 public int hashCode() { 171 return Objects.hash(getTopic(), getTaxonomyVersion(), getModelVersion()); 172 } 173 } 174