Skip to content

Commit 2e57f07

Browse files
committed
ICU-22736 Address Markus' feedback
1 parent 73956e9 commit 2e57f07

File tree

2 files changed

+30
-20
lines changed

2 files changed

+30
-20
lines changed

icu4c/source/i18n/persncal.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ static const int32_t kPersianCalendarLimits[UCAL_FIELD_COUNT][4] = {
6767

6868
namespace { // anonymous
6969

70-
static icu::UnicodeSet *gLeapCorrection = nullptr;
70+
static const icu::UnicodeSet *gLeapCorrection = nullptr;
7171
static icu::UInitOnce gCorrectionInitOnce {};
7272
static int32_t gMinCorrection;
7373
} // namespace
@@ -96,7 +96,7 @@ static void U_CALLCONV initLeapCorrection() {
9696
for (auto year : nonLeapYears) {
9797
prefab.add(year);
9898
}
99-
gLeapCorrection = prefab.cloneAsThawed();
99+
gLeapCorrection = prefab.cloneAsThawed()->freeze();
100100
ucln_i18n_registerCleanup(UCLN_I18N_PERSIAN_CALENDAR, calendar_persian_cleanup);
101101
}
102102
const icu::UnicodeSet* getLeapCorrection() {

icu4j/main/core/src/main/java/com/ibm/icu/util/PersianCalendar.java

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
package com.ibm.icu.util;
1111

12+
import java.util.BitSet;
1213
import java.util.Date;
13-
import java.util.HashSet;
1414
import java.util.Locale;
1515
import java.util.Set;
1616

@@ -95,22 +95,32 @@ public class PersianCalendar extends Calendar {
9595

9696
private static final int PERSIAN_EPOCH = 1948320;
9797

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+
}
105116

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);
111119
}
112-
LEAP_CORRECTION = prefab;
113120
}
121+
122+
private static NonLeapYears LEAP_CORRECTION = new NonLeapYears();
123+
114124
//-------------------------------------------------------------------------
115125
// Constructors...
116126
//-------------------------------------------------------------------------
@@ -330,10 +340,10 @@ protected int handleGetLimit(int field, int limitType) {
330340
*/
331341
private final static boolean isLeapYear(int year)
332342
{
333-
if (year >= NON_LEAP_YEARS[0] && LEAP_CORRECTION.contains(year)) {
343+
if (LEAP_CORRECTION.contains(year)) {
334344
return false;
335345
}
336-
if (year > NON_LEAP_YEARS[0] && LEAP_CORRECTION.contains(year-1)) {
346+
if (LEAP_CORRECTION.contains(year-1)) {
337347
return true;
338348
}
339349
int[] remainder = new int[1];
@@ -426,7 +436,7 @@ protected int handleGetExtendedYear() {
426436

427437
private static long firstJulianOfYear(int year) {
428438
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)) {
430440
julianDay--;
431441
}
432442
return julianDay;
@@ -458,7 +468,7 @@ protected void handleComputeFields(int julianDay) {
458468
long farvardin1 = firstJulianOfYear(year);
459469

460470
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)) {
462472
year++;
463473
dayOfYear = 0;
464474
}

0 commit comments

Comments
 (0)