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.os.Parcel; 21 import android.os.Parcelable; 22 23 import com.android.internal.util.Preconditions; 24 25 import java.util.UUID; 26 27 /** 28 * This class represents the id of a print job. 29 */ 30 public final class PrintJobId implements Parcelable { 31 private final @NonNull String mValue; 32 33 /** 34 * Creates a new instance. 35 * 36 * @hide 37 */ PrintJobId()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 */ PrintJobId(@onNull String value)49 public PrintJobId(@NonNull String value) { 50 mValue = value; 51 } 52 53 @Override hashCode()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 equals(Object obj)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 writeToParcel(Parcel parcel, int flags)80 public void writeToParcel(Parcel parcel, int flags) { 81 parcel.writeString(mValue); 82 } 83 84 @Override describeContents()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 */ flattenToString()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 */ unflattenFromString(@onNull String string)108 public static @NonNull PrintJobId unflattenFromString(@NonNull String string) { 109 return new PrintJobId(string); 110 } 111 112 public static final @android.annotation.NonNull 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