Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ex_timetranslator.bll;
- import java.util.HashMap;
- import java.util.function.Supplier;
- public class Service {
- // 時間変換用マップたち
- // 入力時間の単位[秒]用変換機
- private static HashMap<String, Supplier<String>> secTranslatorMap = new HashMap<>();
- // 入力時間の単位[分]用変換機
- private static HashMap<String, Supplier<String>> minTranslatorMap = new HashMap<>();
- // 入力単位別変換機格納用マップ
- private static HashMap<String, HashMap<String, Supplier<String>>> translatorsMap = new HashMap<>();
- // キーワードから 分、秒を取得するためのマップ
- private static HashMap<String, String> unitStrMap = new HashMap<>();
- // 入力時間を変換
- public static String transform(String unit, String format, int time)
- {
- // 初回アクセス時は各mapは空
- if(secTranslatorMap.isEmpty())
- {
- // 入力時間[秒] => xx時間xx分xx秒
- secTranslatorMap.put("hhmmss", () -> time/3600 + "時間" + (time%3600)/60 + "分" + (time%3600)%60 + "秒");
- // 入力時間[秒] => xx分xx秒
- secTranslatorMap.put("mmss", () -> time/60 + "分" + time%60 + "秒");
- // 入力時間[秒] => xx秒
- secTranslatorMap.put("ss", () -> time + "秒");
- }
- if(minTranslatorMap.isEmpty())
- {
- // 入力時間[分] => xx時間xx分xx秒
- minTranslatorMap.put("hhmmss", () -> time/60 + "時間" + time%60 + "分0秒");
- // 入力時間[分] => xx分xx秒
- minTranslatorMap.put("hhmmss", () -> time + "分0秒");
- // 入力時間[分] => xx秒
- minTranslatorMap.put("hhmmss", () -> time*60 + "秒");
- }
- if(translatorsMap.isEmpty())
- {
- // 入力時間の単位は[秒]
- translatorsMap.put("sec", secTranslatorMap);
- // 入力時間の単位は[分]
- translatorsMap.put("min", minTranslatorMap);
- }
- // 変換
- return translatorsMap.get(unit).get(format).get();
- }
- // 選択された時間の単位の日本語を取得
- public static String getUnitStr(String unit)
- {
- // 初回アクセス時はmapは空
- if(unitStrMap.isEmpty())
- {
- // 入力時間の単位[秒]
- unitStrMap.put("sec", "秒");
- // 入力時間の単位[分]
- unitStrMap.put("min", "分");
- }
- return unitStrMap.get(unit);
- }
- }
Add Comment
Please, Sign In to add comment