TextClassificationSessionId.java revision 88be5a6cee59868eaee6f7b52fd8b2e6f6f28429
1/*
2 * Copyright (C) 2013 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
17package android.view.textclassifier;
18
19import android.annotation.NonNull;
20import android.os.Parcel;
21import android.os.Parcelable;
22
23import com.android.internal.util.Preconditions;
24
25import java.util.UUID;
26
27/**
28 * This class represents the id of a text classification session.
29 */
30public final class TextClassificationSessionId implements Parcelable {
31    private final @NonNull String mValue;
32
33    /**
34     * Creates a new instance.
35     *
36     * @hide
37     */
38    public TextClassificationSessionId() {
39        this(UUID.randomUUID().toString());
40    }
41
42    /**
43     * Creates a new instance.
44     *
45     * @param value The internal value.
46     *
47     * @hide
48     */
49    public TextClassificationSessionId(@NonNull String value) {
50        mValue = value;
51    }
52
53    @Override
54    public int hashCode() {
55        final int prime = 31;
56        int result = 1;
57        result = prime * result + mValue.hashCode();
58        return result;
59    }
60
61    @Override
62    public boolean equals(Object obj) {
63        if (this == obj) {
64            return true;
65        }
66        if (obj == null) {
67            return false;
68        }
69        if (getClass() != obj.getClass()) {
70            return false;
71        }
72        TextClassificationSessionId other = (TextClassificationSessionId) obj;
73        if (!mValue.equals(other.mValue)) {
74            return false;
75        }
76        return true;
77    }
78
79    @Override
80    public void writeToParcel(Parcel parcel, int flags) {
81        parcel.writeString(mValue);
82    }
83
84    @Override
85    public int describeContents() {
86        return 0;
87    }
88
89    /**
90     * Flattens this id to a string.
91     *
92     * @return The flattened id.
93     *
94     * @hide
95     */
96    public @NonNull String flattenToString() {
97        return mValue;
98    }
99
100    /**
101     * Unflattens a print job id from a string.
102     *
103     * @param string The string.
104     * @return The unflattened id, or null if the string is malformed.
105     *
106     * @hide
107     */
108    public static @NonNull TextClassificationSessionId unflattenFromString(@NonNull String string) {
109        return new TextClassificationSessionId(string);
110    }
111
112    public static final Parcelable.Creator<TextClassificationSessionId> CREATOR =
113            new Parcelable.Creator<TextClassificationSessionId>() {
114                @Override
115                public TextClassificationSessionId createFromParcel(Parcel parcel) {
116                    return new TextClassificationSessionId(
117                            Preconditions.checkNotNull(parcel.readString()));
118                }
119
120                @Override
121                public TextClassificationSessionId[] newArray(int size) {
122                    return new TextClassificationSessionId[size];
123                }
124            };
125}
126