1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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<DateTimeZone, ISOChronology> cCache = new HashMap<DateTimeZone, ISOChronology>();
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
71
72
73
74
75
76 public static ISOChronology getInstanceUTC() {
77 return INSTANCE_UTC;
78 }
79
80
81
82
83
84
85 public static ISOChronology getInstance() {
86 return getInstance(DateTimeZone.getDefault());
87 }
88
89
90
91
92
93
94
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 = 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
116
117
118
119
120
121 private ISOChronology(Chronology base) {
122 super(base, null);
123 }
124
125
126
127
128
129
130
131
132 public Chronology withUTC() {
133 return INSTANCE_UTC;
134 }
135
136
137
138
139
140
141
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
153
154
155
156
157
158
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
171 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
183
184
185
186
187
188
189 public boolean equals(Object obj) {
190 return super.equals(obj);
191 }
192
193
194
195
196
197
198
199 public int hashCode() {
200 return "ISO".hashCode() * 11 + getZone().hashCode();
201 }
202
203
204
205
206
207 private Object writeReplace() {
208 return new Stub(getZone());
209 }
210
211 private static final class Stub implements Serializable {
212 private static final long serialVersionUID = -6212696554273812441L;
213
214 private transient DateTimeZone iZone;
215
216 Stub(DateTimeZone zone) {
217 iZone = zone;
218 }
219
220 private Object readResolve() {
221 return ISOChronology.getInstance(iZone);
222 }
223
224 private void writeObject(ObjectOutputStream out) throws IOException {
225 out.writeObject(iZone);
226 }
227
228 private void readObject(ObjectInputStream in)
229 throws IOException, ClassNotFoundException
230 {
231 iZone = (DateTimeZone)in.readObject();
232 }
233 }
234
235 }