1 16 package org.joda.time.base; 17 18 import org.joda.time.Chronology; 19 import org.joda.time.DateTime; 20 import org.joda.time.DateTimeField; 21 import org.joda.time.DateTimeFieldType; 22 import org.joda.time.DateTimeUtils; 23 import org.joda.time.DurationFieldType; 24 import org.joda.time.ReadableInstant; 25 import org.joda.time.ReadablePartial; 26 import org.joda.time.field.FieldUtils; 27 import org.joda.time.format.DateTimeFormatter; 28 29 46 public abstract class AbstractPartial 47 implements ReadablePartial, Comparable { 48 49 53 protected AbstractPartial() { 54 super(); 55 } 56 57 68 protected abstract DateTimeField getField(int index, Chronology chrono); 69 70 78 public DateTimeFieldType getFieldType(int index) { 79 return getField(index, getChronology()).getType(); 80 } 81 82 89 public DateTimeFieldType[] getFieldTypes() { 90 DateTimeFieldType[] result = new DateTimeFieldType[size()]; 91 for (int i = 0; i < result.length; i++) { 92 result[i] = getFieldType(i); 93 } 94 return result; 95 } 96 97 104 public DateTimeField getField(int index) { 105 return getField(index, getChronology()); 106 } 107 108 115 public DateTimeField[] getFields() { 116 DateTimeField[] result = new DateTimeField[size()]; 117 for (int i = 0; i < result.length; i++) { 118 result[i] = getField(i); 119 } 120 return result; 121 } 122 123 131 public int[] getValues() { 132 int[] result = new int[size()]; 133 for (int i = 0; i < result.length; i++) { 134 result[i] = getValue(i); 135 } 136 return result; 137 } 138 139 149 public int get(DateTimeFieldType type) { 150 return getValue(indexOfSupported(type)); 151 } 152 153 159 public boolean isSupported(DateTimeFieldType type) { 160 return (indexOf(type) != -1); 161 } 162 163 169 public int indexOf(DateTimeFieldType type) { 170 for (int i = 0, isize = size(); i < isize; i++) { 171 if (getFieldType(i) == type) { 172 return i; 173 } 174 } 175 return -1; 176 } 177 178 186 protected int indexOfSupported(DateTimeFieldType type) { 187 int index = indexOf(type); 188 if (index == -1) { 189 throw new IllegalArgumentException ("Field '" + type + "' is not supported"); 190 } 191 return index; 192 } 193 194 201 protected int indexOf(DurationFieldType type) { 202 for (int i = 0, isize = size(); i < isize; i++) { 203 if (getFieldType(i).getDurationType() == type) { 204 return i; 205 } 206 } 207 return -1; 208 } 209 210 218 protected int indexOfSupported(DurationFieldType type) { 219 int index = indexOf(type); 220 if (index == -1) { 221 throw new IllegalArgumentException ("Field '" + type + "' is not supported"); 222 } 223 return index; 224 } 225 226 239 public DateTime toDateTime(ReadableInstant baseInstant) { 240 Chronology chrono = DateTimeUtils.getInstantChronology(baseInstant); 241 long instantMillis = DateTimeUtils.getInstantMillis(baseInstant); 242 long resolved = chrono.set(this, instantMillis); 243 return new DateTime(resolved, chrono); 244 } 245 246 254 public boolean equals(Object partial) { 255 if (this == partial) { 256 return true; 257 } 258 if (partial instanceof ReadablePartial == false) { 259 return false; 260 } 261 ReadablePartial other = (ReadablePartial) partial; 262 if (size() != other.size()) { 263 return false; 264 } 265 for (int i = 0, isize = size(); i < isize; i++) { 266 if (getValue(i) != other.getValue(i) || getFieldType(i) != other.getFieldType(i)) { 267 return false; 268 } 269 } 270 return FieldUtils.equals(getChronology(), other.getChronology()); 271 } 272 273 279 public int hashCode() { 280 int total = 157; 281 for (int i = 0, isize = size(); i < isize; i++) { 282 total = 23 * total + getValue(i); 283 total = 23 * total + getFieldType(i).hashCode(); 284 } 285 total += getChronology().hashCode(); 286 return total; 287 } 288 289 315 public int compareTo(Object partial) { 316 if (this == partial) { 317 return 0; 318 } 319 ReadablePartial other = (ReadablePartial) partial; 320 if (size() != other.size()) { 321 throw new ClassCastException ("ReadablePartial objects must have matching field types"); 322 } 323 for (int i = 0, isize = size(); i < isize; i++) { 324 if (getFieldType(i) != other.getFieldType(i)) { 325 throw new ClassCastException ("ReadablePartial objects must have matching field types"); 326 } 327 } 328 for (int i = 0, isize = size(); i < isize; i++) { 330 if (getValue(i) > other.getValue(i)) { 331 return 1; 332 } 333 if (getValue(i) < other.getValue(i)) { 334 return -1; 335 } 336 } 337 return 0; 338 } 339 340 355 public boolean isAfter(ReadablePartial partial) { 356 if (partial == null) { 357 throw new IllegalArgumentException ("Partial cannot be null"); 358 } 359 return compareTo(partial) > 0; 360 } 361 362 377 public boolean isBefore(ReadablePartial partial) { 378 if (partial == null) { 379 throw new IllegalArgumentException ("Partial cannot be null"); 380 } 381 return compareTo(partial) < 0; 382 } 383 384 399 public boolean isEqual(ReadablePartial partial) { 400 if (partial == null) { 401 throw new IllegalArgumentException ("Partial cannot be null"); 402 } 403 return compareTo(partial) == 0; 404 } 405 406 414 public String toString(DateTimeFormatter formatter) { 415 if (formatter == null) { 416 return toString(); 417 } 418 return formatter.print(this); 419 } 420 421 } 422 | Popular Tags |