1 16 package org.joda.time.chrono; 17 18 import java.io.IOException ; 19 import java.io.ObjectInputStream ; 20 import java.io.ObjectOutputStream ; 21 import java.io.Serializable ; 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 import org.joda.time.Chronology; 26 import org.joda.time.DateTimeFieldType; 27 import org.joda.time.DateTimeZone; 28 import org.joda.time.field.DividedDateTimeField; 29 import org.joda.time.field.RemainderDateTimeField; 30 31 49 public final class ISOChronology extends AssembledChronology { 50 51 52 private static final long serialVersionUID = -6212696554273812441L; 53 54 55 private static final ISOChronology INSTANCE_UTC; 56 57 private static final int FAST_CACHE_SIZE = 64; 58 59 60 private static final ISOChronology[] cFastCache; 61 62 63 private static final Map cCache = new HashMap (); 64 static { 65 cFastCache = new ISOChronology[FAST_CACHE_SIZE]; 66 INSTANCE_UTC = new ISOChronology(GregorianChronology.getInstanceUTC()); 67 cCache.put(DateTimeZone.UTC, INSTANCE_UTC); 68 } 69 70 76 public static ISOChronology getInstanceUTC() { 77 return INSTANCE_UTC; 78 } 79 80 85 public static ISOChronology getInstance() { 86 return getInstance(DateTimeZone.getDefault()); 87 } 88 89 95 public static ISOChronology getInstance(DateTimeZone zone) { 96 if (zone == null) { 97 zone = DateTimeZone.getDefault(); 98 } 99 int index = System.identityHashCode(zone) & (FAST_CACHE_SIZE - 1); 100 ISOChronology chrono = cFastCache[index]; 101 if (chrono != null && chrono.getZone() == zone) { 102 return chrono; 103 } 104 synchronized (cCache) { 105 chrono = (ISOChronology) cCache.get(zone); 106 if (chrono == null) { 107 chrono = new ISOChronology(ZonedChronology.getInstance(INSTANCE_UTC, zone)); 108 cCache.put(zone, chrono); 109 } 110 } 111 cFastCache[index] = chrono; 112 return chrono; 113 } 114 115 118 121 private ISOChronology(Chronology base) { 122 super(base, null); 123 } 124 125 132 public Chronology withUTC() { 133 return INSTANCE_UTC; 134 } 135 136 142 public Chronology withZone(DateTimeZone zone) { 143 if (zone == null) { 144 zone = DateTimeZone.getDefault(); 145 } 146 if (zone == getZone()) { 147 return this; 148 } 149 return getInstance(zone); 150 } 151 152 159 public String toString() { 160 String str = "ISOChronology"; 161 DateTimeZone zone = getZone(); 162 if (zone != null) { 163 str = str + '[' + zone.getID() + ']'; 164 } 165 return str; 166 } 167 168 protected void assemble(Fields fields) { 169 if (getBase().getZone() == DateTimeZone.UTC) { 170 fields.centuryOfEra = new DividedDateTimeField( 172 ISOYearOfEraDateTimeField.INSTANCE, DateTimeFieldType.centuryOfEra(), 100); 173 fields.yearOfCentury = new RemainderDateTimeField( 174 (DividedDateTimeField) fields.centuryOfEra, DateTimeFieldType.yearOfCentury()); 175 fields.weekyearOfCentury = new RemainderDateTimeField( 176 (DividedDateTimeField) fields.centuryOfEra, DateTimeFieldType.weekyearOfCentury()); 177 178 fields.centuries = fields.centuryOfEra.getDurationField(); 179 } 180 } 181 182 186 private Object writeReplace() { 187 return new Stub(getZone()); 188 } 189 190 private static final class Stub implements Serializable { 191 private static final long serialVersionUID = -6212696554273812441L; 192 193 private transient DateTimeZone iZone; 194 195 Stub(DateTimeZone zone) { 196 iZone = zone; 197 } 198 199 private Object readResolve() { 200 return ISOChronology.getInstance(iZone); 201 } 202 203 private void writeObject(ObjectOutputStream out) throws IOException { 204 out.writeObject(iZone); 205 } 206 207 private void readObject(ObjectInputStream in) 208 throws IOException , ClassNotFoundException 209 { 210 iZone = (DateTimeZone)in.readObject(); 211 } 212 } 213 214 } 215 | Popular Tags |