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