1 16 package org.joda.time.base; 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.DateTimeUtils; 24 import org.joda.time.ReadablePartial; 25 import org.joda.time.convert.ConverterManager; 26 import org.joda.time.convert.PartialConverter; 27 import org.joda.time.format.DateTimeFormat; 28 import org.joda.time.format.DateTimeFormatter; 29 30 43 public abstract class BasePartial 44 extends AbstractPartial 45 implements ReadablePartial, Serializable { 46 47 48 private static final long serialVersionUID = 2353678632973660L; 49 50 51 private Chronology iChronology; 52 53 private int[] iValues; 54 55 64 protected BasePartial() { 65 this(DateTimeUtils.currentTimeMillis(), null); 66 } 67 68 78 protected BasePartial(Chronology chronology) { 79 this(DateTimeUtils.currentTimeMillis(), chronology); 80 } 81 82 92 protected BasePartial(long instant) { 93 this(instant, null); 94 } 95 96 107 protected BasePartial(long instant, Chronology chronology) { 108 super(); 109 chronology = DateTimeUtils.getChronology(chronology); 110 iChronology = chronology.withUTC(); 111 iValues = chronology.get(this, instant); 112 } 113 114 130 protected BasePartial(Object instant, Chronology chronology) { 131 super(); 132 PartialConverter converter = ConverterManager.getInstance().getPartialConverter(instant); 133 chronology = converter.getChronology(instant, chronology); 134 chronology = DateTimeUtils.getChronology(chronology); 135 iChronology = chronology.withUTC(); 136 iValues = converter.getPartialValues(this, instant, chronology); 137 } 138 139 157 protected BasePartial(Object instant, Chronology chronology, DateTimeFormatter parser) { 158 super(); 159 PartialConverter converter = ConverterManager.getInstance().getPartialConverter(instant); 160 chronology = converter.getChronology(instant, chronology); 161 chronology = DateTimeUtils.getChronology(chronology); 162 iChronology = chronology.withUTC(); 163 iValues = converter.getPartialValues(this, instant, chronology, parser); 164 } 165 166 179 protected BasePartial(int[] values, Chronology chronology) { 180 super(); 181 chronology = DateTimeUtils.getChronology(chronology); 182 iChronology = chronology.withUTC(); 183 chronology.validate(this, values); 184 iValues = values; 185 } 186 187 195 protected BasePartial(BasePartial base, int[] values) { 196 super(); 197 iChronology = base.iChronology; 198 iValues = values; 199 } 200 201 210 protected BasePartial(BasePartial base, Chronology chrono) { 211 super(); 212 iChronology = chrono.withUTC(); 213 iValues = base.iValues; 214 } 215 216 224 public int getValue(int index) { 225 return iValues[index]; 226 } 227 228 236 public int[] getValues() { 237 return (int[]) iValues.clone(); 238 } 239 240 248 public Chronology getChronology() { 249 return iChronology; 250 } 251 252 260 protected void setValue(int index, int value) { 261 DateTimeField field = getField(index); 262 iValues = field.set(this, index, iValues, value); 263 } 264 265 270 protected void setValues(int[] values) { 271 getChronology().validate(this, values); 272 iValues = values; 273 } 274 275 282 public String toString(String pattern) { 283 if (pattern == null) { 284 return toString(); 285 } 286 return DateTimeFormat.forPattern(pattern).print(this); 287 } 288 289 296 public String toString(String pattern, Locale locale) throws IllegalArgumentException { 297 if (pattern == null) { 298 return toString(); 299 } 300 return DateTimeFormat.forPattern(pattern).withLocale(locale).print(this); 301 } 302 303 } 304 | Popular Tags |