/** * Process sampleCount sequential samples with identical values. sampleCount will usually be 1, * but may be larger than 1. Implementors may just loop processing identical values, but some * implementations may optimize adding a bunch of repeated values * * @param timeCursor a TimeCursor instance, which supplies successive int UNIX times * @param sampleCount the count of sequential, identical values * @param opcode the opcode of the sample value, which may not be a REPEAT opcode * @param value the value of this kind of sample over the sampleCount samples */ @Override public void processSamples(final TimelineCursor timeCursor, final int sampleCount, final SampleOpcode opcode, final Object value) { for (int i = 0; i < sampleCount; i++) { // Check if the sample is in the right time range final DateTime sampleTime = timeCursor.getNextTime(); if ((startTime == null || !sampleTime.isBefore(startTime)) && ((endTime == null || !sampleTime.isAfter(endTime)))) { processOneSample(sampleTime, opcode, value); } } }
@Override public void processSamples(final TimelineCursor timeCursor, final int sampleCount, final SampleOpcode opcode, final Object value) { if (builder.length() > 0) { builder.append(", "); } final DateTime timestamp = timeCursor.getNextTime(); builder.append("at ").append(timestamp.toString("yyyy-MM-dd HH:mm:ss")).append(" "); if (sampleCount > 1) { builder.append(sampleCount).append(" of "); } builder.append(opcode.name().toLowerCase()); switch (opcode) { case NULL: case DOUBLE_ZERO: case INT_ZERO: break; default: builder.append("(").append(String.valueOf(value)).append(")"); break; } }
@Test(groups = "fast") public void testTimeRangeSampleProcessor() throws Exception { final DateTime startTime = new DateTime(dateFormatter.parseDateTime("2012-03-23T17:35:11.000Z")); final DateTime endTime = new DateTime(dateFormatter.parseDateTime("2012-03-23T17:35:17.000Z")); final int sampleCount = 2; final List<DateTime> dateTimes = ImmutableList.<DateTime>of(startTime, endTime); final byte[] compressedTimes = timelineCoder.compressDateTimes(dateTimes); final TimelineCursor cursor = new DefaultTimelineCursor(compressedTimes, sampleCount); Assert.assertEquals(cursor.getNextTime(), startTime); Assert.assertEquals(cursor.getNextTime(), endTime); // 2 x the value 12: REPEAT_BYTE, SHORT, 2, SHORT, 12 (2 bytes) final byte[] samples = new byte[]{(byte) 0xff, 2, 2, 0, 12}; final AtomicInteger samplesCount = new AtomicInteger(0); sampleCoder.scan(samples, compressedTimes, sampleCount, new TimeRangeSampleProcessor(startTime, endTime) { @Override public void processOneSample(final DateTime time, final SampleOpcode opcode, final Object value) { if (samplesCount.get() == 0) { Assert.assertEquals(DateTimeUtils.unixSeconds(time), DateTimeUtils.unixSeconds(startTime)); } else { Assert.assertEquals(DateTimeUtils.unixSeconds(time), DateTimeUtils.unixSeconds(endTime)); } samplesCount.incrementAndGet(); } }); Assert.assertEquals(samplesCount.get(), sampleCount); }