MaxValueReducer implements ValueReducer { @Override public Double reduce(List<Double> values) { if(values == null || values.isEmpty()) { return null; } Stream<Double> stream = StreamSupport.stream(values.spliterator(), true); if(stream.allMatch(o -> o == null)) { stream.close(); return null; } stream.close(); double max = Double.NEGATIVE_INFINITY; for (Double value : values) { if (value == null) { continue; } double candidate = value; if (candidate > max) { max = candidate; } } return max; }  @Override Double reduce(List<Double> values); @Override String name();  }
@Test public void reduce_shouldReturnNullWhenZeroValues() { MaxValueReducer r = new MaxValueReducer(); assertThat(r.reduce(new ArrayList<Double>()), is(nullValue())); }
@Test public void reduce_shouldReturnMaxWhenSingleValue() { MaxValueReducer r = new MaxValueReducer(); assertThat(r.reduce(Arrays.asList(1.0)), equalTo(1.0)); }
@Test public void reduce_shouldReturnMaxWithMultipleDoubles() { MaxValueReducer r = new MaxValueReducer(); assertThat(r.reduce(Arrays.asList(5.0, 1.0, 4.0)), equalTo(5.0)); }
@Test public void reduce_shouldReturnMaxWithNullValue() { MaxValueReducer r = new MaxValueReducer(); assertThat(r.reduce(Arrays.asList(null, 5.0, 1.0, 4.0)), equalTo(5.0)); }