1 16 package org.joda.time.field; 17 18 import java.io.Serializable ; 19 import java.util.Locale ; 20 21 import org.joda.time.Chronology; 22 import org.joda.time.DateTimeField; 23 import org.joda.time.DateTimeFieldType; 24 import org.joda.time.DateTimeUtils; 25 import org.joda.time.DurationField; 26 import org.joda.time.Interval; 27 import org.joda.time.ReadableInstant; 28 import org.joda.time.ReadablePartial; 29 30 46 public abstract class AbstractReadableInstantFieldProperty implements Serializable { 47 48 49 private static final long serialVersionUID = 1971226328211649661L; 50 51 54 public AbstractReadableInstantFieldProperty() { 55 super(); 56 } 57 58 64 public abstract DateTimeField getField(); 65 66 71 public DateTimeFieldType getFieldType() { 72 return getField().getType(); 73 } 74 75 80 public String getName() { 81 return getField().getName(); 82 } 83 84 89 protected abstract long getMillis(); 90 91 100 protected Chronology getChronology() { 101 throw new UnsupportedOperationException ( 102 "The method getChronology() was added in v1.4 and needs " + 103 "to be implemented by subclasses of AbstractReadableInstantFieldProperty"); 104 } 105 106 119 public int get() { 120 return getField().get(getMillis()); 121 } 122 123 135 public String getAsString() { 136 return Integer.toString(get()); 137 } 138 139 150 public String getAsText() { 151 return getAsText(null); 152 } 153 154 166 public String getAsText(Locale locale) { 167 return getField().getAsText(getMillis(), locale); 168 } 169 170 181 public String getAsShortText() { 182 return getAsShortText(null); 183 } 184 185 197 public String getAsShortText(Locale locale) { 198 return getField().getAsShortText(getMillis(), locale); 199 } 200 201 212 public int getDifference(ReadableInstant instant) { 213 if (instant == null) { 214 return getField().getDifference(getMillis(), DateTimeUtils.currentTimeMillis()); 215 } 216 return getField().getDifference(getMillis(), instant.getMillis()); 217 } 218 219 229 public long getDifferenceAsLong(ReadableInstant instant) { 230 if (instant == null) { 231 return getField().getDifferenceAsLong(getMillis(), DateTimeUtils.currentTimeMillis()); 232 } 233 return getField().getDifferenceAsLong(getMillis(), instant.getMillis()); 234 } 235 236 243 public DurationField getDurationField() { 244 return getField().getDurationField(); 245 } 246 247 253 public DurationField getRangeDurationField() { 254 return getField().getRangeDurationField(); 255 } 256 257 263 public boolean isLeap() { 264 return getField().isLeap(getMillis()); 265 } 266 267 273 public int getLeapAmount() { 274 return getField().getLeapAmount(getMillis()); 275 } 276 277 281 public DurationField getLeapDurationField() { 282 return getField().getLeapDurationField(); 283 } 284 285 292 public int getMinimumValueOverall() { 293 return getField().getMinimumValue(); 294 } 295 296 302 public int getMinimumValue() { 303 return getField().getMinimumValue(getMillis()); 304 } 305 306 312 public int getMaximumValueOverall() { 313 return getField().getMaximumValue(); 314 } 315 316 322 public int getMaximumValue() { 323 return getField().getMaximumValue(getMillis()); 324 } 325 326 333 public int getMaximumTextLength(Locale locale) { 334 return getField().getMaximumTextLength(locale); 335 } 336 337 344 public int getMaximumShortTextLength(Locale locale) { 345 return getField().getMaximumShortTextLength(locale); 346 } 347 348 349 355 public long remainder() { 356 return getField().remainder(getMillis()); 357 } 358 359 369 public Interval toInterval() { 370 DateTimeField field = getField(); 371 long start = field.roundFloor(getMillis()); 372 long end = field.add(start, 1); 373 Interval interval = new Interval(start, end); 374 return interval; 375 } 376 377 390 public int compareTo(ReadableInstant instant) { 391 if (instant == null) { 392 throw new IllegalArgumentException ("The instant must not be null"); 393 } 394 int thisValue = get(); 395 int otherValue = instant.get(getFieldType()); 396 if (thisValue < otherValue) { 397 return -1; 398 } else if (thisValue > otherValue) { 399 return 1; 400 } else { 401 return 0; 402 } 403 } 404 405 419 public int compareTo(ReadablePartial partial) { 420 if (partial == null) { 421 throw new IllegalArgumentException ("The partial must not be null"); 422 } 423 int thisValue = get(); 424 int otherValue = partial.get(getFieldType()); 425 if (thisValue < otherValue) { 426 return -1; 427 } else if (thisValue > otherValue) { 428 return 1; 429 } else { 430 return 0; 431 } 432 } 433 434 441 public boolean equals(Object object) { 442 if (this == object) { 443 return true; 444 } 445 if (object instanceof AbstractReadableInstantFieldProperty == false) { 446 return false; 447 } 448 AbstractReadableInstantFieldProperty other = (AbstractReadableInstantFieldProperty) object; 449 return 450 get() == other.get() && 451 getFieldType().equals(other.getFieldType()) && 452 FieldUtils.equals(getChronology(), other.getChronology()); 453 } 454 455 460 public int hashCode() { 461 return get() * 17 + getFieldType().hashCode() + getChronology().hashCode(); 462 } 463 464 470 public String toString() { 471 return "Property[" + getName() + "]"; 472 } 473 474 } 475 | Popular Tags |