MedianAggregatorStrategy extends AbstractAggregatorStrategy { float getMedian(Number[] bandValues) { if (bandValues == null || bandValues.length == 0) { return Float.NaN; } Number[] values = bandValues.clone(); Arrays.sort(values); if (values.length % 2 == 1) { return values[values.length / 2].floatValue(); } final float leftMedian = values[values.length / 2 - 1].floatValue(); final float rightMedian = values[values.length / 2].floatValue(); return (leftMedian + rightMedian) / 2; }  @Override Number[] getValues(Record record, int rasterIndex); @Override String[] getSuffixes();  }
@Test public void testGetValueForOddNumberOfIntegerBandValues() { Number[] oddNumberOfIntegerBandValues = new Integer[]{1, 1, 2, 3, 5, 8, 13, 21, 34}; assertEquals(5F, strategy.getMedian(oddNumberOfIntegerBandValues), 0.0001); }
@Test public void testGetValueForEvenNumberOfIntegerBandValues() { Number[] evenNumberOfIntegerBandValues = new Integer[]{1, 2, 3, 5, 8, 13}; assertEquals(4F, strategy.getMedian(evenNumberOfIntegerBandValues), 0.0001); }
@Test public void testGetValueForOddNumberOfFloatBandValues() { Number[] oddNumberOfFloatBandValues = new Float[]{2.5f, 3.5f, 5.5f, 8.5f, 13.5f, 21.5f, 34.5f}; assertEquals(8.5F, strategy.getMedian(oddNumberOfFloatBandValues), 0.0001); }
@Test public void testGetValueForEvenNumberOfFloatBandValues() { Number[] evenNumberOfFloatBandValues = new Float[]{3.5f, 5.5f, 8.5f, 13.5f}; assertEquals(7F, strategy.getMedian(evenNumberOfFloatBandValues), 0.0001); }
@Test public void testGetValuesForOneValue() { Number[] evenNumberOfFloatBandValues = new Float[]{3.5f}; assertEquals(3.5F, strategy.getMedian(evenNumberOfFloatBandValues), 0.0001); }