Skip to content

Instantly share code, notes, and snippets.

@asdf913
Last active April 3, 2024 23:49
Show Gist options
  • Save asdf913/ac9fe972bcccc3fe2113faf20da46c34 to your computer and use it in GitHub Desktop.
Save asdf913/ac9fe972bcccc3fe2113faf20da46c34 to your computer and use it in GitHub Desktop.
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;
//
}
}
@asdf913
Copy link
Author

asdf913 commented Apr 3, 2024

Required dependencies

<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
	<groupId>com.google.guava</groupId>
	<artifactId>guava</artifactId>
	<version>33.1.0-jre</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
<dependency>
	<groupId>org.apache.commons</groupId>
	<artifactId>commons-lang3</artifactId>
	<version>3.14.0</version>
</dependency>

@asdf913
Copy link
Author

asdf913 commented Apr 3, 2024

Purpose / 目的

Convert a Japanese Era into corresponding ligature
日本の年号を対応する合字に変換する

Reference

https://en.wikipedia.org/wiki/Ligature_(writing)

https://en.wikipedia.org/wiki/Japanese_era_name

Certain era names have specific characters assigned to them, for instance ㋿ for the Reiwa period, which can also be written as 令和. These are included in Unicode: Code points U+32FF (㋿), U+337B (㍻), U+337C (㍼), U+337D (㍽) and U+337E (㍾) are used for the Reiwa, Heisei, Shōwa, Taishō and Meiji eras, respectively.

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

Meiji  ㍾
Taisho ㍽
Showa  ㍼
Heisei ㍻
Reiwa  ㋿
PT0.4362886S
Meiji  ㍾
Taisho ㍽
Showa  ㍼
Heisei ㍻
Reiwa  ㋿
PT0.002274S

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment