@Test public void renderTest() { assertEquals("0", BytesParser.render(0)); assertEquals("17", BytesParser.render(17)); assertEquals("1025", BytesParser.render(1025)); assertEquals("4k", BytesParser.render(4L << 10)); assertEquals("16M", BytesParser.render(16L << 20)); assertEquals("389G", BytesParser.render(389L << 30)); assertEquals("2020T", BytesParser.render(2020L << 40)); assertEquals("5P", BytesParser.render(5L << 50)); assertEquals("6E", BytesParser.render(6L << 60)); }
public static String render(long v) { return SIZE_MAP.entrySet().stream() .map(spec -> { return Optional.of(v) .filter(vv -> vv >= spec.getValue()) .filter(vv -> vv % spec.getValue() == 0) .map(vv -> vv / spec.getValue()) .map(vv -> SimpleMapEntry.create(vv, spec.getKey())); }) .flatMap(opt -> opt.map(Stream::of).orElseGet(Stream::empty)) .min(Comparator.comparing(Map.Entry::getKey)) .map(v_with_spec -> v_with_spec.getKey().toString() + v_with_spec.getValue().toString()) .orElseGet(() -> Long.toString(v)); }
BytesParser { public static String render(long v) { return SIZE_MAP.entrySet().stream() .map(spec -> { return Optional.of(v) .filter(vv -> vv >= spec.getValue()) .filter(vv -> vv % spec.getValue() == 0) .map(vv -> vv / spec.getValue()) .map(vv -> SimpleMapEntry.create(vv, spec.getKey())); }) .flatMap(opt -> opt.map(Stream::of).orElseGet(Stream::empty)) .min(Comparator.comparing(Map.Entry::getKey)) .map(v_with_spec -> v_with_spec.getKey().toString() + v_with_spec.getValue().toString()) .orElseGet(() -> Long.toString(v)); } }
BytesParser { public static String render(long v) { return SIZE_MAP.entrySet().stream() .map(spec -> { return Optional.of(v) .filter(vv -> vv >= spec.getValue()) .filter(vv -> vv % spec.getValue() == 0) .map(vv -> vv / spec.getValue()) .map(vv -> SimpleMapEntry.create(vv, spec.getKey())); }) .flatMap(opt -> opt.map(Stream::of).orElseGet(Stream::empty)) .min(Comparator.comparing(Map.Entry::getKey)) .map(v_with_spec -> v_with_spec.getKey().toString() + v_with_spec.getValue().toString()) .orElseGet(() -> Long.toString(v)); }  }
BytesParser { public static String render(long v) { return SIZE_MAP.entrySet().stream() .map(spec -> { return Optional.of(v) .filter(vv -> vv >= spec.getValue()) .filter(vv -> vv % spec.getValue() == 0) .map(vv -> vv / spec.getValue()) .map(vv -> SimpleMapEntry.create(vv, spec.getKey())); }) .flatMap(opt -> opt.map(Stream::of).orElseGet(Stream::empty)) .min(Comparator.comparing(Map.Entry::getKey)) .map(v_with_spec -> v_with_spec.getKey().toString() + v_with_spec.getValue().toString()) .orElseGet(() -> Long.toString(v)); }  static long parse(String s); static String render(long v); }
BytesParser { public static String render(long v) { return SIZE_MAP.entrySet().stream() .map(spec -> { return Optional.of(v) .filter(vv -> vv >= spec.getValue()) .filter(vv -> vv % spec.getValue() == 0) .map(vv -> vv / spec.getValue()) .map(vv -> SimpleMapEntry.create(vv, spec.getKey())); }) .flatMap(opt -> opt.map(Stream::of).orElseGet(Stream::empty)) .min(Comparator.comparing(Map.Entry::getKey)) .map(v_with_spec -> v_with_spec.getKey().toString() + v_with_spec.getValue().toString()) .orElseGet(() -> Long.toString(v)); }  static long parse(String s); static String render(long v); static final Map<Character, Long> SIZE_MAP; }