1 16 package org.joda.time.base; 17 18 import org.joda.time.DurationFieldType; 19 import org.joda.time.MutablePeriod; 20 import org.joda.time.Period; 21 import org.joda.time.ReadablePeriod; 22 import org.joda.time.format.ISOPeriodFormat; 23 24 37 public abstract class AbstractPeriod implements ReadablePeriod { 38 39 42 protected AbstractPeriod() { 43 super(); 44 } 45 46 54 public DurationFieldType[] getFieldTypes() { 55 DurationFieldType[] result = new DurationFieldType[size()]; 56 for (int i = 0; i < result.length; i++) { 57 result[i] = getFieldType(i); 58 } 59 return result; 60 } 61 62 70 public int[] getValues() { 71 int[] result = new int[size()]; 72 for (int i = 0; i < result.length; i++) { 73 result[i] = getValue(i); 74 } 75 return result; 76 } 77 78 88 public int get(DurationFieldType type) { 89 int index = indexOf(type); 90 if (index == -1) { 91 return 0; 92 } 93 return getValue(index); 94 } 95 96 102 public boolean isSupported(DurationFieldType type) { 103 return getPeriodType().isSupported(type); 104 } 105 106 112 public int indexOf(DurationFieldType type) { 113 return getPeriodType().indexOf(type); 114 } 115 116 122 public Period toPeriod() { 123 return new Period(this); 124 } 125 126 133 public MutablePeriod toMutablePeriod() { 134 return new MutablePeriod(this); 135 } 136 137 158 public boolean equals(Object period) { 159 if (this == period) { 160 return true; 161 } 162 if (period instanceof ReadablePeriod == false) { 163 return false; 164 } 165 ReadablePeriod other = (ReadablePeriod) period; 166 if (size() != other.size()) { 167 return false; 168 } 169 for (int i = 0, isize = size(); i < isize; i++) { 170 if (getValue(i) != other.getValue(i) || getFieldType(i) != other.getFieldType(i)) { 171 return false; 172 } 173 } 174 return true; 175 } 176 177 182 public int hashCode() { 183 int total = 17; 184 for (int i = 0, isize = size(); i < isize; i++) { 185 total = 27 * total + getValue(i); 186 total = 27 * total + getFieldType(i).hashCode(); 187 } 188 return total; 189 } 190 191 202 public String toString() { 203 return ISOPeriodFormat.standard().print(this); 204 } 205 206 } 207 | Popular Tags |