1 16 package org.joda.time; 17 18 import java.io.Serializable ; 19 20 40 public abstract class DateTimeFieldType implements Serializable { 41 42 43 private static final long serialVersionUID = -42615285973990L; 44 45 46 static final byte 47 ERA = 1, 48 YEAR_OF_ERA = 2, 49 CENTURY_OF_ERA = 3, 50 YEAR_OF_CENTURY = 4, 51 YEAR = 5, 52 DAY_OF_YEAR = 6, 53 MONTH_OF_YEAR = 7, 54 DAY_OF_MONTH = 8, 55 WEEKYEAR_OF_CENTURY = 9, 56 WEEKYEAR = 10, 57 WEEK_OF_WEEKYEAR = 11, 58 DAY_OF_WEEK = 12, 59 HALFDAY_OF_DAY = 13, 60 HOUR_OF_HALFDAY = 14, 61 CLOCKHOUR_OF_HALFDAY = 15, 62 CLOCKHOUR_OF_DAY = 16, 63 HOUR_OF_DAY = 17, 64 MINUTE_OF_DAY = 18, 65 MINUTE_OF_HOUR = 19, 66 SECOND_OF_DAY = 20, 67 SECOND_OF_MINUTE = 21, 68 MILLIS_OF_DAY = 22, 69 MILLIS_OF_SECOND = 23; 70 71 72 private static final DateTimeFieldType ERA_TYPE = new StandardDateTimeFieldType( 73 "era", ERA, DurationFieldType.eras(), null); 74 75 private static final DateTimeFieldType YEAR_OF_ERA_TYPE = new StandardDateTimeFieldType( 76 "yearOfEra", YEAR_OF_ERA, DurationFieldType.years(), DurationFieldType.eras()); 77 78 private static final DateTimeFieldType CENTURY_OF_ERA_TYPE = new StandardDateTimeFieldType( 79 "centuryOfEra", CENTURY_OF_ERA, DurationFieldType.centuries(), DurationFieldType.eras()); 80 81 private static final DateTimeFieldType YEAR_OF_CENTURY_TYPE = new StandardDateTimeFieldType( 82 "yearOfCentury", YEAR_OF_CENTURY, DurationFieldType.years(), DurationFieldType.centuries()); 83 84 private static final DateTimeFieldType YEAR_TYPE = new StandardDateTimeFieldType( 85 "year", YEAR, DurationFieldType.years(), null); 86 87 private static final DateTimeFieldType DAY_OF_YEAR_TYPE = new StandardDateTimeFieldType( 88 "dayOfYear", DAY_OF_YEAR, DurationFieldType.days(), DurationFieldType.years()); 89 90 private static final DateTimeFieldType MONTH_OF_YEAR_TYPE = new StandardDateTimeFieldType( 91 "monthOfYear", MONTH_OF_YEAR, DurationFieldType.months(), DurationFieldType.years()); 92 93 private static final DateTimeFieldType DAY_OF_MONTH_TYPE = new StandardDateTimeFieldType( 94 "dayOfMonth", DAY_OF_MONTH, DurationFieldType.days(), DurationFieldType.months()); 95 96 private static final DateTimeFieldType WEEKYEAR_OF_CENTURY_TYPE = new StandardDateTimeFieldType( 97 "weekyearOfCentury", WEEKYEAR_OF_CENTURY, DurationFieldType.weekyears(), DurationFieldType.centuries()); 98 99 private static final DateTimeFieldType WEEKYEAR_TYPE = new StandardDateTimeFieldType( 100 "weekyear", WEEKYEAR, DurationFieldType.weekyears(), null); 101 102 private static final DateTimeFieldType WEEK_OF_WEEKYEAR_TYPE = new StandardDateTimeFieldType( 103 "weekOfWeekyear", WEEK_OF_WEEKYEAR, DurationFieldType.weeks(), DurationFieldType.weekyears()); 104 105 private static final DateTimeFieldType DAY_OF_WEEK_TYPE = new StandardDateTimeFieldType( 106 "dayOfWeek", DAY_OF_WEEK, DurationFieldType.days(), DurationFieldType.weeks()); 107 108 109 private static final DateTimeFieldType HALFDAY_OF_DAY_TYPE = new StandardDateTimeFieldType( 110 "halfdayOfDay", HALFDAY_OF_DAY, DurationFieldType.halfdays(), DurationFieldType.days()); 111 112 private static final DateTimeFieldType HOUR_OF_HALFDAY_TYPE = new StandardDateTimeFieldType( 113 "hourOfHalfday", HOUR_OF_HALFDAY, DurationFieldType.hours(), DurationFieldType.halfdays()); 114 115 private static final DateTimeFieldType CLOCKHOUR_OF_HALFDAY_TYPE = new StandardDateTimeFieldType( 116 "clockhourOfHalfday", CLOCKHOUR_OF_HALFDAY, DurationFieldType.hours(), DurationFieldType.halfdays()); 117 118 private static final DateTimeFieldType CLOCKHOUR_OF_DAY_TYPE = new StandardDateTimeFieldType( 119 "clockhourOfDay", CLOCKHOUR_OF_DAY, DurationFieldType.hours(), DurationFieldType.days()); 120 121 private static final DateTimeFieldType HOUR_OF_DAY_TYPE = new StandardDateTimeFieldType( 122 "hourOfDay", HOUR_OF_DAY, DurationFieldType.hours(), DurationFieldType.days()); 123 124 private static final DateTimeFieldType MINUTE_OF_DAY_TYPE = new StandardDateTimeFieldType( 125 "minuteOfDay", MINUTE_OF_DAY, DurationFieldType.minutes(), DurationFieldType.days()); 126 127 private static final DateTimeFieldType MINUTE_OF_HOUR_TYPE = new StandardDateTimeFieldType( 128 "minuteOfHour", MINUTE_OF_HOUR, DurationFieldType.minutes(), DurationFieldType.hours()); 129 130 private static final DateTimeFieldType SECOND_OF_DAY_TYPE = new StandardDateTimeFieldType( 131 "secondOfDay", SECOND_OF_DAY, DurationFieldType.seconds(), DurationFieldType.days()); 132 133 private static final DateTimeFieldType SECOND_OF_MINUTE_TYPE = new StandardDateTimeFieldType( 134 "secondOfMinute", SECOND_OF_MINUTE, DurationFieldType.seconds(), DurationFieldType.minutes()); 135 136 private static final DateTimeFieldType MILLIS_OF_DAY_TYPE = new StandardDateTimeFieldType( 137 "millisOfDay", MILLIS_OF_DAY, DurationFieldType.millis(), DurationFieldType.days()); 138 139 private static final DateTimeFieldType MILLIS_OF_SECOND_TYPE = new StandardDateTimeFieldType( 140 "millisOfSecond", MILLIS_OF_SECOND, DurationFieldType.millis(), DurationFieldType.seconds()); 141 142 143 private final String iName; 144 145 151 protected DateTimeFieldType(String name) { 152 super(); 153 iName = name; 154 } 155 156 162 public static DateTimeFieldType millisOfSecond() { 163 return MILLIS_OF_SECOND_TYPE; 164 } 165 166 171 public static DateTimeFieldType millisOfDay() { 172 return MILLIS_OF_DAY_TYPE; 173 } 174 175 180 public static DateTimeFieldType secondOfMinute() { 181 return SECOND_OF_MINUTE_TYPE; 182 } 183 184 189 public static DateTimeFieldType secondOfDay() { 190 return SECOND_OF_DAY_TYPE; 191 } 192 193 198 public static DateTimeFieldType minuteOfHour() { 199 return MINUTE_OF_HOUR_TYPE; 200 } 201 202 207 public static DateTimeFieldType minuteOfDay() { 208 return MINUTE_OF_DAY_TYPE; 209 } 210 211 216 public static DateTimeFieldType hourOfDay() { 217 return HOUR_OF_DAY_TYPE; 218 } 219 220 225 public static DateTimeFieldType clockhourOfDay() { 226 return CLOCKHOUR_OF_DAY_TYPE; 227 } 228 229 234 public static DateTimeFieldType hourOfHalfday() { 235 return HOUR_OF_HALFDAY_TYPE; 236 } 237 238 243 public static DateTimeFieldType clockhourOfHalfday() { 244 return CLOCKHOUR_OF_HALFDAY_TYPE; 245 } 246 247 252 public static DateTimeFieldType halfdayOfDay() { 253 return HALFDAY_OF_DAY_TYPE; 254 } 255 256 262 public static DateTimeFieldType dayOfWeek() { 263 return DAY_OF_WEEK_TYPE; 264 } 265 266 271 public static DateTimeFieldType dayOfMonth() { 272 return DAY_OF_MONTH_TYPE; 273 } 274 275 280 public static DateTimeFieldType dayOfYear() { 281 return DAY_OF_YEAR_TYPE; 282 } 283 284 289 public static DateTimeFieldType weekOfWeekyear() { 290 return WEEK_OF_WEEKYEAR_TYPE; 291 } 292 293 298 public static DateTimeFieldType weekyear() { 299 return WEEKYEAR_TYPE; 300 } 301 302 307 public static DateTimeFieldType weekyearOfCentury() { 308 return WEEKYEAR_OF_CENTURY_TYPE; 309 } 310 311 316 public static DateTimeFieldType monthOfYear() { 317 return MONTH_OF_YEAR_TYPE; 318 } 319 320 325 public static DateTimeFieldType year() { 326 return YEAR_TYPE; 327 } 328 329 334 public static DateTimeFieldType yearOfEra() { 335 return YEAR_OF_ERA_TYPE; 336 } 337 338 343 public static DateTimeFieldType yearOfCentury() { 344 return YEAR_OF_CENTURY_TYPE; 345 } 346 347 352 public static DateTimeFieldType centuryOfEra() { 353 return CENTURY_OF_ERA_TYPE; 354 } 355 356 361 public static DateTimeFieldType era() { 362 return ERA_TYPE; 363 } 364 365 376 public String getName() { 377 return iName; 378 } 379 380 385 public abstract DurationFieldType getDurationType(); 386 387 392 public abstract DurationFieldType getRangeDurationType(); 393 394 400 public abstract DateTimeField getField(Chronology chronology); 401 402 408 public boolean isSupported(Chronology chronology) { 409 return getField(chronology).isSupported(); 410 } 411 412 417 public String toString() { 418 return getName(); 419 } 420 421 private static class StandardDateTimeFieldType extends DateTimeFieldType { 422 423 private static final long serialVersionUID = -9937958251642L; 424 425 426 private final byte iOrdinal; 427 428 429 private final transient DurationFieldType iUnitType; 430 431 private final transient DurationFieldType iRangeType; 432 433 441 StandardDateTimeFieldType(String name, byte ordinal, 442 DurationFieldType unitType, DurationFieldType rangeType) { 443 super(name); 444 iOrdinal = ordinal; 445 iUnitType = unitType; 446 iRangeType = rangeType; 447 } 448 449 450 public DurationFieldType getDurationType() { 451 return iUnitType; 452 } 453 454 455 public DurationFieldType getRangeDurationType() { 456 return iRangeType; 457 } 458 459 460 public DateTimeField getField(Chronology chronology) { 461 chronology = DateTimeUtils.getChronology(chronology); 462 463 switch (iOrdinal) { 464 case ERA: 465 return chronology.era(); 466 case YEAR_OF_ERA: 467 return chronology.yearOfEra(); 468 case CENTURY_OF_ERA: 469 return chronology.centuryOfEra(); 470 case YEAR_OF_CENTURY: 471 return chronology.yearOfCentury(); 472 case YEAR: 473 return chronology.year(); 474 case DAY_OF_YEAR: 475 return chronology.dayOfYear(); 476 case MONTH_OF_YEAR: 477 return chronology.monthOfYear(); 478 case DAY_OF_MONTH: 479 return chronology.dayOfMonth(); 480 case WEEKYEAR_OF_CENTURY: 481 return chronology.weekyearOfCentury(); 482 case WEEKYEAR: 483 return chronology.weekyear(); 484 case WEEK_OF_WEEKYEAR: 485 return chronology.weekOfWeekyear(); 486 case DAY_OF_WEEK: 487 return chronology.dayOfWeek(); 488 case HALFDAY_OF_DAY: 489 return chronology.halfdayOfDay(); 490 case HOUR_OF_HALFDAY: 491 return chronology.hourOfHalfday(); 492 case CLOCKHOUR_OF_HALFDAY: 493 return chronology.clockhourOfHalfday(); 494 case CLOCKHOUR_OF_DAY: 495 return chronology.clockhourOfDay(); 496 case HOUR_OF_DAY: 497 return chronology.hourOfDay(); 498 case MINUTE_OF_DAY: 499 return chronology.minuteOfDay(); 500 case MINUTE_OF_HOUR: 501 return chronology.minuteOfHour(); 502 case SECOND_OF_DAY: 503 return chronology.secondOfDay(); 504 case SECOND_OF_MINUTE: 505 return chronology.secondOfMinute(); 506 case MILLIS_OF_DAY: 507 return chronology.millisOfDay(); 508 case MILLIS_OF_SECOND: 509 return chronology.millisOfSecond(); 510 default: 511 throw new InternalError (); 513 } 514 } 515 516 521 private Object readResolve() { 522 switch (iOrdinal) { 523 case ERA: 524 return ERA_TYPE; 525 case YEAR_OF_ERA: 526 return YEAR_OF_ERA_TYPE; 527 case CENTURY_OF_ERA: 528 return CENTURY_OF_ERA_TYPE; 529 case YEAR_OF_CENTURY: 530 return YEAR_OF_CENTURY_TYPE; 531 case YEAR: 532 return YEAR_TYPE; 533 case DAY_OF_YEAR: 534 return DAY_OF_YEAR_TYPE; 535 case MONTH_OF_YEAR: 536 return MONTH_OF_YEAR_TYPE; 537 case DAY_OF_MONTH: 538 return DAY_OF_MONTH_TYPE; 539 case WEEKYEAR_OF_CENTURY: 540 return WEEKYEAR_OF_CENTURY_TYPE; 541 case WEEKYEAR: 542 return WEEKYEAR_TYPE; 543 case WEEK_OF_WEEKYEAR: 544 return WEEK_OF_WEEKYEAR_TYPE; 545 case DAY_OF_WEEK: 546 return DAY_OF_WEEK_TYPE; 547 case HALFDAY_OF_DAY: 548 return HALFDAY_OF_DAY_TYPE; 549 case HOUR_OF_HALFDAY: 550 return HOUR_OF_HALFDAY_TYPE; 551 case CLOCKHOUR_OF_HALFDAY: 552 return CLOCKHOUR_OF_HALFDAY_TYPE; 553 case CLOCKHOUR_OF_DAY: 554 return CLOCKHOUR_OF_DAY_TYPE; 555 case HOUR_OF_DAY: 556 return HOUR_OF_DAY_TYPE; 557 case MINUTE_OF_DAY: 558 return MINUTE_OF_DAY_TYPE; 559 case MINUTE_OF_HOUR: 560 return MINUTE_OF_HOUR_TYPE; 561 case SECOND_OF_DAY: 562 return SECOND_OF_DAY_TYPE; 563 case SECOND_OF_MINUTE: 564 return SECOND_OF_MINUTE_TYPE; 565 case MILLIS_OF_DAY: 566 return MILLIS_OF_DAY_TYPE; 567 case MILLIS_OF_SECOND: 568 return MILLIS_OF_SECOND_TYPE; 569 default: 570 return this; 572 } 573 } 574 } 575 576 } 577 | Popular Tags |