private SparseLocalDateDoubleTimeSeries createSparseSeries() { return SparseLocalDateDoubleTimeSeries.of(entries.keySet(), entries.values()); }
@Override public SparseLocalDateDoubleTimeSeries build() { return new SparseLocalDateDoubleTimeSeries( dates, values); }
@Override public LocalDateDoubleTimeSeries tailSeries(int numPoints) { ArgChecker.notNegative(numPoints, "numPoints"); if (numPoints == 0) { return EMPTY; } else if (numPoints >= size()) { return this; } LocalDate[] datesArray = Arrays.copyOfRange(dates, size() - numPoints, size()); double[] valuesArray = Arrays.copyOfRange(values, size() - numPoints, size()); return createUnsafe(datesArray, valuesArray); }
/** * Obtains a time-series from matching arrays of dates and values. * <p> * The two arrays must be the same size and must be sorted from earliest to latest. * * @param dates the date list * @param values the value list * @return the time-series */ static SparseLocalDateDoubleTimeSeries of(Collection<LocalDate> dates, Collection<Double> values) { ArgChecker.noNulls(dates, "dates"); ArgChecker.noNulls(values, "values"); LocalDate[] datesArray = dates.toArray(new LocalDate[dates.size()]); double[] valuesArray = Doubles.toArray(values); validate(datesArray, valuesArray); return createUnsafe(datesArray, valuesArray); }
@Override public LocalDateDoubleTimeSeries subSeries(LocalDate startInclusive, LocalDate endExclusive) { ArgChecker.notNull(startInclusive, "startInclusive"); ArgChecker.notNull(endExclusive, "endExclusive"); if (endExclusive.isBefore(startInclusive)) { throw new IllegalArgumentException( "Invalid sub series, end before start: " + startInclusive + " to " + endExclusive); } // special case when this is empty or when the dates are the same if (isEmpty() || startInclusive.equals(endExclusive)) { return EMPTY; } // where in the array would start/end be (whether or not it's actually in the series) int startPos = Arrays.binarySearch(dates, startInclusive); startPos = startPos >= 0 ? startPos : -startPos - 1; int endPos = Arrays.binarySearch(dates, endExclusive); endPos = endPos >= 0 ? endPos : -endPos - 1; // create sub-series LocalDate[] timesArray = Arrays.copyOfRange(dates, startPos, endPos); double[] valuesArray = Arrays.copyOfRange(values, startPos, endPos); return createUnsafe(timesArray, valuesArray); }
@Override public Stream<LocalDateDoublePoint> stream() { return IntStream.range(0, size()).mapToObj(i -> LocalDateDoublePoint.of(dates[i], values[i])); }
@Override public LocalDateDoubleTimeSeries mapDates(Function<? super LocalDate, ? extends LocalDate> mapper) { ArgChecker.notNull(mapper, "mapper"); LocalDate[] dates = Arrays.stream(this.dates).map(mapper).toArray(size -> new LocalDate[size]); // Check the dates are still in ascending order after the mapping Arrays.stream(dates).reduce(this::checkAscending); return createUnsafe(dates, values); }
@Override public boolean containsDate(LocalDate date) { return (findDatePosition(date) >= 0); }
@Override public double getLatestValue() { if (isEmpty()) { throw new NoSuchElementException("Unable to return latest, time-series is empty"); } return values[values.length - 1]; }
/** * Returns a string representation of the time-series. * * @return the string */ @Override public String toString() { return stream() .map(LocalDateDoublePoint::toString) .collect(Collectors.joining(", ", "[", "]")); }
/** * Creates an instance, validating the supplied arrays. * <p> * The arrays are cloned as this constructor is called from Joda-Beans. * * @param dates the dates * @param values the values */ @ImmutableConstructor private SparseLocalDateDoubleTimeSeries(LocalDate[] dates, double[] values) { ArgChecker.noNulls(dates, "dates"); ArgChecker.notNull(values, "values"); validate(dates, values); this.dates = dates.clone(); this.values = values.clone(); }
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)); }
@Override public void forEach(ObjDoubleConsumer<LocalDate> action) { ArgChecker.notNull(action, "action"); for (int i = 0; i < size(); i++) { action.accept(dates[i], values[i]); } }
@Override public LocalDateDoubleTimeSeries mapValues(DoubleUnaryOperator mapper) { ArgChecker.notNull(mapper, "mapper"); return createUnsafe(dates, DoubleStream.of(values).map(mapper).toArray()); }
@Override public OptionalDouble get(LocalDate date) { int position = findDatePosition(date); return (position >= 0 ? OptionalDouble.of(values[position]) : OptionalDouble.empty()); }
@Override public LocalDate getLatestDate() { if (isEmpty()) { throw new NoSuchElementException("Unable to return latest, time-series is empty"); } return dates[dates.length - 1]; }
@Override public LocalDateDoubleTimeSeries headSeries(int numPoints) { ArgChecker.notNegative(numPoints, "numPoints"); if (numPoints == 0) { return EMPTY; } else if (numPoints >= size()) { return this; } LocalDate[] datesArray = Arrays.copyOfRange(dates, 0, numPoints); double[] valuesArray = Arrays.copyOfRange(values, 0, numPoints); return createUnsafe(datesArray, valuesArray); }
public void coverage() { List<LocalDate> dates = ImmutableList.of(DATE_2011_01_01, DATE_2011_06_01); List<Double> values = ImmutableList.of(1d, 2d); SparseLocalDateDoubleTimeSeries test = SparseLocalDateDoubleTimeSeries.of(dates, values); coverImmutableBean(test); List<LocalDate> dates2 = ImmutableList.of(DATE_2011_06_01, DATE_2012_01_01); List<Double> values2 = ImmutableList.of(2d, 3d); SparseLocalDateDoubleTimeSeries test2 = SparseLocalDateDoubleTimeSeries.of(dates2, values2); coverBeanEquals(test, test2); }
private static SparseLocalDateDoubleTimeSeries createUnsafe(LocalDate[] dates, double[] values) { return new SparseLocalDateDoubleTimeSeries(dates, values, true); }
@Override public LocalDateDoubleTimeSeries filter(ObjDoublePredicate<LocalDate> predicate) { ArgChecker.notNull(predicate, "predicate"); // build up result in arrays keeping track of count of retained dates LocalDate[] resDates = new LocalDate[size()]; double[] resValues = new double[size()]; int resCount = 0; for (int i = 0; i < size(); i++) { if (predicate.test(dates[i], values[i])) { resDates[resCount] = dates[i]; resValues[resCount] = values[i]; resCount++; } } return createUnsafe(Arrays.copyOf(resDates, resCount), Arrays.copyOf(resValues, resCount)); }