blob: 606cbb81906504c7f0cf506748ab923f9551546f [file] [log] [blame]
Svetoslav2fbd2a72013-09-16 17:53:51 -07001/*
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
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080019import android.annotation.NonNull;
Svetoslav2fbd2a72013-09-16 17:53:51 -070020import android.os.Parcel;
21import android.os.Parcelable;
Philip P. Moltmannfdb2b762016-01-14 15:37:11 -080022
23import com.android.internal.util.Preconditions;
Svetoslav2fbd2a72013-09-16 17:53:51 -070024
25import java.util.UUID;
26
27/**
28 * This class represents the id of a print job.
29 */
30public final class PrintJobId implements Parcelable {
Philip P. Moltmannfdb2b762016-01-14 15:37:11 -080031 private final @NonNull String mValue;
Svetoslav2fbd2a72013-09-16 17:53:51 -070032
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 */
Philip P. Moltmannfdb2b762016-01-14 15:37:11 -080049 public PrintJobId(@NonNull String value) {
Svetoslav2fbd2a72013-09-16 17:53:51 -070050 mValue = value;
51 }
52
53 @Override
54 public int hashCode() {
55 final int prime = 31;
56 int result = 1;
Philip P. Moltmannfdb2b762016-01-14 15:37:11 -080057 result = prime * result + mValue.hashCode();
Svetoslav2fbd2a72013-09-16 17:53:51 -070058 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;
Philip P. Moltmannfdb2b762016-01-14 15:37:11 -080073 if (!mValue.equals(other.mValue)) {
Svetoslav2fbd2a72013-09-16 17:53:51 -070074 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 */
Philip P. Moltmannc43639c2015-12-18 13:58:40 -080096 public @NonNull String flattenToString() {
Svetoslav2fbd2a72013-09-16 17:53:51 -070097 return mValue;
98 }
99
100 /**
101 * Unflattens a print job id from a string.
102 *
Philip P. Moltmannc43639c2015-12-18 13:58:40 -0800103 * @param string The string.
Svetoslav2fbd2a72013-09-16 17:53:51 -0700104 * @return The unflattened id, or null if the string is malformed.
105 *
106 * @hide
107 */
Philip P. Moltmannfdb2b762016-01-14 15:37:11 -0800108 public static @NonNull PrintJobId unflattenFromString(@NonNull String string) {
Svetoslav2fbd2a72013-09-16 17:53:51 -0700109 return new PrintJobId(string);
110 }
111
Jeff Sharkey9e8f83d2019-02-28 12:06:45 -0700112 public static final @android.annotation.NonNull Parcelable.Creator<PrintJobId> CREATOR =
Svetoslav2fbd2a72013-09-16 17:53:51 -0700113 new Parcelable.Creator<PrintJobId>() {
114 @Override
115 public PrintJobId createFromParcel(Parcel parcel) {
Philip P. Moltmannfdb2b762016-01-14 15:37:11 -0800116 return new PrintJobId(Preconditions.checkNotNull(parcel.readString()));
Svetoslav2fbd2a72013-09-16 17:53:51 -0700117 }
118
119 @Override
120 public PrintJobId[] newArray(int size) {
121 return new PrintJobId[size];
122 }
123 };
124}