|
| 1 | +package guepardoapps.timext.java.extensions; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.Locale; |
| 5 | + |
| 6 | +public class LongExtensions { |
| 7 | + public static String formatMilliseconds(long milliseconds) { |
| 8 | + ArrayList<Tuple<String, Tuple<Long, Integer>>> dictionary = new ArrayList<>(); |
| 9 | + dictionary.add(new Tuple<>("week", new Tuple<>(7 * 24 * 60 * 60 * 1000L, Integer.MAX_VALUE))); |
| 10 | + dictionary.add(new Tuple<>("day", new Tuple<>(24 * 60 * 60 * 1000L, 7))); |
| 11 | + dictionary.add(new Tuple<>("hour", new Tuple<>(60 * 60 * 1000L, 24))); |
| 12 | + dictionary.add(new Tuple<>("minute", new Tuple<>(60 * 1000L, 60))); |
| 13 | + dictionary.add(new Tuple<>("second", new Tuple<>(1000L, 60))); |
| 14 | + dictionary.add(new Tuple<>("millisecond", new Tuple<>(1L, 1000))); |
| 15 | + |
| 16 | + ArrayList<String> arrayList = new ArrayList<>(); |
| 17 | + dictionary.forEach(value -> { |
| 18 | + long testValue = ((milliseconds / value.second.first) % value.second.second); |
| 19 | + if (testValue > 1) { |
| 20 | + arrayList.add(String.format(Locale.getDefault(), "%d %ss", testValue, value.first)); |
| 21 | + } else if (testValue > 0) { |
| 22 | + arrayList.add(String.format(Locale.getDefault(), "%d %s", testValue, value.first)); |
| 23 | + } |
| 24 | + }); |
| 25 | + |
| 26 | + if (arrayList.size() > 0) { |
| 27 | + return String.join(", ", arrayList); |
| 28 | + } else { |
| 29 | + return "0 milliseconds"; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static String formatSeconds(long seconds) { |
| 34 | + ArrayList<Tuple<String, Tuple<Long, Integer>>> dictionary = new ArrayList<>(); |
| 35 | + dictionary.add(new Tuple<>("week", new Tuple<>(7 * 24 * 60 * 60L, Integer.MAX_VALUE))); |
| 36 | + dictionary.add(new Tuple<>("day", new Tuple<>(24 * 60 * 60L, 7))); |
| 37 | + dictionary.add(new Tuple<>("hour", new Tuple<>(60 * 60L, 24))); |
| 38 | + dictionary.add(new Tuple<>("minute", new Tuple<>(60L, 60))); |
| 39 | + dictionary.add(new Tuple<>("second", new Tuple<>(1L, 60))); |
| 40 | + |
| 41 | + ArrayList<String> arrayList = new ArrayList<>(); |
| 42 | + dictionary.forEach(value -> { |
| 43 | + long testValue = ((seconds / value.second.first) % value.second.second); |
| 44 | + if (testValue > 1) { |
| 45 | + arrayList.add(String.format(Locale.getDefault(), "%d %ss", testValue, value.first)); |
| 46 | + } else if (testValue > 0) { |
| 47 | + arrayList.add(String.format(Locale.getDefault(), "%d %s", testValue, value.first)); |
| 48 | + } |
| 49 | + }); |
| 50 | + |
| 51 | + if (arrayList.size() > 0) { |
| 52 | + return String.join(", ", arrayList); |
| 53 | + } else { |
| 54 | + return LongExtensions.formatMilliseconds(1000* seconds); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + public static String formatMinutes(long minutes) { |
| 59 | + ArrayList<Tuple<String, Tuple<Long, Integer>>> dictionary = new ArrayList<>(); |
| 60 | + dictionary.add(new Tuple<>("week", new Tuple<>(7 * 24 * 60L, Integer.MAX_VALUE))); |
| 61 | + dictionary.add(new Tuple<>("day", new Tuple<>(24 * 60L, 7))); |
| 62 | + dictionary.add(new Tuple<>("hour", new Tuple<>(60L, 24))); |
| 63 | + dictionary.add(new Tuple<>("minute", new Tuple<>(1L, 60))); |
| 64 | + |
| 65 | + ArrayList<String> arrayList = new ArrayList<>(); |
| 66 | + dictionary.forEach(value -> { |
| 67 | + long testValue = ((minutes / value.second.first) % value.second.second); |
| 68 | + if (testValue > 1) { |
| 69 | + arrayList.add(String.format(Locale.getDefault(), "%d %ss", testValue, value.first)); |
| 70 | + } else if (testValue > 0) { |
| 71 | + arrayList.add(String.format(Locale.getDefault(), "%d %s", testValue, value.first)); |
| 72 | + } |
| 73 | + }); |
| 74 | + |
| 75 | + if (arrayList.size() > 0) { |
| 76 | + return String.join(", ", arrayList); |
| 77 | + } else { |
| 78 | + return LongExtensions.formatSeconds(60 * minutes); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + public static String formatHours(long hours) { |
| 83 | + ArrayList<Tuple<String, Tuple<Long, Integer>>> dictionary = new ArrayList<>(); |
| 84 | + dictionary.add(new Tuple<>("week", new Tuple<>(7 * 24L, Integer.MAX_VALUE))); |
| 85 | + dictionary.add(new Tuple<>("day", new Tuple<>(24L, 7))); |
| 86 | + dictionary.add(new Tuple<>("hour", new Tuple<>(1L, 24))); |
| 87 | + |
| 88 | + ArrayList<String> arrayList = new ArrayList<>(); |
| 89 | + dictionary.forEach(value -> { |
| 90 | + long testValue = ((hours / value.second.first) % value.second.second); |
| 91 | + if (testValue > 1) { |
| 92 | + arrayList.add(String.format(Locale.getDefault(), "%d %ss", testValue, value.first)); |
| 93 | + } else if (testValue > 0) { |
| 94 | + arrayList.add(String.format(Locale.getDefault(), "%d %s", testValue, value.first)); |
| 95 | + } |
| 96 | + }); |
| 97 | + |
| 98 | + if (arrayList.size() > 0) { |
| 99 | + return String.join(", ", arrayList); |
| 100 | + } else { |
| 101 | + return LongExtensions.formatMinutes(60 * hours); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + private static class Tuple<T, K> { |
| 106 | + final T first; |
| 107 | + final K second; |
| 108 | + |
| 109 | + Tuple(T first, K second) { |
| 110 | + this.first = first; |
| 111 | + this.second = second; |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments