1 /*
2  * Copyright (C) 2017 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.app.timezone;
18 
19 import static android.app.timezone.Utils.validateRulesVersion;
20 import static android.app.timezone.Utils.validateVersion;
21 
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 
25 /**
26  * Versioning information about a set of time zone rules.
27  *
28  * <p>The following properties are included:
29  * <dl>
30  *     <dt>rulesVersion</dt>
31  *     <dd>the IANA rules version. e.g. "2017a"</dd>
32  *     <dt>revision</dt>
33  *     <dd>the revision for the rules. Allows there to be several revisions for a given IANA rules
34  *     release. Numerically higher is newer.</dd>
35  * </dl>
36  *
37  * @hide
38  */
39 public final class DistroRulesVersion implements Parcelable {
40 
41     private final String mRulesVersion;
42     private final int mRevision;
43 
DistroRulesVersion(String rulesVersion, int revision)44     public DistroRulesVersion(String rulesVersion, int revision) {
45         mRulesVersion = validateRulesVersion("rulesVersion", rulesVersion);
46         mRevision = validateVersion("revision", revision);
47     }
48 
49     public static final @android.annotation.NonNull Creator<DistroRulesVersion> CREATOR = new Creator<DistroRulesVersion>() {
50         public DistroRulesVersion createFromParcel(Parcel in) {
51             String rulesVersion = in.readString();
52             int revision = in.readInt();
53             return new DistroRulesVersion(rulesVersion, revision);
54         }
55 
56         public DistroRulesVersion[] newArray(int size) {
57             return new DistroRulesVersion[size];
58         }
59     };
60 
getRulesVersion()61     public String getRulesVersion() {
62         return mRulesVersion;
63     }
64 
getRevision()65     public int getRevision() {
66         return mRevision;
67     }
68 
69     /**
70      * Returns true if this DistroRulesVersion is older than the one supplied. It returns false if
71      * it is the same or newer. This method compares the {@code rulesVersion} and the
72      * {@code revision}.
73      */
isOlderThan(DistroRulesVersion distroRulesVersion)74     public boolean isOlderThan(DistroRulesVersion distroRulesVersion) {
75         int rulesComparison = mRulesVersion.compareTo(distroRulesVersion.mRulesVersion);
76         if (rulesComparison < 0) {
77             return true;
78         }
79         if (rulesComparison > 0) {
80             return false;
81         }
82         return mRevision < distroRulesVersion.mRevision;
83     }
84 
85     @Override
describeContents()86     public int describeContents() {
87         return 0;
88     }
89 
90     @Override
writeToParcel(Parcel out, int flags)91     public void writeToParcel(Parcel out, int flags) {
92         out.writeString(mRulesVersion);
93         out.writeInt(mRevision);
94     }
95 
96     @Override
equals(Object o)97     public boolean equals(Object o) {
98         if (this == o) {
99             return true;
100         }
101         if (o == null || getClass() != o.getClass()) {
102             return false;
103         }
104 
105         DistroRulesVersion that = (DistroRulesVersion) o;
106 
107         if (mRevision != that.mRevision) {
108             return false;
109         }
110         return mRulesVersion.equals(that.mRulesVersion);
111     }
112 
113     @Override
hashCode()114     public int hashCode() {
115         int result = mRulesVersion.hashCode();
116         result = 31 * result + mRevision;
117         return result;
118     }
119 
120     @Override
toString()121     public String toString() {
122         return "DistroRulesVersion{"
123                 + "mRulesVersion='" + mRulesVersion + '\''
124                 + ", mRevision='" + mRevision + '\''
125                 + '}';
126     }
127 
toDumpString()128     public String toDumpString() {
129         return mRulesVersion + "," + mRevision;
130     }
131 }
132