/** * Obtains the union of a pair of time series. * <p> * This returns a time-series with the union of the dates of the two inputs. * When the same date occurs in both time-series, 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 union(LocalDateDoubleTimeSeries other, DoubleBinaryOperator mapper) { ArgChecker.notNull(other, "other"); ArgChecker.notNull(mapper, "mapper"); LocalDateDoubleTimeSeriesBuilder builder = new LocalDateDoubleTimeSeriesBuilder(stream()); other.stream().forEach(pt -> builder.merge(pt, mapper)); return builder.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_stream() { LocalDateDoubleTimeSeries base = LocalDateDoubleTimeSeries.builder().putAll(DATES_2010_12, VALUES_10_12).build(); Object[] test = base.stream().toArray(); assertEquals(test[0], LocalDateDoublePoint.of(DATE_2010_01_01, 10)); assertEquals(test[1], LocalDateDoublePoint.of(DATE_2011_01_01, 11)); assertEquals(test[2], LocalDateDoublePoint.of(DATE_2012_01_01, 12)); }
public void test_stream() { LocalDateDoubleTimeSeries base = LocalDateDoubleTimeSeries.builder().putAll(DATES_2010_12, VALUES_10_12).build(); Object[] test = base.stream().toArray(); assertEquals(test[0], LocalDateDoublePoint.of(DATE_2010_01_01, 10)); assertEquals(test[1], LocalDateDoublePoint.of(DATE_2011_01_01, 11)); assertEquals(test[2], LocalDateDoublePoint.of(DATE_2012_01_01, 12)); }
public void roundTrip() { Map<LocalDate, Double> in = ImmutableMap.<LocalDate, Double>builder() .put(dt(2015, 1, 1), 10d) // Thursday .put(dt(2015, 1, 2), 11d) // Friday .put(dt(2015, 1, 5), 12d) .put(dt(2015, 1, 6), 13d) .put(dt(2015, 1, 7), 14d) .put(dt(2015, 1, 8), 15d) // Thursday .put(dt(2015, 1, 9), 16d) // Friday .build(); LocalDateDoubleTimeSeries ts = LocalDateDoubleTimeSeries.builder().putAll(in).build(); Map<LocalDate, Double> out = ts.stream() .collect(Guavate.toImmutableMap(LocalDateDoublePoint::getDate, LocalDateDoublePoint::getValue)); assertThat(out).isEqualTo(in); }
public void test_immutableDatesViaBeanGet() { LocalDateDoubleTimeSeries test = LocalDateDoubleTimeSeries.builder().putAll(DATES_2010_12, VALUES_10_12).build(); LocalDate[] array = (LocalDate[]) ((Bean) test).property("dates").get(); array[0] = DATE_2012_01_01; LocalDateDoublePoint[] points = test.stream().toArray(LocalDateDoublePoint[]::new); assertEquals(points[0], LocalDateDoublePoint.of(DATE_2010_01_01, 10d)); assertEquals(points[1], LocalDateDoublePoint.of(DATE_2011_01_01, 11d)); assertEquals(points[2], LocalDateDoublePoint.of(DATE_2012_01_01, 12d)); }
/** * 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(); }
public void test_immutableValuesViaBeanGet() { LocalDateDoubleTimeSeries test = LocalDateDoubleTimeSeries.builder().putAll(DATES_2010_12, VALUES_10_12).build(); double[] array = (double[]) ((Bean) test).property("values").get(); array[0] = -1; LocalDateDoublePoint[] points = test.stream().toArray(LocalDateDoublePoint[]::new); assertEquals(points[0], LocalDateDoublePoint.of(DATE_2010_01_01, 10d)); assertEquals(points[1], LocalDateDoublePoint.of(DATE_2011_01_01, 11d)); assertEquals(points[2], LocalDateDoublePoint.of(DATE_2012_01_01, 12d)); }
public void test_immutableValuesViaBeanGet() { LocalDateDoubleTimeSeries test = LocalDateDoubleTimeSeries.builder().putAll(DATES_2015_1_WEEK, VALUES_1_WEEK).build(); double[] array = (double[]) ((Bean) test).property("points").get(); array[0] = -1; LocalDateDoublePoint[] points = test.stream().toArray(LocalDateDoublePoint[]::new); assertEquals(points[0], LocalDateDoublePoint.of(DATE_2015_01_05, 10d)); assertEquals(points[1], LocalDateDoublePoint.of(DATE_2015_01_06, 11d)); assertEquals(points[2], LocalDateDoublePoint.of(DATE_2015_01_07, 12d)); }
public void test_stream_withCollector() { LocalDateDoubleTimeSeries base = LocalDateDoubleTimeSeries.builder().putAll(DATES_2010_12, VALUES_10_12).build(); LocalDateDoubleTimeSeries test = base.stream() .map(point -> point.withValue(1.5d)) .collect(LocalDateDoubleTimeSeries.collector()); assertEquals(test.size(), 3); assertEquals(test.get(DATE_2010_01_01), OptionalDouble.of(1.5)); assertEquals(test.get(DATE_2011_01_01), OptionalDouble.of(1.5)); assertEquals(test.get(DATE_2012_01_01), OptionalDouble.of(1.5)); }
public void test_immutableViaBeanBuilder() { LocalDate[] dates = {DATE_2010_01_01, DATE_2011_01_01, DATE_2012_01_01}; double[] values = {6, 5, 4}; BeanBuilder<? extends LocalDateDoubleTimeSeries> builder = SparseLocalDateDoubleTimeSeries.meta().builder(); builder.set("dates", dates); builder.set("values", values); LocalDateDoubleTimeSeries test = builder.build(); dates[0] = DATE_2012_01_01; values[0] = -1; LocalDateDoublePoint[] points = test.stream().toArray(LocalDateDoublePoint[]::new); assertEquals(points[0], LocalDateDoublePoint.of(DATE_2010_01_01, 6d)); assertEquals(points[1], LocalDateDoublePoint.of(DATE_2011_01_01, 5d)); assertEquals(points[2], LocalDateDoublePoint.of(DATE_2012_01_01, 4d)); }
public void test_stream_withCollector() { LocalDateDoubleTimeSeries base = LocalDateDoubleTimeSeries.builder().putAll(DATES_2010_12, VALUES_10_12).build(); LocalDateDoubleTimeSeries test = base.stream() .map(point -> point.withValue(1.5d)) .collect(LocalDateDoubleTimeSeries.collector()); assertEquals(test.size(), 3); assertEquals(test.get(DATE_2010_01_01), OptionalDouble.of(1.5)); assertEquals(test.get(DATE_2011_01_01), OptionalDouble.of(1.5)); assertEquals(test.get(DATE_2012_01_01), OptionalDouble.of(1.5)); }
public void test_putAll_stream() { Collection<LocalDate> dates = Arrays.asList(date(2013, 1, 1), date(2014, 1, 1)); Collection<Double> values = Doubles.asList(2d, 3d); LocalDateDoubleTimeSeries base = LocalDateDoubleTimeSeries.builder().putAll(dates, values).build(); LocalDateDoubleTimeSeriesBuilder test = LocalDateDoubleTimeSeries.builder(); test.put(date(2012, 1, 1), 0d); test.put(date(2013, 1, 1), 1d); test.putAll(base.stream()); assertEquals(test.get(date(2012, 1, 1)), OptionalDouble.of(0d)); assertEquals(test.get(date(2013, 1, 1)), OptionalDouble.of(2d)); assertEquals(test.get(date(2014, 1, 1)), OptionalDouble.of(3d)); }