1818
1919import android .os .Parcel ;
2020import android .os .Parcelable ;
21+ import android .os .SystemClock ;
2122
2223import java .util .Locale ;
2324
@@ -58,8 +59,14 @@ public class Country implements Parcelable {
5859 private final int mSource ;
5960
6061 private int mHashCode ;
62+
63+ /**
64+ * Time that this object was created (which we assume to be the time that the source was
65+ * consulted). This time is in milliseconds since boot up.
66+ */
67+ private final long mTimestamp ;
68+
6169 /**
62- *
6370 * @param countryIso the ISO 3166-1 two letters country code.
6471 * @param source where the countryIso came from, could be one of below
6572 * values
@@ -78,11 +85,23 @@ public Country(final String countryIso, final int source) {
7885 }
7986 mCountryIso = countryIso .toUpperCase (Locale .US );
8087 mSource = source ;
88+ mTimestamp = SystemClock .elapsedRealtime ();
89+ }
90+
91+ private Country (final String countryIso , final int source , long timestamp ) {
92+ if (countryIso == null || source < COUNTRY_SOURCE_NETWORK
93+ || source > COUNTRY_SOURCE_LOCALE ) {
94+ throw new IllegalArgumentException ();
95+ }
96+ mCountryIso = countryIso .toUpperCase (Locale .US );
97+ mSource = source ;
98+ mTimestamp = timestamp ;
8199 }
82100
83101 public Country (Country country ) {
84102 mCountryIso = country .mCountryIso ;
85103 mSource = country .mSource ;
104+ mTimestamp = country .mTimestamp ;
86105 }
87106
88107 /**
@@ -106,9 +125,17 @@ public final int getSource() {
106125 return mSource ;
107126 }
108127
128+ /**
129+ * Returns the time that this object was created (which we assume to be the time that the source
130+ * was consulted).
131+ */
132+ public final long getTimestamp () {
133+ return mTimestamp ;
134+ }
135+
109136 public static final Parcelable .Creator <Country > CREATOR = new Parcelable .Creator <Country >() {
110137 public Country createFromParcel (Parcel in ) {
111- return new Country (in .readString (), in .readInt ());
138+ return new Country (in .readString (), in .readInt (), in . readLong () );
112139 }
113140
114141 public Country [] newArray (int size ) {
@@ -123,15 +150,22 @@ public int describeContents() {
123150 public void writeToParcel (Parcel parcel , int flags ) {
124151 parcel .writeString (mCountryIso );
125152 parcel .writeInt (mSource );
153+ parcel .writeLong (mTimestamp );
126154 }
127155
156+ /**
157+ * Returns true if this {@link Country} is equivalent to the given object. This ignores
158+ * the timestamp value and just checks for equivalence of countryIso and source values.
159+ * Returns false otherwise.
160+ */
128161 @ Override
129162 public boolean equals (Object object ) {
130163 if (object == this ) {
131164 return true ;
132165 }
133166 if (object instanceof Country ) {
134167 Country c = (Country ) object ;
168+ // No need to check the equivalence of the timestamp
135169 return mCountryIso .equals (c .getCountryIso ()) && mSource == c .getSource ();
136170 }
137171 return false ;
@@ -150,8 +184,8 @@ public int hashCode() {
150184 }
151185
152186 /**
153- * Compare the specified country to this country object ignoring the mSource
154- * field , return true if the countryIso fields are equal
187+ * Compare the specified country to this country object ignoring the source
188+ * and timestamp fields , return true if the countryIso fields are equal
155189 *
156190 * @param country the country to compare
157191 * @return true if the specified country's countryIso field is equal to this
@@ -160,4 +194,9 @@ public int hashCode() {
160194 public boolean equalsIgnoreSource (Country country ) {
161195 return country != null && mCountryIso .equals (country .getCountryIso ());
162196 }
197+
198+ @ Override
199+ public String toString () {
200+ return "Country {ISO=" + mCountryIso + ", source=" + mSource + ", time=" + mTimestamp + "}" ;
201+ }
163202}
0 commit comments