Guest User

Untitled

a guest
Nov 15th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. package ex_timetranslator.bll;
  2.  
  3. import java.util.HashMap;
  4. import java.util.function.Supplier;
  5.  
  6. public class Service {
  7. // 時間変換用マップたち
  8. // 入力時間の単位[秒]用変換機
  9. private static HashMap<String, Supplier<String>> secTranslatorMap = new HashMap<>();
  10. // 入力時間の単位[分]用変換機
  11. private static HashMap<String, Supplier<String>> minTranslatorMap = new HashMap<>();
  12. // 入力単位別変換機格納用マップ
  13. private static HashMap<String, HashMap<String, Supplier<String>>> translatorsMap = new HashMap<>();
  14.  
  15. // キーワードから 分、秒を取得するためのマップ
  16. private static HashMap<String, String> unitStrMap = new HashMap<>();
  17.  
  18. // 入力時間を変換
  19. public static String transform(String unit, String format, int time)
  20. {
  21. // 初回アクセス時は各mapは空
  22. if(secTranslatorMap.isEmpty())
  23. {
  24. // 入力時間[秒] => xx時間xx分xx秒
  25. secTranslatorMap.put("hhmmss", () -> time/3600 + "時間" + (time%3600)/60 + "分" + (time%3600)%60 + "秒");
  26. // 入力時間[秒] => xx分xx秒
  27. secTranslatorMap.put("mmss", () -> time/60 + "分" + time%60 + "秒");
  28. // 入力時間[秒] => xx秒
  29. secTranslatorMap.put("ss", () -> time + "秒");
  30. }
  31.  
  32. if(minTranslatorMap.isEmpty())
  33. {
  34. // 入力時間[分] => xx時間xx分xx秒
  35. minTranslatorMap.put("hhmmss", () -> time/60 + "時間" + time%60 + "分0秒");
  36. // 入力時間[分] => xx分xx秒
  37. minTranslatorMap.put("hhmmss", () -> time + "分0秒");
  38. // 入力時間[分] => xx秒
  39. minTranslatorMap.put("hhmmss", () -> time*60 + "秒");
  40. }
  41.  
  42. if(translatorsMap.isEmpty())
  43. {
  44. // 入力時間の単位は[秒]
  45. translatorsMap.put("sec", secTranslatorMap);
  46. // 入力時間の単位は[分]
  47. translatorsMap.put("min", minTranslatorMap);
  48.  
  49. }
  50.  
  51.  
  52. // 変換
  53. return translatorsMap.get(unit).get(format).get();
  54. }
  55.  
  56. // 選択された時間の単位の日本語を取得
  57. public static String getUnitStr(String unit)
  58. {
  59. // 初回アクセス時はmapは空
  60. if(unitStrMap.isEmpty())
  61. {
  62. // 入力時間の単位[秒]
  63. unitStrMap.put("sec", "秒");
  64. // 入力時間の単位[分]
  65. unitStrMap.put("min", "分");
  66. }
  67.  
  68. return unitStrMap.get(unit);
  69. }
  70. }
Add Comment
Please, Sign In to add comment