PrinterId.java revision c43639c3067dda5df189fb3cbf14f256c17e677d
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
17package android.print;
18
19import android.annotation.NonNull;
20import android.content.ComponentName;
21import android.os.Parcel;
22import android.os.Parcelable;
23import android.text.TextUtils;
24
25/**
26 * This class represents the unique id of a printer.
27 */
28public final class PrinterId implements Parcelable {
29
30    private final ComponentName mServiceName;
31
32    private final String mLocalId;
33
34    /**
35     * Creates a new instance.
36     *
37     * @param serviceName The managing print service.
38     * @param localId The locally unique id within the managing service.
39     *
40     * @hide
41     */
42    public PrinterId(ComponentName serviceName, String localId) {
43        mServiceName = serviceName;
44        mLocalId = localId;
45    }
46
47    private PrinterId(Parcel parcel) {
48        mServiceName = parcel.readParcelable(null);
49        mLocalId = parcel.readString();
50    }
51
52    /**
53     * The id of the print service this printer is managed by.
54     *
55     * @return The print service component name.
56     *
57     * @hide
58     */
59    public ComponentName getServiceName() {
60        return mServiceName;
61    }
62
63    /**
64     * Gets the id of this printer which is unique in the context
65     * of the print service that manages it.
66     *
67     * @return The printer name.
68     */
69    public @NonNull String getLocalId() {
70        return mLocalId;
71    }
72
73    @Override
74    public int describeContents() {
75        return 0;
76    }
77
78    @Override
79    public void writeToParcel(Parcel parcel, int flags) {
80        parcel.writeParcelable(mServiceName, flags);
81        parcel.writeString(mLocalId);
82    }
83
84    @Override
85    public boolean equals(Object object) {
86        if (this == object) {
87            return true;
88        }
89        if (object == null) {
90            return false;
91        }
92        if (getClass() != object.getClass()) {
93            return false;
94        }
95        PrinterId other = (PrinterId) object;
96        if (mServiceName == null) {
97            if (other.mServiceName != null) {
98                return false;
99            }
100        } else if (!mServiceName.equals(other.mServiceName)) {
101            return false;
102        }
103        if (!TextUtils.equals(mLocalId, other.mLocalId)) {
104            return false;
105        }
106        return true;
107    }
108
109    @Override
110    public int hashCode() {
111        final int prime = 31;
112        int hashCode = 1;
113        hashCode = prime * hashCode + ((mServiceName != null)
114                ? mServiceName.hashCode() : 1);
115        hashCode = prime * hashCode + mLocalId.hashCode();
116        return hashCode;
117    }
118
119    @Override
120    public String toString() {
121        StringBuilder builder = new StringBuilder();
122        builder.append("PrinterId{");
123        builder.append("serviceName=").append(mServiceName.flattenToString());
124        builder.append(", localId=").append(mLocalId);
125        builder.append('}');
126        return builder.toString();
127    }
128
129    public static final Parcelable.Creator<PrinterId> CREATOR =
130            new Creator<PrinterId>() {
131        @Override
132        public PrinterId createFromParcel(Parcel parcel) {
133            return new PrinterId(parcel);
134        }
135
136        @Override
137        public PrinterId[] newArray(int size) {
138            return new PrinterId[size];
139        }
140    };
141}
142