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