TemporalBinningStrategy implements BinningStrategy { @Override public BinRange[] getNormalizedRanges(final NumericData range) { if ((range == null) || (range.getMax() < range.getMin())) { return new BinRange[] {}; } final Calendar startEpoch = Calendar.getInstance(TimeZone.getTimeZone(timezone)); final long binSizeMillis = getBinSizeMillis(); startEpoch.setTimeInMillis((long) range.getMin()); setToEpoch(startEpoch); final long startEpochMillis = startEpoch.getTimeInMillis(); long epochIterator = startEpochMillis; final List<BinRange> bins = new ArrayList<>(); boolean firstBin = ((long) range.getMin() != startEpochMillis); boolean lastBin = false; do { final Calendar nextEpochCal = Calendar.getInstance(TimeZone.getTimeZone(timezone)); nextEpochCal.setTimeInMillis(epochIterator + (long) (binSizeMillis * 1.5)); setToEpoch(nextEpochCal); final long nextEpoch = nextEpochCal.getTimeInMillis(); final long maxOfBin = nextEpoch - 1; final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone(timezone)); cal.setTimeInMillis(epochIterator); long startMillis, endMillis; boolean fullExtent; if ((long) range.getMax() <= maxOfBin) { lastBin = true; endMillis = (long) range.getMax(); fullExtent = FloatCompareUtils.checkDoublesEqual(range.getMax(), maxOfBin); } else { endMillis = maxOfBin; fullExtent = !firstBin; } if (firstBin) { startMillis = (long) range.getMin(); firstBin = false; } else { startMillis = epochIterator; } bins.add( new BinRange( getBinId(cal), startMillis - epochIterator, endMillis - epochIterator, fullExtent)); epochIterator = nextEpoch; } while (!lastBin); return bins.toArray(new BinRange[bins.size()]); } TemporalBinningStrategy();  TemporalBinningStrategy(final Unit unit);  TemporalBinningStrategy(final Unit unit, final String timezone); @Override double getBinMin(); @Override double getBinMax(); @Override BinValue getBinnedValue(final double value); @Override int getFixedBinIdSize(); @Override BinRange[] getNormalizedRanges(final NumericData range); @Override byte[] toBinary(); @Override void fromBinary(final byte[] bytes); @Override int hashCode(); @Override boolean equals(final Object obj); @Override NumericRange getDenormalizedRanges(final BinRange binnedRange);  }
@Test public void testStartAndEndEqual() { final long time = 123987564019283L; final TemporalBinningStrategy binStrategy = new TemporalBinningStrategy(Unit.YEAR); final BinRange[] ranges = binStrategy.getNormalizedRanges(new NumericRange(time, time)); Assert.assertEquals(1, ranges.length); Assert.assertTrue(ranges[0].getNormalizedMin() == ranges[0].getNormalizedMax()); }
@Test public void testEndLessThanStart() { final long time = 123987564019283L; final TemporalBinningStrategy binStrategy = new TemporalBinningStrategy(Unit.YEAR); final BinRange[] ranges = binStrategy.getNormalizedRanges(new NumericRange(time, time - 1)); Assert.assertEquals(0, ranges.length); }
@Test public void testFeb28ToMarch1NonLeapYear() { final long time = 47920164930285667L; final Calendar startCal = Calendar.getInstance(); startCal.setTimeInMillis(time); startCal.set(Calendar.MONTH, 1); startCal.set(Calendar.YEAR, 2015); startCal.set(Calendar.DAY_OF_MONTH, 28); final Calendar endCal = Calendar.getInstance(); endCal.setTimeInMillis(time); endCal.set(Calendar.MONTH, 2); endCal.set(Calendar.YEAR, 2015); endCal.set(Calendar.DAY_OF_MONTH, 1); TemporalBinningStrategy binStrategy = new TemporalBinningStrategy(Unit.DAY); BinRange[] ranges = binStrategy.getNormalizedRanges( new NumericRange(startCal.getTimeInMillis(), endCal.getTimeInMillis())); Assert.assertEquals(2, ranges.length); binStrategy = new TemporalBinningStrategy(Unit.MONTH); ranges = binStrategy.getNormalizedRanges( new NumericRange( startCal.getTimeInMillis(), startCal.getTimeInMillis() + (TemporalBinningStrategy.MILLIS_PER_DAY))); Assert.assertEquals(2, ranges.length); }
@Test public void testFeb28ToMarch1LeapYear() { final long time = 29374659120374656L; final Calendar startCal = Calendar.getInstance(); startCal.setTimeInMillis(time); startCal.set(Calendar.MONTH, 1); startCal.set(Calendar.YEAR, 2016); startCal.set(Calendar.DAY_OF_MONTH, 28); final Calendar endCal = Calendar.getInstance(); endCal.setTimeInMillis(time); endCal.set(Calendar.MONTH, 2); endCal.set(Calendar.YEAR, 2016); endCal.set(Calendar.DAY_OF_MONTH, 1); TemporalBinningStrategy binStrategy = new TemporalBinningStrategy(Unit.DAY); BinRange[] ranges = binStrategy.getNormalizedRanges( new NumericRange(startCal.getTimeInMillis(), endCal.getTimeInMillis())); Assert.assertEquals(3, ranges.length); binStrategy = new TemporalBinningStrategy(Unit.MONTH); ranges = binStrategy.getNormalizedRanges( new NumericRange( startCal.getTimeInMillis(), startCal.getTimeInMillis() + (TemporalBinningStrategy.MILLIS_PER_DAY))); Assert.assertEquals(1, ranges.length); ranges = binStrategy.getNormalizedRanges( new NumericRange( startCal.getTimeInMillis(), startCal.getTimeInMillis() + (TemporalBinningStrategy.MILLIS_PER_DAY * 2))); Assert.assertEquals(2, ranges.length); }