/** * Creates an instance. * <p> * Use {@link DenseLocalDateDoubleTimeSeries#toBuilder()}. * * @param points the stream of points to initialize with */ LocalDateDoubleTimeSeriesBuilder(Stream<LocalDateDoublePoint> points) { points.forEach(pt -> put(pt.getDate(), pt.getValue())); }
/** * Puts the specified date/value point into this builder. * * @param point the point to be added * @return this builder */ public LocalDateDoubleTimeSeriesBuilder put(LocalDateDoublePoint point) { ArgChecker.notNull(point, "point"); put(point.getDate(), point.getValue()); return this; }
/** * Package protected factory method intended to be called * by the {@link LocalDateDoubleTimeSeriesBuilder}. As such * all the information passed is assumed to be consistent. * * @param startDate the earliest date included in the time-series * @param endDate the latest date included in the time-series * @param values stream holding the time-series points * @param dateCalculation the date calculation method to be used * @return a new time-series */ static LocalDateDoubleTimeSeries of( LocalDate startDate, LocalDate endDate, Stream<LocalDateDoublePoint> values, DenseTimeSeriesCalculation dateCalculation) { double[] points = new double[dateCalculation.calculatePosition(startDate, endDate) + 1]; Arrays.fill(points, Double.NaN); values.forEach(pt -> points[dateCalculation.calculatePosition(startDate, pt.getDate())] = pt.getValue()); return new DenseLocalDateDoubleTimeSeries(startDate, points, dateCalculation, true); }
@Override public LocalDateDoubleTimeSeries filter(ObjDoublePredicate<LocalDate> predicate) { Stream<LocalDateDoublePoint> filteredPoints = stream().filter(pt -> predicate.test(pt.getDate(), pt.getValue())); // As we may have changed the density of the series by filtering // go via the builder to get the best implementation return new LocalDateDoubleTimeSeriesBuilder(filteredPoints).build(); }
/** * Obtains the intersection of a pair of time series. * <p> * This returns a time-series with the intersection of the dates of the two inputs. * The operator is invoked to combine the values. * * @param other the time-series to combine with * @param mapper the function to be used to combine the values * @return a new time-series containing the dates in common between the * input series with their values combined together using the function */ public default LocalDateDoubleTimeSeries intersection(LocalDateDoubleTimeSeries other, DoubleBinaryOperator mapper) { ArgChecker.notNull(other, "other"); ArgChecker.notNull(mapper, "mapper"); return new LocalDateDoubleTimeSeriesBuilder() .putAll(stream() .filter(pt -> other.containsDate(pt.getDate())) .map(pt -> LocalDateDoublePoint.of( pt.getDate(), mapper.applyAsDouble(pt.getValue(), other.get(pt.getDate()).getAsDouble())))) .build(); }
/** * Partition the time-series into a pair of distinct series using a predicate. * <p> * Points in the time-series which match the predicate will be put into the first series, * whilst those points which do not match will be put into the second. * * @param predicate predicate used to test the points in the time-series * @return a {@code Pair} containing two time-series. The first is a series * made of all the points in this series which match the predicate. The * second is a series made of the points which do not match. */ public default Pair<LocalDateDoubleTimeSeries, LocalDateDoubleTimeSeries> partition( ObjDoublePredicate<LocalDate> predicate) { Map<Boolean, LocalDateDoubleTimeSeries> partitioned = stream() .collect( partitioningBy( pt -> predicate.test(pt.getDate(), pt.getValue()), LocalDateDoubleTimeSeries.collector())); return Pair.of(partitioned.get(true), partitioned.get(false)); }
public void test_of() { LocalDateDoublePoint test = LocalDateDoublePoint.of(DATE_2012_06_30, 1d); assertEquals(test.getDate(), DATE_2012_06_30); assertEquals(test.getValue(), 1d, TOLERANCE); }
/** * Merges the specified date/value point into this builder. * <p> * The operator is invoked if the date already exists. * * @param point the point to be added * @param operator the operator to use for merging * @return this builder */ public LocalDateDoubleTimeSeriesBuilder merge(LocalDateDoublePoint point, DoubleBinaryOperator operator) { ArgChecker.notNull(point, "point"); entries.merge(point.getDate(), point.getValue(), (a, b) -> operator.applyAsDouble(a, b)); return this; }
public void test_withValue() { LocalDateDoublePoint base = LocalDateDoublePoint.of(DATE_2012_06_30, 1d); LocalDateDoublePoint test = base.withValue(2d); assertEquals(test.getDate(), DATE_2012_06_30); assertEquals(test.getValue(), 2d, TOLERANCE); }
public void test_withDate() { LocalDateDoublePoint base = LocalDateDoublePoint.of(DATE_2012_06_30, 1d); LocalDateDoublePoint test = base.withDate(DATE_2012_06_29); assertEquals(test.getDate(), DATE_2012_06_29); assertEquals(test.getValue(), 1d, TOLERANCE); }