| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.types; |
| 34 | | | |
| 35 | | | import java.io.Serializable; |
| 36 | | | import java.util.Calendar; |
| 37 | | | import java.util.GregorianCalendar; |
| 38 | | | import java.util.TimeZone; |
| 39 | | | |
| 40 | | | import org.jcoderz.commons.ArgumentMalformedException; |
| 41 | | | import org.jcoderz.commons.util.Assert; |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | @author |
| 50 | | | |
| 51 | | | public final class YearMonth |
| 52 | | | implements Serializable |
| 53 | | | { |
| 54 | | | |
| 55 | | | public static final String TYPE_NAME = "YearMonth"; |
| 56 | | | |
| 57 | | | |
| 58 | | | public static final int MINIMUM_NUMBER_OF_YEAR_DIGITS = 4; |
| 59 | | | |
| 60 | | | |
| 61 | | | public static final int MONTH_LENGTH = 2; |
| 62 | | | |
| 63 | | | private static final int TWO_DIGIT_MONTH = 10; |
| 64 | | | |
| 65 | | | <code></code> |
| 66 | | | private static final long serialVersionUID = 1L; |
| 67 | | | |
| 68 | | | private final int mYear; |
| 69 | | | |
| 70 | | | private final int mMonth; |
| 71 | | | |
| 72 | | | |
| 73 | | | private transient Date mEndDate; |
| 74 | | | private transient Date mStartDate; |
| 75 | | | private transient int mHashCode; |
| 76 | | | private transient String mString; |
| 77 | | | private transient Period mPeriod; |
| 78 | | | |
| 79 | | | |
| 80 | | | |
| 81 | | | |
| 82 | | | private YearMonth (int year, int month) |
| 83 | 100 | | { |
| 84 | 100 | | if (1 > month || month > Date.MONTH_PER_YEAR) |
| 85 | | | { |
| 86 | 100 | (1) | throw new ArgumentMalformedException(TYPE_NAME, String.valueOf(month), |
| 87 | | | "Month must be between 1 and 12."); |
| 88 | | | } |
| 89 | 100 | | if (year == 0) |
| 90 | | | { |
| 91 | 100 | (2) | throw new ArgumentMalformedException(TYPE_NAME, String.valueOf(year), |
| 92 | | | "Value of Year must not be 0."); |
| 93 | | | } |
| 94 | 100 | | mYear = year; |
| 95 | 100 | | mMonth = month; |
| 96 | 100 | | } |
| 97 | | | |
| 98 | | | |
| 99 | | | |
| 100 | | | <tt></tt> |
| 101 | | | @param |
| 102 | | | @return |
| 103 | | | |
| 104 | | | public static YearMonth fromString (String str) |
| 105 | | | { |
| 106 | 100 | | Assert.notNull(str, TYPE_NAME); |
| 107 | | | |
| 108 | 100 | | final int minusPos = str.indexOf('-', 1); |
| 109 | 100 | | if (minusPos == -1) |
| 110 | | | { |
| 111 | 100 | (3) | throw new ArgumentMalformedException(TYPE_NAME, str, |
| 112 | | | "MonthYear type must contain a '-' character. (CCYY-MM)"); |
| 113 | | | } |
| 114 | 100 | | if ((minusPos + 1) < MINIMUM_NUMBER_OF_YEAR_DIGITS) |
| 115 | | | { |
| 116 | 100 | (4) | throw new ArgumentMalformedException(TYPE_NAME, str, |
| 117 | | | "MonthYear type have at least " |
| 118 | | | + MINIMUM_NUMBER_OF_YEAR_DIGITS + " digits in front of the '-' " |
| 119 | | | + "character. (CCYY-MM)"); |
| 120 | | | } |
| 121 | | | final int year; |
| 122 | | | try |
| 123 | | | { |
| 124 | 100 | | year = Integer.parseInt(str.substring(0, minusPos)); |
| 125 | | | } |
| 126 | 0 | | catch (NumberFormatException ex) |
| 127 | | | { |
| 128 | 0 | (5) | throw new ArgumentMalformedException(TYPE_NAME, str, |
| 129 | | | "Failed to parse year. (CCYY-MM)", ex); |
| 130 | 100 | | } |
| 131 | 100 | | if (str.length() - minusPos <= MONTH_LENGTH) |
| 132 | | | { |
| 133 | 100 | (6) | throw new ArgumentMalformedException(TYPE_NAME, str, |
| 134 | | | "Month must be 2 digits long. (CCYY-MM)"); |
| 135 | | | } |
| 136 | | | final int month; |
| 137 | | | try |
| 138 | | | { |
| 139 | 100 | | month = Integer.parseInt(str.substring(minusPos + 1, |
| 140 | | | minusPos + 1 + MONTH_LENGTH)); |
| 141 | | | } |
| 142 | 100 | | catch (NumberFormatException ex) |
| 143 | | | { |
| 144 | 100 | (7) | throw new ArgumentMalformedException(TYPE_NAME, str, |
| 145 | | | "Failed to parse month. (CCYY-MM)", ex); |
| 146 | 100 | | } |
| 147 | 100 | | if (str.length() > minusPos + 1 + MONTH_LENGTH) |
| 148 | | | { |
| 149 | | | |
| 150 | 100 | | final String tz = str.substring(minusPos + 1 + MONTH_LENGTH); |
| 151 | 100 | | final TimeZone timeZone = TimeZone.getTimeZone(tz); |
| 152 | 100 | | if (timeZone.getRawOffset() != 0) |
| 153 | | | { |
| 154 | 100 | | throw new ArgumentMalformedException(TYPE_NAME, str, |
| 155 | | | "Only UTC is supported as time zone, not '" + tz + "'."); |
| 156 | | | } |
| 157 | | | } |
| 158 | 100 | | return new YearMonth(year, month); |
| 159 | | | } |
| 160 | | | |
| 161 | | | |
| 162 | | | |
| 163 | | | |
| 164 | | | |
| 165 | | | @return |
| 166 | | | |
| 167 | | | public Date toStartDate () |
| 168 | | | { |
| 169 | 100 | | if (mStartDate == null) |
| 170 | | | { |
| 171 | 100 | | final Calendar cal = Calendar.getInstance(Date.TIME_ZONE); |
| 172 | 100 | | cal.setLenient(false); |
| 173 | 100 | | cal.clear(); |
| 174 | 100 | | if (getYear() > 0) |
| 175 | | | { |
| 176 | 100 | | cal.set(getYear(), getMonth() - 1, 1); |
| 177 | | | } |
| 178 | | | else |
| 179 | | | { |
| 180 | 0 | | cal.set(-getYear(), getMonth() - 1, 1); |
| 181 | 0 | | cal.set(Calendar.ERA, GregorianCalendar.BC); |
| 182 | | | } |
| 183 | 100 | | cal.set(Calendar.DAY_OF_MONTH, |
| 184 | | | cal.getActualMinimum(Calendar.DAY_OF_MONTH)); |
| 185 | 100 | | cal.set(Calendar.HOUR_OF_DAY, |
| 186 | | | cal.getActualMinimum(Calendar.HOUR_OF_DAY)); |
| 187 | 100 | | cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE)); |
| 188 | 100 | | cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND)); |
| 189 | 100 | | cal.set(Calendar.MILLISECOND, |
| 190 | | | cal.getActualMinimum(Calendar.MILLISECOND)); |
| 191 | 100 | | mStartDate = new Date(cal.getTimeInMillis()); |
| 192 | | | } |
| 193 | 100 | | return mStartDate; |
| 194 | | | } |
| 195 | | | |
| 196 | | | |
| 197 | | | |
| 198 | | | |
| 199 | | | |
| 200 | | | @return |
| 201 | | | |
| 202 | | | public Date toEndDate () |
| 203 | | | { |
| 204 | 100 | | if (mEndDate == null) |
| 205 | | | { |
| 206 | 100 | | final Calendar cal = Calendar.getInstance(Date.TIME_ZONE); |
| 207 | 100 | | cal.setLenient(false); |
| 208 | 100 | | cal.clear(); |
| 209 | 100 | | if (getYear() > 0) |
| 210 | | | { |
| 211 | 100 | | cal.set(getYear(), getMonth() - 1, 1); |
| 212 | | | } |
| 213 | | | else |
| 214 | | | { |
| 215 | 0 | | cal.set(-getYear(), getMonth() - 1, 1); |
| 216 | 0 | | cal.set(Calendar.ERA, GregorianCalendar.BC); |
| 217 | | | } |
| 218 | 100 | | cal.set(Calendar.DAY_OF_MONTH, |
| 219 | | | cal.getActualMaximum(Calendar.DAY_OF_MONTH)); |
| 220 | 100 | | cal.set(Calendar.HOUR_OF_DAY, |
| 221 | | | cal.getActualMaximum(Calendar.HOUR_OF_DAY)); |
| 222 | 100 | | cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE)); |
| 223 | 100 | | cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND)); |
| 224 | 100 | | cal.set(Calendar.MILLISECOND, |
| 225 | | | cal.getActualMaximum(Calendar.MILLISECOND)); |
| 226 | 100 | | mEndDate = new Date(cal.getTimeInMillis()); |
| 227 | | | } |
| 228 | 100 | | return mEndDate; |
| 229 | | | } |
| 230 | | | |
| 231 | | | |
| 232 | | | |
| 233 | | | |
| 234 | | | @return |
| 235 | | | |
| 236 | | | public Period toPeriod () |
| 237 | | | { |
| 238 | 100 | | if (mPeriod == null) |
| 239 | | | { |
| 240 | 100 | | mPeriod = Period.createPeriod(toStartDate(), toEndDate()); |
| 241 | | | } |
| 242 | 100 | | return mPeriod; |
| 243 | | | } |
| 244 | | | |
| 245 | | | |
| 246 | | | |
| 247 | | | @return |
| 248 | | | |
| 249 | | | public int getMonth () |
| 250 | | | { |
| 251 | 100 | | return mMonth; |
| 252 | | | } |
| 253 | | | |
| 254 | | | @return |
| 255 | | | |
| 256 | | | public int getYear () |
| 257 | | | { |
| 258 | 100 | | return mYear; |
| 259 | | | } |
| 260 | | | |
| 261 | | | |
| 262 | | | |
| 263 | | | @param |
| 264 | | | @return |
| 265 | | | |
| 266 | | | public boolean isWithin (Date date) |
| 267 | | | { |
| 268 | 0 | | final long current = date.getTime(); |
| 269 | 0 | | return toStartDate().getTime() <= current |
| 270 | | | && current <= toEndDate().getTime(); |
| 271 | | | } |
| 272 | | | |
| 273 | | | {@inheritDoc} |
| 274 | | | public String toString () |
| 275 | | | { |
| 276 | 100 | | if (mString == null) |
| 277 | | | { |
| 278 | | | String year; |
| 279 | 100 | | if (mYear >= 0) |
| 280 | | | { |
| 281 | 100 | | year = Integer.toString(mYear); |
| 282 | 100 | | if (year.length() < MINIMUM_NUMBER_OF_YEAR_DIGITS) |
| 283 | | | { |
| 284 | 100 | (8) | year = "0000".substring(year.length()) + year; |
| 285 | | | } |
| 286 | | | } |
| 287 | | | else |
| 288 | | | { |
| 289 | 100 | | year = Integer.toString(-mYear); |
| 290 | 100 | | if (year.length() < MINIMUM_NUMBER_OF_YEAR_DIGITS) |
| 291 | | | { |
| 292 | 100 | (9) | year = "0000".substring(year.length()) + year; |
| 293 | | | } |
| 294 | 100 | (10) | year = "-" + year; |
| 295 | | | } |
| 296 | 100 | | if (mMonth < TWO_DIGIT_MONTH) |
| 297 | | | { |
| 298 | 100 | | mString = year + "-0" + Integer.toString(mMonth) + 'Z'; |
| 299 | | | } |
| 300 | | | else |
| 301 | | | { |
| 302 | 100 | | mString = year + '-' + Integer.toString(mMonth) + 'Z'; |
| 303 | | | } |
| 304 | | | } |
| 305 | 100 | | return mString; |
| 306 | | | } |
| 307 | | | |
| 308 | | | {@inheritDoc} |
| 309 | | | public int hashCode () |
| 310 | | | { |
| 311 | 0 | | if (mHashCode == 0) |
| 312 | | | { |
| 313 | 0 | | mHashCode = mYear * Date.MONTH_PER_YEAR + mMonth; |
| 314 | | | } |
| 315 | 0 | | return mHashCode; |
| 316 | | | } |
| 317 | | | |
| 318 | | | {@inheritDoc} |
| 319 | | | public boolean equals (Object o) |
| 320 | | | { |
| 321 | | | final boolean result; |
| 322 | 0 | | if (o instanceof YearMonth) |
| 323 | | | { |
| 324 | 0 | | final YearMonth other = (YearMonth) o; |
| 325 | 0 | | result = other.mMonth == mMonth && other.mYear == mYear; |
| 326 | 0 | | } |
| 327 | | | else |
| 328 | | | { |
| 329 | 0 | | result = false; |
| 330 | | | } |
| 331 | 0 | | return result; |
| 332 | | | } |
| 333 | | | } |