@Override
    public void add(T item) {
        final int sizeAtTimeOfAdd;
        synchronized (items) {
            items.add(item);
            sizeAtTimeOfAdd = items.size();
        }

        /*
            WARNING: It is possible that the item that was just added to the list
            has been processed by an existing idle task at this point.

            By rescheduling the following timers, it is possible that a
            superfluous maxTask is generated now OR that the idle task and max
            task are scheduled at their specified delays. This could result in
            calls to processItems sooner than expected.
         */

        // Did we hit the max item threshold?
        if (sizeAtTimeOfAdd >= maxItems) {
            if (maxIdleMillis < maxBatchMillis) {
                cancelTask(idleTask);
            }
            rescheduleTask(maxTask, 0 /* now! */);
        } else {
            // Otherwise, schedule idle task and if this is a first item
            // also schedule the max batch age task.
            if (maxIdleMillis < maxBatchMillis) {
                rescheduleTask(idleTask, maxIdleMillis);
            }
            if (sizeAtTimeOfAdd == 1) {
                rescheduleTask(maxTask, maxBatchMillis);
            }
        }
    }
@Test
    public void readyMaxTrigger() {
        TestAccumulator accumulator = new TestAccumulator();
        accumulator.ready = false;
        accumulator.add(new TestItem("a"));
        accumulator.add(new TestItem("b"));
        accumulator.add(new TestItem("c"));
        accumulator.add(new TestItem("d"));
        accumulator.add(new TestItem("e"));
        accumulator.add(new TestItem("f"));
        assertTrue("should not have fired yet", accumulator.batch.isEmpty());
        accumulator.ready = true;
        accumulator.add(new TestItem("g"));
        timer.advanceTimeMillis(10, LONG_REAL_TIME_DELAY);
        assertFalse("should have fired", accumulator.batch.isEmpty());
        assertEquals("incorrect batch", "abcdefg", accumulator.batch);
    }