blob: cc50c8996ba7437bc146dc2b83dda107b9ae493d [file] [log] [blame]
Jaikumar Ganesh82c3ef72009-09-15 16:27:38 -07001/*
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 Pellyaef439e2009-09-28 12:33:17 -070017package android.os;
Jaikumar Ganesh82c3ef72009-09-15 16:27:38 -070018
David Brazdil576da052019-01-28 12:43:28 +000019import android.annotation.UnsupportedAppUsage;
20
Jaikumar Ganesh82c3ef72009-09-15 16:27:38 -070021import 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 */
28public 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 Wakasaf76a50c2012-03-09 19:56:35 +090047 * @return a ParcelUuid instance.
Jaikumar Ganesh82c3ef72009-09-15 16:27:38 -070048 * @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 Sharkey9e8f83d2019-02-28 12:06:45 -0700112 public static final @android.annotation.NonNull Parcelable.Creator<ParcelUuid> CREATOR =
Jaikumar Ganesh82c3ef72009-09-15 16:27:38 -0700113 new Parcelable.Creator<ParcelUuid>() {
David Brazdil576da052019-01-28 12:43:28 +0000114 @UnsupportedAppUsage
Jaikumar Ganesh82c3ef72009-09-15 16:27:38 -0700115 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}