blob: c67ff7caaf6434d9f7ea925e2f5c18404a543ae5 [file] [log] [blame]
Felipe Leme044c63b2019-02-12 14:35:20 -08001/*
2 * Copyright (C) 2019 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 */
16package android.content;
17
18import android.annotation.NonNull;
Felipe Leme044c63b2019-02-12 14:35:20 -080019import android.os.Parcel;
20import android.os.Parcelable;
21
22import com.android.internal.util.Preconditions;
23
24import java.io.PrintWriter;
25
26/**
27 * Identifier for an unique state in the application.
28 *
29 * <p>Should be stable across reboots and backup / restore.
30 *
31 * <p>For example, a chat app could use the context to resume a conversation between 2 users.
32 */
33// TODO(b/123577059): make sure this is well documented and understandable
34public final class LocusId implements Parcelable {
35
Felipe Leme7e735752019-03-01 16:27:24 -080036 private final String mId;
Felipe Leme044c63b2019-02-12 14:35:20 -080037
38 /**
39 * Default constructor.
Felipe Leme85e2b012019-03-05 09:41:03 -080040 *
41 * @throws IllegalArgumentException if {@code id} is empty or {@code null}.
Felipe Leme044c63b2019-02-12 14:35:20 -080042 */
Felipe Leme7e735752019-03-01 16:27:24 -080043 public LocusId(@NonNull String id) {
Felipe Leme85e2b012019-03-05 09:41:03 -080044 mId = Preconditions.checkStringNotEmpty(id, "id cannot be empty");
Felipe Leme044c63b2019-02-12 14:35:20 -080045 }
46
47 /**
Felipe Leme7e735752019-03-01 16:27:24 -080048 * Gets the {@code id} associated with the locus.
Felipe Leme044c63b2019-02-12 14:35:20 -080049 */
50 @NonNull
Felipe Leme7e735752019-03-01 16:27:24 -080051 public String getId() {
52 return mId;
Felipe Leme044c63b2019-02-12 14:35:20 -080053 }
54
55 @Override
56 public int hashCode() {
57 final int prime = 31;
58 int result = 1;
Felipe Leme7e735752019-03-01 16:27:24 -080059 result = prime * result + ((mId == null) ? 0 : mId.hashCode());
Felipe Leme044c63b2019-02-12 14:35:20 -080060 return result;
61 }
62
63 @Override
64 public boolean equals(Object obj) {
65 if (this == obj) return true;
66 if (obj == null) return false;
67 if (getClass() != obj.getClass()) return false;
68 final LocusId other = (LocusId) obj;
Felipe Leme7e735752019-03-01 16:27:24 -080069 if (mId == null) {
70 if (other.mId != null) return false;
Felipe Leme044c63b2019-02-12 14:35:20 -080071 } else {
Felipe Leme7e735752019-03-01 16:27:24 -080072 if (!mId.equals(other.mId)) return false;
Felipe Leme044c63b2019-02-12 14:35:20 -080073 }
74 return true;
75 }
76
77 @Override
78 public String toString() {
Felipe Leme7e735752019-03-01 16:27:24 -080079 return "LocusId[" + getSanitizedId() + "]";
Felipe Leme044c63b2019-02-12 14:35:20 -080080 }
81
82 /** @hide */
83 public void dump(@NonNull PrintWriter pw) {
Felipe Leme7e735752019-03-01 16:27:24 -080084 pw.print("id:"); pw.println(getSanitizedId());
Felipe Leme044c63b2019-02-12 14:35:20 -080085 }
86
Felipe Leme7e735752019-03-01 16:27:24 -080087 @NonNull
88 private String getSanitizedId() {
89 final int size = mId.length();
Felipe Leme044c63b2019-02-12 14:35:20 -080090 return size + "_chars";
91 }
92
93 @Override
94 public int describeContents() {
95 return 0;
96 }
97
98 @Override
Felipe Leme7e735752019-03-01 16:27:24 -080099 public void writeToParcel(Parcel parcel, int flags) {
100 parcel.writeString(mId);
Felipe Leme044c63b2019-02-12 14:35:20 -0800101 }
102
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700103 public static final @android.annotation.NonNull Parcelable.Creator<LocusId> CREATOR =
Felipe Leme044c63b2019-02-12 14:35:20 -0800104 new Parcelable.Creator<LocusId>() {
105
Felipe Lemece6877b2019-02-28 09:02:26 -0800106 @NonNull
Felipe Leme044c63b2019-02-12 14:35:20 -0800107 @Override
Felipe Leme7e735752019-03-01 16:27:24 -0800108 public LocusId createFromParcel(Parcel parcel) {
109 return new LocusId(parcel.readString());
Felipe Leme044c63b2019-02-12 14:35:20 -0800110 }
111
Felipe Lemece6877b2019-02-28 09:02:26 -0800112 @NonNull
Felipe Leme044c63b2019-02-12 14:35:20 -0800113 @Override
114 public LocusId[] newArray(int size) {
115 return new LocusId[size];
116 }
117 };
118}