NumberLibrary { public static <T extends Number & Comparable<T>> double median(Collection<? extends T> values) { if (values.isEmpty()) { return 0.0; } int size = values.size(); int median = size / 2; List<? extends T> ordered = sorted(values); double medianValue = ordered.get(median).doubleValue(); if (size % 2 == 0) { medianValue = (medianValue + ordered.get(median - 1).doubleValue()) / 2.0; } return medianValue; }  static Function<String, Integer> methodParseInteger(); static int ifNegative(int number, int ifNegative); static int ifLessThan(int bound, int number, int ifLess); static int ifGreaterThan(int bound, int number, int ifGreater); static int bounded(int min, int max, int target); static int sumInts(Iterable<Integer> values); static long sumLongs(Iterable<Long> values); static double sum(Iterable<? extends Number> values); static double mean(Collection<? extends Number> values); static double median(Collection<? extends T> values);  }
@Test public void medianValueOfACollection() { Collection<Integer> empty = asList(); assertTrue(0.0 == median(empty)); assertTrue(92.0 == median(asList(92))); assertTrue(96.0 == median(asList(92, 100))); assertTrue(3.0 == median(asList(1, 2, 3, 4, 5))); assertTrue(3.5 == median(asList(1, 2, 3, 4, 5, 6))); assertTrue(21.0 == median(asList(400, 500, 19, 21, 3))); assertTrue(20.0 == median(asList(400, 500, 19, 21, 3, 0))); }