/** * 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 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 // or the series don't intersect if (isEmpty() || startInclusive.equals(endExclusive) || !startDate.isBefore(endExclusive) || startInclusive.isAfter(getLatestDate())) { return LocalDateDoubleTimeSeries.empty(); } LocalDate resolvedStart = dateCalculation.adjustDate(Ordering.natural().max(startInclusive, startDate)); int startIndex = dateCalculation.calculatePosition(startDate, resolvedStart); int endIndex = dateCalculation.calculatePosition(startDate, endExclusive); return new DenseLocalDateDoubleTimeSeries( resolvedStart, Arrays.copyOfRange(points, Math.max(0, startIndex), Math.min(points.length, endIndex)), dateCalculation, true); }
private LocalDate calculateDateFromPosition(int i) { return dateCalculation.calculateDateFromPosition(startDate, i); }
@Override public OptionalDouble get(LocalDate date) { if (!isEmpty() && !date.isBefore(startDate) && dateCalculation.allowsDate(date)) { int position = dateCalculation.calculatePosition(startDate, date); if (position < points.length) { double value = points[position]; if (isValidPoint(value)) { return OptionalDouble.of(value); } } } return OptionalDouble.empty(); }
private double density() { // We can use the calculators to work out range size double rangeSize = determineCalculation().calculatePosition(entries.firstKey(), entries.lastKey()) + 1; return entries.size() / rangeSize; }