|
9 | 9 |
|
10 | 10 | package com.ibm.icu.util; |
11 | 11 |
|
| 12 | +import java.util.BitSet; |
12 | 13 | import java.util.Date; |
13 | | -import java.util.HashSet; |
14 | 14 | import java.util.Locale; |
15 | 15 | import java.util.Set; |
16 | 16 |
|
@@ -95,22 +95,32 @@ public class PersianCalendar extends Calendar { |
95 | 95 |
|
96 | 96 | private static final int PERSIAN_EPOCH = 1948320; |
97 | 97 |
|
98 | | - private static final int NON_LEAP_YEARS[] = { |
99 | | - 1502, 1601, 1634, 1667, 1700, 1733, 1766, 1799, 1832, 1865, 1898, 1931, 1964, 1997, 2030, 2059, |
100 | | - 2063, 2096, 2129, 2158, 2162, 2191, 2195, 2224, 2228, 2257, 2261, 2290, 2294, 2323, 2327, 2356, |
101 | | - 2360, 2389, 2393, 2422, 2426, 2455, 2459, 2488, 2492, 2521, 2525, 2554, 2558, 2587, 2591, 2620, |
102 | | - 2624, 2653, 2657, 2686, 2690, 2719, 2723, 2748, 2752, 2756, 2781, 2785, 2789, 2818, 2822, 2847, |
103 | | - 2851, 2855, 2880, 2884, 2888, 2913, 2917, 2921, 2946, 2950, 2954, 2979, 2983, 2987, |
104 | | - }; |
| 98 | + private static final class NonLeapYears { |
| 99 | + private static final int NON_LEAP_YEARS[] = { |
| 100 | + 1502, 1601, 1634, 1667, 1700, 1733, 1766, 1799, 1832, 1865, 1898, 1931, 1964, 1997, 2030, 2059, |
| 101 | + 2063, 2096, 2129, 2158, 2162, 2191, 2195, 2224, 2228, 2257, 2261, 2290, 2294, 2323, 2327, 2356, |
| 102 | + 2360, 2389, 2393, 2422, 2426, 2455, 2459, 2488, 2492, 2521, 2525, 2554, 2558, 2587, 2591, 2620, |
| 103 | + 2624, 2653, 2657, 2686, 2690, 2719, 2723, 2748, 2752, 2756, 2781, 2785, 2789, 2818, 2822, 2847, |
| 104 | + 2851, 2855, 2880, 2884, 2888, 2913, 2917, 2921, 2946, 2950, 2954, 2979, 2983, 2987, |
| 105 | + }; |
| 106 | + private int minYear = NON_LEAP_YEARS[0]; |
| 107 | + private int maxYear = NON_LEAP_YEARS[NON_LEAP_YEARS.length - 1]; |
| 108 | + private BitSet offsetYears; |
| 109 | + |
| 110 | + public NonLeapYears() { |
| 111 | + offsetYears = new BitSet(maxYear - minYear + 1); |
| 112 | + for (int nonLeap : NON_LEAP_YEARS) { |
| 113 | + offsetYears.set(nonLeap - minYear); |
| 114 | + } |
| 115 | + } |
105 | 116 |
|
106 | | - private static Set<Integer> LEAP_CORRECTION = null; |
107 | | - { |
108 | | - Set<Integer> prefab = new HashSet<Integer>(NON_LEAP_YEARS.length); |
109 | | - for (int nonLeap : NON_LEAP_YEARS) { |
110 | | - prefab.add(nonLeap); |
| 117 | + public boolean contains(int year) { |
| 118 | + return minYear <= year && year <= maxYear && offsetYears.get(year - minYear); |
111 | 119 | } |
112 | | - LEAP_CORRECTION = prefab; |
113 | 120 | } |
| 121 | + |
| 122 | + private static NonLeapYears LEAP_CORRECTION = new NonLeapYears(); |
| 123 | + |
114 | 124 | //------------------------------------------------------------------------- |
115 | 125 | // Constructors... |
116 | 126 | //------------------------------------------------------------------------- |
@@ -330,10 +340,10 @@ protected int handleGetLimit(int field, int limitType) { |
330 | 340 | */ |
331 | 341 | private final static boolean isLeapYear(int year) |
332 | 342 | { |
333 | | - if (year >= NON_LEAP_YEARS[0] && LEAP_CORRECTION.contains(year)) { |
| 343 | + if (LEAP_CORRECTION.contains(year)) { |
334 | 344 | return false; |
335 | 345 | } |
336 | | - if (year > NON_LEAP_YEARS[0] && LEAP_CORRECTION.contains(year-1)) { |
| 346 | + if (LEAP_CORRECTION.contains(year-1)) { |
337 | 347 | return true; |
338 | 348 | } |
339 | 349 | int[] remainder = new int[1]; |
@@ -426,7 +436,7 @@ protected int handleGetExtendedYear() { |
426 | 436 |
|
427 | 437 | private static long firstJulianOfYear(int year) { |
428 | 438 | long julianDay = 365L * (year - 1L) + floorDivide(8L * year + 21, 33L); |
429 | | - if (year > NON_LEAP_YEARS[0] && LEAP_CORRECTION.contains(year-1)) { |
| 439 | + if (LEAP_CORRECTION.contains(year-1)) { |
430 | 440 | julianDay--; |
431 | 441 | } |
432 | 442 | return julianDay; |
@@ -458,7 +468,7 @@ protected void handleComputeFields(int julianDay) { |
458 | 468 | long farvardin1 = firstJulianOfYear(year); |
459 | 469 |
|
460 | 470 | dayOfYear = (int)(daysSinceEpoch - farvardin1); // 0-based |
461 | | - if (dayOfYear == 365 && year >= NON_LEAP_YEARS[0] && LEAP_CORRECTION.contains(year)) { |
| 471 | + if (dayOfYear == 365 && LEAP_CORRECTION.contains(year)) { |
462 | 472 | year++; |
463 | 473 | dayOfYear = 0; |
464 | 474 | } |
|
0 commit comments