1 /*
2  * Copyright (C) 2020 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.internal.logging;
18 
19 import static java.lang.Math.max;
20 import static java.lang.Math.min;
21 
22 import android.annotation.Nullable;
23 import android.os.Parcel;
24 import android.os.Parcelable;
25 
26 import com.android.internal.annotations.VisibleForTesting;
27 
28 /**
29  * An opaque identifier used to disambiguate which logs refer to a particular instance of some
30  * UI element. Useful when there might be multiple instances simultaneously active.
31  * Obtain from InstanceIdSequence.  Clipped to range [0, INSTANCE_ID_MAX].
32  */
33 public final class InstanceId implements Parcelable {
34     // At most 20 bits: ~1m possibilities, ~0.5% probability of collision in 100 values
35     static final int INSTANCE_ID_MAX = 1 << 20;
36 
37     private final int mId;
InstanceId(int id)38     InstanceId(int id) {
39         mId = min(max(0, id), INSTANCE_ID_MAX);
40     }
41 
InstanceId(Parcel in)42     private InstanceId(Parcel in) {
43         this(in.readInt());
44     }
45 
46     @VisibleForTesting
getId()47     public int getId() {
48         return mId;
49     }
50 
51     /**
52      * Create a fake instance ID for testing purposes.  Not for production use. See also
53      * InstanceIdSequenceFake, which is a testing replacement for InstanceIdSequence.
54      * @param id The ID you want to assign.
55      * @return new InstanceId.
56      */
57     @VisibleForTesting
fakeInstanceId(int id)58     public static InstanceId fakeInstanceId(int id) {
59         return new InstanceId(id);
60     }
61 
62     @Override
hashCode()63     public int hashCode() {
64         return mId;
65     }
66 
67     @Override
equals(@ullable Object obj)68     public boolean equals(@Nullable Object obj) {
69         if (!(obj instanceof InstanceId)) {
70             return false;
71         }
72         return mId == ((InstanceId) obj).mId;
73     }
74 
75     @Override
describeContents()76     public int describeContents() {
77         return 0;
78     }
79 
80     @Override
writeToParcel(Parcel out, int flags)81     public void writeToParcel(Parcel out, int flags) {
82         out.writeInt(mId);
83     }
84 
85     public static final Parcelable.Creator<InstanceId> CREATOR =
86             new Parcelable.Creator<InstanceId>() {
87         @Override
88         public InstanceId createFromParcel(Parcel in) {
89             return new InstanceId(in);
90         }
91 
92         @Override
93         public InstanceId[] newArray(int size) {
94             return new InstanceId[size];
95         }
96     };
97 
98 }
99