Upstream version 10.38.222.0
[platform/framework/web/crosswalk.git] / src / third_party / cacheinvalidation / src / java / com / google / ipc / invalidation / external / client / android / service / ParcelableErrorInfo.java
1 /*
2  * Copyright 2011 Google Inc.
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 com.google.ipc.invalidation.external.client.android.service;
18
19 import com.google.ipc.invalidation.external.client.types.ErrorInfo;
20
21 import android.os.Parcel;
22 import android.os.Parcelable;
23
24 /**
25  * Wraps an {link ErrorInfo} to enable writing to and reading from a
26  * {@link Parcel}.
27  */
28 class ParcelableErrorInfo implements Parcelable {
29   final ErrorInfo errorInfo;
30
31   /**
32    * Creator required by the Parcelable implementation conventions.
33    */
34   public static final Parcelable.Creator<ParcelableErrorInfo> CREATOR =
35       new Parcelable.Creator<ParcelableErrorInfo>() {
36         @Override
37         public ParcelableErrorInfo createFromParcel(Parcel in) {
38           return new ParcelableErrorInfo(in);
39         }
40
41         @Override
42         public ParcelableErrorInfo[] newArray(int size) {
43           return new ParcelableErrorInfo[size];
44         }
45       };
46
47   /**
48    * Creates a new wrapper around the provided error info.
49    */
50   ParcelableErrorInfo(ErrorInfo errorInfo) {
51     this.errorInfo = errorInfo;
52   }
53
54   /**
55    * Creates a new ErrorInfo wrapper by reading data from a parcel.
56    */
57   public ParcelableErrorInfo(Parcel in) {
58     int reason = in.readInt();
59     boolean isTransient = in.createBooleanArray()[0];
60     String message = in.readString();
61     this.errorInfo = ErrorInfo.newInstance(reason, isTransient, message, null);
62   }
63
64   @Override
65   public int describeContents() {
66     return 0;  // no special contents
67   }
68
69   @Override
70   public void writeToParcel(Parcel parcel, int flags) {
71
72     // Data written to parcel is:
73     // 1. int errorReason
74     // 2. boolean [] { isTransient }
75     // 3. String error message
76     // TODO: Add support for object marshaling when needed
77     parcel.writeInt(errorInfo.getErrorReason());
78     parcel.writeBooleanArray(new boolean[]{errorInfo.isTransient()});
79     parcel.writeString(errorInfo.getErrorMessage());
80   }
81
82   @Override
83   public boolean equals(Object object) {
84     return object instanceof ParcelableErrorInfo
85         && errorInfo.equals(((ParcelableErrorInfo) object).errorInfo);
86   }
87
88   @Override
89   public int hashCode() {
90     return errorInfo.hashCode();
91   }
92 }