| 1 | |
package org.webslinger.util; |
| 2 | |
|
| 3 | |
import java.text.SimpleDateFormat; |
| 4 | |
import java.util.Calendar; |
| 5 | |
import java.util.Locale; |
| 6 | |
import java.util.TimeZone; |
| 7 | |
|
| 8 | |
|
| 9 | |
public final class RolloverLogic { |
| 10 | |
protected final Calendar cal; |
| 11 | |
protected final SimpleDateFormat formatter; |
| 12 | |
protected final Period period; |
| 13 | |
|
| 14 | |
protected long rolloverTime; |
| 15 | |
|
| 16 | 0 | protected static enum Period { |
| 17 | 0 | YEAR(Calendar.YEAR), |
| 18 | 0 | MONTH(Calendar.MONTH), |
| 19 | 0 | DAY(Calendar.DAY_OF_MONTH), |
| 20 | 0 | AM_PM(Calendar.AM_PM), |
| 21 | 0 | HOUR(Calendar.HOUR_OF_DAY), |
| 22 | 0 | MINUTE(Calendar.MINUTE), |
| 23 | 0 | SECOND(Calendar.SECOND); |
| 24 | |
|
| 25 | |
private final int field; |
| 26 | 0 | Period(int field) { |
| 27 | 0 | this.field = field; |
| 28 | 0 | } |
| 29 | |
|
| 30 | |
public int field() { |
| 31 | 0 | return field; |
| 32 | |
} |
| 33 | |
}; |
| 34 | |
|
| 35 | |
public RolloverLogic(String pattern) { |
| 36 | 0 | this(pattern, System.currentTimeMillis(), Locale.getDefault(), TimeZone.getDefault()); |
| 37 | 0 | } |
| 38 | |
|
| 39 | |
public RolloverLogic(String pattern, Locale locale, TimeZone timeZone) { |
| 40 | 0 | this(pattern, System.currentTimeMillis(), locale, timeZone); |
| 41 | 0 | } |
| 42 | |
|
| 43 | |
public RolloverLogic(String pattern, long currentTime) { |
| 44 | 0 | this(pattern, currentTime, Locale.getDefault(), TimeZone.getDefault()); |
| 45 | 0 | } |
| 46 | |
|
| 47 | 0 | public RolloverLogic(String pattern, long currentTime, Locale locale, TimeZone timeZone) { |
| 48 | 0 | cal = Calendar.getInstance(timeZone, locale); |
| 49 | 0 | formatter = new SimpleDateFormat(pattern); |
| 50 | 0 | cal.setTimeInMillis(0); |
| 51 | 0 | Period period = null; |
| 52 | 0 | String s = formatter.format(cal.getTime()); |
| 53 | 0 | for (Period periodCheck: Period.values()) { |
| 54 | 0 | cal.add(periodCheck.field(), 1); |
| 55 | 0 | if (!formatter.format(cal.getTime()).equals(s)) period = periodCheck; |
| 56 | 0 | cal.add(periodCheck.field(), -1); |
| 57 | |
} |
| 58 | 0 | if (period == null) throw new IllegalArgumentException("Couldn't detect check frequency"); |
| 59 | 0 | this.period = period; |
| 60 | 0 | cal.setTimeInMillis(currentTime); |
| 61 | 0 | cal.add(period.field(), 1); |
| 62 | 0 | for (Period p: Period.values()) { |
| 63 | 0 | cal.set(p.field(), 0); |
| 64 | |
} |
| 65 | 0 | rolloverTime = cal.getTimeInMillis(); |
| 66 | 0 | } |
| 67 | |
|
| 68 | |
public final String getPattern() { |
| 69 | 0 | return formatter.toPattern(); |
| 70 | |
} |
| 71 | |
|
| 72 | |
public final long getRolloverTime() { |
| 73 | 0 | return rolloverTime; |
| 74 | |
} |
| 75 | |
|
| 76 | |
public final boolean checkRollover() { |
| 77 | 0 | long millis = System.currentTimeMillis(); |
| 78 | 0 | if (millis < rolloverTime) return false; |
| 79 | |
do { |
| 80 | 0 | cal.add(period.field(), 1); |
| 81 | 0 | } while ((rolloverTime = cal.getTimeInMillis()) < millis); |
| 82 | 0 | return true; |
| 83 | |
} |
| 84 | |
|
| 85 | |
public final String getCurrentFormat() { |
| 86 | 0 | return formatter.format(cal.getTime()); |
| 87 | |
} |
| 88 | |
} |