public void addFirst(T event) throws RejectedExecutionException {
        lock.lock();
        try {
            if (closed) throw new RejectedExecutionException("Can't accept an event because the accumulator is closed.");

            K key = event.key();
            Deque<T> queue = queues.get(key);
            if (queue == null) {
                queue = new LinkedList<>();
                queues.put(key, queue);
                if (!inflightKeys.contains(key)) {
                    addAvailableKey(key);
                }
            }
            queue.addFirst(event);
            size++;
        } finally {
            lock.unlock();
        }
    }
@Test
    public void testAddFirst() {
        EventAccumulator<Integer, MockEvent> accumulator = new EventAccumulator<>();

        List<MockEvent> events = Arrays.asList(
            new MockEvent(1, 0),
            new MockEvent(1, 1),
            new MockEvent(1, 2)
        );

        events.forEach(accumulator::addFirst);
        assertEquals(3, accumulator.size());

        List<MockEvent> polledEvents = new ArrayList<>(3);
        for (int i = 0; i < events.size(); i++) {
            MockEvent event = accumulator.poll();
            assertNotNull(event);
            polledEvents.add(event);
            assertEquals(events.size() - 1 - i, accumulator.size());
            accumulator.done(event);
        }

        Collections.reverse(events);
        assertEquals(events, polledEvents);

        accumulator.close();
    }