PercentileCalculator extends Function1D<double[], Double> { @Override public Double evaluate(final double[] x) { Validate.notNull(x, "x"); Validate.isTrue(x.length > 0, "x cannot be empty"); final int length = x.length; final double[] copy = Arrays.copyOf(x, length); Arrays.sort(copy); final double n = _percentile * (length - 1) + 1; if (Math.round(n) == 1) { return copy[0]; } if (Math.round(n) == length) { return copy[length - 1]; } final double d = n % 1; final int k = (int) Math.round(n - d); return copy[k - 1] + d * (copy[k] - copy[k - 1]); } PercentileCalculator(final double percentile); void setPercentile(final double percentile); @Override Double evaluate(final double[] x);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void testNullArray() { CALCULATOR.evaluate((double[]) null); }
@Test(expectedExceptions = IllegalArgumentException.class) public void testEmptyArray() { CALCULATOR.evaluate(new double[0]); }