### Question:
SynchronizedVDoubleAggregator implements ReadFunction<VMultiDouble> { static <T extends Time> T closestElement(List<T> data, TimeInterval interval, Instant reference) { StringBuilder buffer = new StringBuilder(); T latest = null; long latestDistance = Long.MAX_VALUE; for (T value : data) { Instant newTime = value.getTimestamp(); if (log.isLoggable(Level.FINEST)) { buffer.append(newTime.getNano()).append(", "); } if (interval.contains(newTime)) { if (latest == null) { latest = value; latestDistance = Math.abs(reference.until(newTime, ChronoUnit.NANOS)); } else { long newDistance = Math.abs(reference.until(newTime, ChronoUnit.NANOS)); if (newDistance < latestDistance) { latest = value; latestDistance = newDistance; } } } } if (log.isLoggable(Level.FINEST)) { buffer.append("[").append(latest.getTimestamp().getNano()).append("|").append(reference.getNano()).append("]"); log.finest(buffer.toString()); } return latest; } @SuppressWarnings("unchecked")  SynchronizedVDoubleAggregator(List<String> names, List<ReadFunction<List<VDouble>>> collectors, Duration tolerance); @Override VMultiDouble readValue();  }### Answer:
@Test public void testClosestElement() { List<VDouble> data = new ArrayList<VDouble>(); Instant reference = Instant.now(); data.add(newVDouble(-2.0, newTime(reference.minus(ofMillis(5))))); data.add(newVDouble(-1.0, newTime(reference.minus(ofMillis(1))))); data.add(newVDouble(0.0, newTime(reference))); data.add(newVDouble(1.0, newTime(reference.plus(ofMillis(2))))); data.add(newVDouble(2.0, newTime(reference.plus(ofMillis(3))))); VDouble result = SynchronizedVDoubleAggregator.closestElement(data, TimeInterval.around(ofMillis(10), reference), reference); assertEquals(0.0, result.getValue(), 0.000001); }
@Test public void testClosestElement2() { List<VDouble> data = new ArrayList<VDouble>(); Instant reference = Instant.now(); data.add(newVDouble(-2.0, newTime(reference.minus(ofMillis(5))))); data.add(newVDouble(-1.0, newTime(reference.minus(ofMillis(1))))); data.add(newVDouble(1.0, newTime(reference.plus(ofMillis(2))))); data.add(newVDouble(2.0, newTime(reference.plus(ofMillis(3))))); VDouble result = SynchronizedVDoubleAggregator.closestElement(data, TimeInterval.around(ofMillis(10), reference), reference); assertEquals(-1.0, result.getValue(), 0.000001); }