1 16 package org.joda.time.field; 17 18 import java.util.Locale ; 19 20 import org.joda.time.DateTimeField; 21 import org.joda.time.DateTimeFieldType; 22 import org.joda.time.DurationField; 23 import org.joda.time.ReadableInstant; 24 import org.joda.time.ReadablePartial; 25 26 37 public abstract class AbstractPartialFieldProperty { 38 39 42 protected AbstractPartialFieldProperty() { 43 super(); 44 } 45 46 52 public abstract DateTimeField getField(); 53 54 59 public DateTimeFieldType getFieldType() { 60 return getField().getType(); 61 } 62 63 68 public String getName() { 69 return getField().getName(); 70 } 71 72 77 protected abstract ReadablePartial getReadablePartial(); 78 79 91 public abstract int get(); 92 93 105 public String getAsString() { 106 return Integer.toString(get()); 107 } 108 109 120 public String getAsText() { 121 return getAsText(null); 122 } 123 124 136 public String getAsText(Locale locale) { 137 return getField().getAsText(getReadablePartial(), get(), locale); 138 } 139 140 151 public String getAsShortText() { 152 return getAsShortText(null); 153 } 154 155 167 public String getAsShortText(Locale locale) { 168 return getField().getAsShortText(getReadablePartial(), get(), locale); 169 } 170 171 178 public DurationField getDurationField() { 179 return getField().getDurationField(); 180 } 181 182 188 public DurationField getRangeDurationField() { 189 return getField().getRangeDurationField(); 190 } 191 192 199 public int getMinimumValueOverall() { 200 return getField().getMinimumValue(); 201 } 202 203 209 public int getMinimumValue() { 210 return getField().getMinimumValue(getReadablePartial()); 211 } 212 213 219 public int getMaximumValueOverall() { 220 return getField().getMaximumValue(); 221 } 222 223 229 public int getMaximumValue() { 230 return getField().getMaximumValue(getReadablePartial()); 231 } 232 233 241 public int getMaximumTextLength(Locale locale) { 242 return getField().getMaximumTextLength(locale); 243 } 244 245 252 public int getMaximumShortTextLength(Locale locale) { 253 return getField().getMaximumShortTextLength(locale); 254 } 255 256 270 public int compareTo(ReadableInstant instant) { 271 if (instant == null) { 272 throw new IllegalArgumentException ("The instant must not be null"); 273 } 274 int thisValue = get(); 275 int otherValue = instant.get(getFieldType()); 276 if (thisValue < otherValue) { 277 return -1; 278 } else if (thisValue > otherValue) { 279 return 1; 280 } else { 281 return 0; 282 } 283 } 284 285 299 public int compareTo(ReadablePartial partial) { 300 if (partial == null) { 301 throw new IllegalArgumentException ("The instant must not be null"); 302 } 303 int thisValue = get(); 304 int otherValue = partial.get(getFieldType()); 305 if (thisValue < otherValue) { 306 return -1; 307 } else if (thisValue > otherValue) { 308 return 1; 309 } else { 310 return 0; 311 } 312 } 313 314 321 public boolean equals(Object object) { 322 if (this == object) { 323 return true; 324 } 325 if (object instanceof AbstractPartialFieldProperty == false) { 326 return false; 327 } 328 AbstractPartialFieldProperty other = (AbstractPartialFieldProperty) object; 329 return 330 get() == other.get() && 331 getFieldType() == other.getFieldType() && 332 FieldUtils.equals(getReadablePartial().getChronology(), other.getReadablePartial().getChronology()); 333 } 334 335 342 public int hashCode() { 343 int hash = 19; 344 hash = 13 * hash + get(); 345 hash = 13 * hash + getFieldType().hashCode(); 346 hash = 13 * hash + getReadablePartial().getChronology().hashCode(); 347 return hash; 348 } 349 350 356 public String toString() { 357 return "Property[" + getName() + "]"; 358 } 359 360 } 361 | Popular Tags |