1f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu/*
2f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * Copyright (C) 2013 The Android Open Source Project
3f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu *
4f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * Licensed under the Apache License, Version 2.0 (the "License");
5f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * you may not use this file except in compliance with the License.
6f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * You may obtain a copy of the License at
7f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu *
8f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu *      http://www.apache.org/licenses/LICENSE-2.0
9f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu *
10f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * Unless required by applicable law or agreed to in writing, software
11f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * distributed under the License is distributed on an "AS IS" BASIS,
12f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * See the License for the specific language governing permissions and
14f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * limitations under the License.
15f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu */
16f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
17f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescupackage com.android.gallery3d.ingest.data;
18f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
19f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescuimport android.annotation.TargetApi;
20f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescuimport android.os.Build;
21f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
22f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescuimport java.text.DateFormat;
23f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescuimport java.util.Calendar;
24f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
25f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu/**
26f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * Represents a date (year, month, day)
27f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu */
28f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)
29f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescupublic class SimpleDate implements Comparable<SimpleDate> {
30f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public int month; // MM
31f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public int day; // DD
32f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public int year; // YYYY
33f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private long timestamp;
34f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private String mCachedStringRepresentation;
35f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
36f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public SimpleDate() {
37f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
38f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
39f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public SimpleDate(long timestamp) {
40f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    setTimestamp(timestamp);
41f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
42f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
43f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private static Calendar sCalendarInstance = Calendar.getInstance();
44f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
45f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public void setTimestamp(long timestamp) {
46f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    synchronized (sCalendarInstance) {
47f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      // TODO(georgescu): find a more efficient way to convert a timestamp to a date?
48f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      sCalendarInstance.setTimeInMillis(timestamp);
49f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      this.day = sCalendarInstance.get(Calendar.DATE);
50f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      this.month = sCalendarInstance.get(Calendar.MONTH);
51f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      this.year = sCalendarInstance.get(Calendar.YEAR);
52f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      this.timestamp = timestamp;
53f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      mCachedStringRepresentation =
54f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu          DateFormat.getDateInstance(DateFormat.SHORT).format(timestamp);
55f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
56f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
57f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
58f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
59f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public int hashCode() {
60f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    final int prime = 31;
61f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    int result = 1;
62f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    result = prime * result + day;
63f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    result = prime * result + month;
64f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    result = prime * result + year;
65f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    return result;
66f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
67f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
68f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
69f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public boolean equals(Object obj) {
70f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (this == obj) {
71f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      return true;
72f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
73f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (obj == null) {
74f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      return false;
75f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
76f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (!(obj instanceof SimpleDate)) {
77f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      return false;
78f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
79f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    SimpleDate other = (SimpleDate) obj;
80f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (year != other.year) {
81f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      return false;
82f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
83f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (month != other.month) {
84f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      return false;
85f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
86f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (day != other.day) {
87f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      return false;
88f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
89f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    return true;
90f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
91f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
92f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
93f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public int compareTo(SimpleDate other) {
94f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    int yearDiff = this.year - other.getYear();
95f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (yearDiff != 0) {
96f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      return yearDiff;
97f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    } else {
98f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      int monthDiff = this.month - other.getMonth();
99f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      if (monthDiff != 0) {
100f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu        return monthDiff;
101f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      } else {
102f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu        return this.day - other.getDay();
103f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      }
104f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
105f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
106f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
107f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public int getDay() {
108f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    return day;
109f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
110f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
111f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public int getMonth() {
112f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    return month;
113f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
114f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
115f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public int getYear() {
116f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    return year;
117f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
118f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
119f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
120f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public String toString() {
121f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (mCachedStringRepresentation == null) {
122f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      mCachedStringRepresentation =
123f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu          DateFormat.getDateInstance(DateFormat.SHORT).format(timestamp);
124f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    }
125f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    return mCachedStringRepresentation;
126f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
127f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu}
128