Skip to content

Commit c865013

Browse files
committed
Use singleton pattern
1 parent cfdd481 commit c865013

File tree

2 files changed

+25
-23
lines changed

2 files changed

+25
-23
lines changed

unicodetools/src/main/java/org/unicode/text/UCD/GenerateConfusables.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,7 @@ static void generateDecompFile() throws IOException {
592592
out.println("");
593593
for (final UnicodeSetIterator usi = new UnicodeSetIterator(s); usi.next(); ) {
594594
final String source = usi.getString();
595-
final String target = getModifiedNFKD(source);
595+
final String target = ModifiedNFKD.normalize(source);
596596
writeSourceTargetLine(out, source, "N", target, value, ARROW);
597597
}
598598
// bf.showSetNames(out, s);
@@ -724,12 +724,12 @@ private static UnicodeSet getSkipNFKD() {
724724
}
725725
final String source = UTF16.valueOf(cp);
726726
final String mapped = NFKD.normalize(cp);
727-
String kmapped = getModifiedNFKD(source);
727+
String kmapped = ModifiedNFKD.normalize(source);
728728
if (!kmapped.equals(source) && !kmapped.equals(nfc)) {
729729
if (kmapped.startsWith(" ") || kmapped.startsWith("\u0640")) {
730730
if (DEBUG) System.out.println("?? " + DEFAULT_UCD.getCodeAndName(cp));
731731
if (DEBUG) System.out.println("\t" + DEFAULT_UCD.getCodeAndName(kmapped));
732-
kmapped = getModifiedNFKD(source); // for debugging
732+
kmapped = ModifiedNFKD.normalize(source); // for debugging
733733
}
734734
nfkdMap.put(cp, kmapped);
735735
}
@@ -2637,24 +2637,15 @@ private static String getReasonFromFilename(String type) {
26372637
return type.substring(dash + 1, period);
26382638
}
26392639

2640-
private static Normalizer modNFKD;
2641-
2642-
static String getModifiedNFKD(String cf) {
2643-
if (modNFKD == null) {
2644-
modNFKD = new Normalizer(UCD_Types.NFKD, Default.ucdVersion());
2645-
modNFKD.setSpacingSubstitute();
2646-
}
2647-
return modNFKD.normalize(cf);
2648-
}
2649-
2650-
private static Normalizer modNFKC;
2651-
2652-
static String getModifiedNFKC(String cf) {
2653-
if (modNFKC == null) {
2654-
modNFKC = new Normalizer(UCD_Types.NFKC, Default.ucdVersion());
2655-
modNFKC.setSpacingSubstitute();
2640+
private static final class ModifiedNFKD {
2641+
private static Normalizer INSTANCE;
2642+
static String normalize(String cf) {
2643+
if (INSTANCE == null) {
2644+
INSTANCE = new Normalizer(UCD_Types.NFKD, Default.ucdVersion());
2645+
INSTANCE.setSpacingSubstitute();
2646+
}
2647+
return ModifiedNFKD.INSTANCE.normalize(cf);
26562648
}
2657-
return modNFKC.normalize(cf);
26582649
}
26592650

26602651
static PrintWriter openAndWriteHeader(String dir, String filename, String title)

unicodetools/src/main/java/org/unicode/text/UCD/IdentifierInfo.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,17 @@ public static void main(String[] args) throws IOException {
138138
info.printIDNStuff();
139139
}
140140

141+
private static final class ModifiedNFKC {
142+
private static Normalizer INSTANCE;
143+
static String normalize(String cf) {
144+
if (INSTANCE == null) {
145+
INSTANCE = new Normalizer(UCD_Types.NFKC, Default.ucdVersion());
146+
INSTANCE.setSpacingSubstitute();
147+
}
148+
return ModifiedNFKC.INSTANCE.normalize(cf);
149+
}
150+
}
151+
141152
private IdentifierInfo() throws IOException {
142153
isCaseFolded = new UnicodeSet();
143154
for (int cp = 0; cp <= 0x10FFFF; ++cp) {
@@ -189,9 +200,9 @@ private IdentifierInfo() throws IOException {
189200
// the output set
190201
for (final UnicodeSetIterator usi = new UnicodeSetIterator(remainingInputSet1);
191202
usi.next(); ) {
192-
final String nss = GenerateConfusables.getModifiedNFKC(usi.getString());
203+
final String nss = ModifiedNFKC.normalize(usi.getString());
193204
final String cf = DEFAULT_UCD.getCase(nss, UCD_Types.FULL, UCD_Types.FOLD);
194-
final String cf2 = GenerateConfusables.getModifiedNFKC(cf);
205+
final String cf2 = ModifiedNFKC.normalize(cf);
195206
if (remainingOutputSet.containsAll(cf2)) {
196207
remainingInputSet.add(usi.codepoint);
197208
} else {
@@ -203,7 +214,7 @@ private IdentifierInfo() throws IOException {
203214
for (final UnicodeSetIterator usi = new UnicodeSetIterator(remainingInputSet);
204215
usi.next(); ) {
205216
final String ss = usi.getString();
206-
final String nss = GenerateConfusables.getModifiedNFKC(ss);
217+
final String nss = ModifiedNFKC.normalize(ss);
207218
final String cf = DEFAULT_UCD.getCase(ss, UCD_Types.FULL, UCD_Types.FOLD);
208219
if (DEBUG && (usi.codepoint == 0x2126 || usi.codepoint == 0x212B)) {
209220
System.out.println("check");

0 commit comments

Comments
 (0)