Last active
April 3, 2024 23:49
-
-
Save asdf913/ac9fe972bcccc3fe2113faf20da46c34 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.time.chrono.JapaneseEra; | |
import java.time.format.TextStyle; | |
import java.util.ArrayList; | |
import java.util.Arrays; | |
import java.util.LinkedHashMap; | |
import java.util.List; | |
import java.util.Locale; | |
import java.util.Map; | |
import java.util.Objects; | |
import org.apache.commons.lang3.ArrayUtils; | |
import org.apache.commons.lang3.ObjectUtils; | |
import org.apache.commons.lang3.StringUtils; | |
import com.google.common.base.Stopwatch; | |
public class JapaneseEraToLigature { | |
private static Map<JapaneseEra, Character> MAP = null; | |
private static char[] SQUARE_ERA_NAME_CHARACTERS = null; | |
public static void main(final String[] args) { | |
// | |
final JapaneseEra[] jes = JapaneseEra.values(); | |
// | |
final int maxDisplayNameLength = Arrays.stream(JapaneseEra.values()) | |
.mapToInt(x -> StringUtils.length(x.getDisplayName(TextStyle.FULL, Locale.ENGLISH))).max().orElse(0); | |
// | |
final Stopwatch stopWatch = Stopwatch.createStarted(); | |
// | |
Arrays.stream(jes).forEachOrdered(je -> { | |
System.out.println(StringUtils.rightPad(Objects.toString(je), maxDisplayNameLength) + " " + toLigature(je)); | |
}); | |
// | |
System.out.println(stopWatch.elapsed()); | |
// | |
stopWatch.reset().start(); | |
// | |
Arrays.stream(jes).forEachOrdered(je -> { | |
System.out.println(StringUtils.rightPad(Objects.toString(je), maxDisplayNameLength) + " " + toLigature(je)); | |
}); | |
// | |
System.out.println(stopWatch.elapsed()); | |
// | |
} | |
private static Character toLigature(final JapaneseEra je) { | |
// | |
if (MAP == null) { | |
// | |
MAP = new LinkedHashMap<>(); | |
// | |
} // if | |
// | |
if (MAP.containsKey(je)) { | |
// | |
return MAP.get(je); | |
// | |
} // if | |
// | |
Character c = null; | |
// | |
if (SQUARE_ERA_NAME_CHARACTERS == null) { | |
// | |
List<Character> cs = null; | |
// | |
for (int i = Character.MIN_CODE_POINT; i <= Character.MAX_CODE_POINT; i++) { | |
// | |
if (!StringUtils.startsWith(Character.getName(i), "SQUARE ERA NAME") | |
|| (cs = ObjectUtils.getIfNull(cs, ArrayList::new)) == null) { | |
// | |
continue; | |
// | |
} // if | |
// | |
if (!cs.contains(c = Character.valueOf((char) i))) { | |
// | |
cs.add(c); | |
// | |
} // if | |
// | |
} // for | |
// | |
SQUARE_ERA_NAME_CHARACTERS = ArrayUtils.toPrimitive(cs.toArray(new Character[cs.size()])); | |
// | |
} // if | |
// | |
Map<JapaneseEra, Integer> jeMap = null; | |
// | |
for (int i = 0; SQUARE_ERA_NAME_CHARACTERS != null && i < SQUARE_ERA_NAME_CHARACTERS.length; i++) { | |
// | |
if (StringUtils | |
.isBlank(StringUtils.getCommonPrefix( | |
StringUtils.trim(StringUtils.substringAfter( | |
Character.getName((int) SQUARE_ERA_NAME_CHARACTERS[i]), "SQUARE ERA NAME")), | |
StringUtils.upperCase(je.getDisplayName(TextStyle.FULL, Locale.ENGLISH)))) | |
|| (jeMap = ObjectUtils.getIfNull(jeMap, LinkedHashMap::new)) == null) { | |
// | |
continue; | |
// | |
} // if | |
// | |
if (!jeMap.containsKey(je)) { | |
// | |
jeMap.put(je, (int) SQUARE_ERA_NAME_CHARACTERS[i]); | |
// | |
} // if | |
// | |
} // for | |
// | |
if (jeMap == null) { | |
// | |
for (int i = Character.MIN_CODE_POINT; i <= Character.MAX_CODE_POINT; i++) { | |
// | |
if (StringUtils | |
.isBlank(StringUtils.getCommonPrefix( | |
StringUtils.trim(StringUtils.substringAfter(Character.getName(i), "SQUARE ERA NAME")), | |
StringUtils.upperCase(je.getDisplayName(TextStyle.FULL, Locale.ENGLISH)))) | |
|| (jeMap = ObjectUtils.getIfNull(jeMap, LinkedHashMap::new)) == null) { | |
// | |
continue; | |
// | |
} // if | |
// | |
if (!jeMap.containsKey(je)) { | |
// | |
jeMap.put(je, Integer.valueOf(i)); | |
// | |
} // if | |
// | |
} // for | |
// | |
} // if | |
// | |
if (jeMap == null || jeMap.isEmpty()) { | |
// | |
return null; | |
// | |
} else if (jeMap.size() > 1) { | |
// | |
throw new IllegalStateException(); | |
// | |
} // if | |
// | |
MAP.put(je, c = Character.valueOf((char) (new ArrayList<Integer>(jeMap.values()).get(0)).intValue())); | |
// | |
return c; | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Purpose / 目的
Reference
https://en.wikipedia.org/wiki/Ligature_(writing)
https://en.wikipedia.org/wiki/Japanese_era_name
https://ja.wikipedia.org/wiki/%E5%85%83%E5%8F%B7%E4%B8%80%E8%A6%A7_(%E6%97%A5%E6%9C%AC)
Output Sample