Jaikumar Ganesh | 82c3ef7 | 2009-09-15 16:27:38 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 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 | |
Nick Pelly | aef439e | 2009-09-28 12:33:17 -0700 | [diff] [blame] | 17 | package android.os; |
Jaikumar Ganesh | 82c3ef7 | 2009-09-15 16:27:38 -0700 | [diff] [blame] | 18 | |
Artur Satayev | afdb23a | 2019-12-10 17:47:53 +0000 | [diff] [blame^] | 19 | import android.compat.annotation.UnsupportedAppUsage; |
David Brazdil | 576da05 | 2019-01-28 12:43:28 +0000 | [diff] [blame] | 20 | |
Jaikumar Ganesh | 82c3ef7 | 2009-09-15 16:27:38 -0700 | [diff] [blame] | 21 | import java.util.UUID; |
| 22 | |
| 23 | /** |
| 24 | * This class is a Parcelable wrapper around {@link UUID} which is an |
| 25 | * immutable representation of a 128-bit universally unique |
| 26 | * identifier. |
| 27 | */ |
| 28 | public final class ParcelUuid implements Parcelable { |
| 29 | |
| 30 | private final UUID mUuid; |
| 31 | |
| 32 | /** |
| 33 | * Constructor creates a ParcelUuid instance from the |
| 34 | * given {@link UUID}. |
| 35 | * |
| 36 | * @param uuid UUID |
| 37 | */ |
| 38 | public ParcelUuid(UUID uuid) { |
| 39 | mUuid = uuid; |
| 40 | } |
| 41 | |
| 42 | /** |
| 43 | * Creates a new ParcelUuid from a string representation of {@link UUID}. |
| 44 | * |
| 45 | * @param uuid |
| 46 | * the UUID string to parse. |
Ken Wakasa | f76a50c | 2012-03-09 19:56:35 +0900 | [diff] [blame] | 47 | * @return a ParcelUuid instance. |
Jaikumar Ganesh | 82c3ef7 | 2009-09-15 16:27:38 -0700 | [diff] [blame] | 48 | * @throws NullPointerException |
| 49 | * if {@code uuid} is {@code null}. |
| 50 | * @throws IllegalArgumentException |
| 51 | * if {@code uuid} is not formatted correctly. |
| 52 | */ |
| 53 | public static ParcelUuid fromString(String uuid) { |
| 54 | return new ParcelUuid(UUID.fromString(uuid)); |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Get the {@link UUID} represented by the ParcelUuid. |
| 59 | * |
| 60 | * @return UUID contained in the ParcelUuid. |
| 61 | */ |
| 62 | public UUID getUuid() { |
| 63 | return mUuid; |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Returns a string representation of the ParcelUuid |
| 68 | * For example: 0000110B-0000-1000-8000-00805F9B34FB will be the return value. |
| 69 | * |
| 70 | * @return a String instance. |
| 71 | */ |
| 72 | @Override |
| 73 | public String toString() { |
| 74 | return mUuid.toString(); |
| 75 | } |
| 76 | |
| 77 | |
| 78 | @Override |
| 79 | public int hashCode() { |
| 80 | return mUuid.hashCode(); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Compares this ParcelUuid to another object for equality. If {@code object} |
| 85 | * is not {@code null}, is a ParcelUuid instance, and all bits are equal, then |
| 86 | * {@code true} is returned. |
| 87 | * |
| 88 | * @param object |
| 89 | * the {@code Object} to compare to. |
| 90 | * @return {@code true} if this ParcelUuid is equal to {@code object} |
| 91 | * or {@code false} if not. |
| 92 | */ |
| 93 | @Override |
| 94 | public boolean equals(Object object) { |
| 95 | if (object == null) { |
| 96 | return false; |
| 97 | } |
| 98 | |
| 99 | if (this == object) { |
| 100 | return true; |
| 101 | } |
| 102 | |
| 103 | if (!(object instanceof ParcelUuid)) { |
| 104 | return false; |
| 105 | } |
| 106 | |
| 107 | ParcelUuid that = (ParcelUuid) object; |
| 108 | |
| 109 | return (this.mUuid.equals(that.mUuid)); |
| 110 | } |
| 111 | |
Jeff Sharkey | 9e8f83d | 2019-02-28 12:06:45 -0700 | [diff] [blame] | 112 | public static final @android.annotation.NonNull Parcelable.Creator<ParcelUuid> CREATOR = |
Jaikumar Ganesh | 82c3ef7 | 2009-09-15 16:27:38 -0700 | [diff] [blame] | 113 | new Parcelable.Creator<ParcelUuid>() { |
David Brazdil | 576da05 | 2019-01-28 12:43:28 +0000 | [diff] [blame] | 114 | @UnsupportedAppUsage |
Jaikumar Ganesh | 82c3ef7 | 2009-09-15 16:27:38 -0700 | [diff] [blame] | 115 | public ParcelUuid createFromParcel(Parcel source) { |
| 116 | long mostSigBits = source.readLong(); |
| 117 | long leastSigBits = source.readLong(); |
| 118 | UUID uuid = new UUID(mostSigBits, leastSigBits); |
| 119 | return new ParcelUuid(uuid); |
| 120 | } |
| 121 | |
| 122 | public ParcelUuid[] newArray(int size) { |
| 123 | return new ParcelUuid[size]; |
| 124 | } |
| 125 | }; |
| 126 | |
| 127 | public int describeContents() { |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | public void writeToParcel(Parcel dest, int flags) { |
| 132 | dest.writeLong(mUuid.getMostSignificantBits()); |
| 133 | dest.writeLong(mUuid.getLeastSignificantBits()); |
| 134 | } |
| 135 | } |