PrintJobId.java revision fdb2b7644fc8b0ef968b5c7a2345c17a58287fd9
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.print;
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 print job.
29 */
30public final class PrintJobId implements Parcelable {
31    private final @NonNull String mValue;
32
33    /**
34     * Creates a new instance.
35     *
36     * @hide
37     */
38    public PrintJobId() {
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 PrintJobId(@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        PrintJobId other = (PrintJobId) 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 PrintJobId unflattenFromString(@NonNull String string) {
109        return new PrintJobId(string);
110    }
111
112    public static final Parcelable.Creator<PrintJobId> CREATOR =
113            new Parcelable.Creator<PrintJobId>() {
114        @Override
115        public PrintJobId createFromParcel(Parcel parcel) {
116            return new PrintJobId(Preconditions.checkNotNull(parcel.readString()));
117        }
118
119        @Override
120        public PrintJobId[] newArray(int size) {
121            return new PrintJobId[size];
122        }
123    };
124}
125