110 * <p>
111 * The main date and time API is built on the ISO calendar system.
112 * The chronology operates behind the scenes to represent the general concept of a calendar system.
113 * <p>
114 * See {@link Chronology} for more details.
115 *
116 * @implSpec
117 * This class is separated from the {@code Chronology} interface so that the static methods
118 * are not inherited. While {@code Chronology} can be implemented directly, it is strongly
119 * recommended to extend this abstract class instead.
120 * <p>
121 * This class must be implemented with care to ensure other classes operate correctly.
122 * All implementations that can be instantiated must be final, immutable and thread-safe.
123 * Subclasses should be Serializable wherever possible.
124 *
125 * @since 1.8
126 */
127 public abstract class AbstractChronology implements Chronology {
128
129 /**
130 * ChronoLocalDate order constant.
131 */
132 static final Comparator<ChronoLocalDate> DATE_ORDER =
133 (Comparator<ChronoLocalDate> & Serializable) (date1, date2) -> {
134 return Long.compare(date1.toEpochDay(), date2.toEpochDay());
135 };
136 /**
137 * ChronoLocalDateTime order constant.
138 */
139 static final Comparator<ChronoLocalDateTime<? extends ChronoLocalDate>> DATE_TIME_ORDER =
140 (Comparator<ChronoLocalDateTime<? extends ChronoLocalDate>> & Serializable) (dateTime1, dateTime2) -> {
141 int cmp = Long.compare(dateTime1.toLocalDate().toEpochDay(), dateTime2.toLocalDate().toEpochDay());
142 if (cmp == 0) {
143 cmp = Long.compare(dateTime1.toLocalTime().toNanoOfDay(), dateTime2.toLocalTime().toNanoOfDay());
144 }
145 return cmp;
146 };
147 /**
148 * ChronoZonedDateTime order constant.
149 */
150 static final Comparator<ChronoZonedDateTime<?>> INSTANT_ORDER =
151 (Comparator<ChronoZonedDateTime<?>> & Serializable) (dateTime1, dateTime2) -> {
152 int cmp = Long.compare(dateTime1.toEpochSecond(), dateTime2.toEpochSecond());
153 if (cmp == 0) {
154 cmp = Long.compare(dateTime1.toLocalTime().getNano(), dateTime2.toLocalTime().getNano());
155 }
156 return cmp;
157 };
158
159 /**
160 * Map of available calendars by ID.
161 */
162 private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_ID = new ConcurrentHashMap<>();
163 /**
164 * Map of available calendars by calendar type.
165 */
166 private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_TYPE = new ConcurrentHashMap<>();
167
168 /**
169 * Register a Chronology by its ID and type for lookup by {@link #of(String)}.
170 * Chronologies must not be registered until they are completely constructed.
171 * Specifically, not in the constructor of Chronology.
172 *
173 * @param chrono the chronology to register; not null
174 * @return the already registered Chronology if any, may be null
175 */
176 static Chronology registerChrono(Chronology chrono) {
177 return registerChrono(chrono, chrono.getId());
178 }
179
|
110 * <p>
111 * The main date and time API is built on the ISO calendar system.
112 * The chronology operates behind the scenes to represent the general concept of a calendar system.
113 * <p>
114 * See {@link Chronology} for more details.
115 *
116 * @implSpec
117 * This class is separated from the {@code Chronology} interface so that the static methods
118 * are not inherited. While {@code Chronology} can be implemented directly, it is strongly
119 * recommended to extend this abstract class instead.
120 * <p>
121 * This class must be implemented with care to ensure other classes operate correctly.
122 * All implementations that can be instantiated must be final, immutable and thread-safe.
123 * Subclasses should be Serializable wherever possible.
124 *
125 * @since 1.8
126 */
127 public abstract class AbstractChronology implements Chronology {
128
129 /**
130 * Map of available calendars by ID.
131 */
132 private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_ID = new ConcurrentHashMap<>();
133 /**
134 * Map of available calendars by calendar type.
135 */
136 private static final ConcurrentHashMap<String, Chronology> CHRONOS_BY_TYPE = new ConcurrentHashMap<>();
137
138 /**
139 * Register a Chronology by its ID and type for lookup by {@link #of(String)}.
140 * Chronologies must not be registered until they are completely constructed.
141 * Specifically, not in the constructor of Chronology.
142 *
143 * @param chrono the chronology to register; not null
144 * @return the already registered Chronology if any, may be null
145 */
146 static Chronology registerChrono(Chronology chrono) {
147 return registerChrono(chrono, chrono.getId());
148 }
149
|