InMemoryEventStore extends AbstractReadableEventStore implements IInMemoryEventStore { @Override public final StreamEventsSlice readEventsBackward(final StreamId streamId, final long start, final int count) { Contract.requireArgNotNull("streamId", streamId); Contract.requireArgMin("start", start, 0); Contract.requireArgMin("count", count, 1); ensureOpen(); final List<CommonEvent> events = getStream(streamId, ExpectedVersion.ANY.getNo()).getEvents(); final List<CommonEvent> result = new ArrayList<CommonEvent>(); if (start < events.size()) { for (int i = (int) start; (i > (start - count)) && (i >= 0); i--) { result.add(events.get(i)); } } final long fromEventNumber = start; long nextEventNumber = start - result.size(); if (nextEventNumber < 0) { nextEventNumber = 0; } final boolean endOfStream = (start - count) < 0; return new StreamEventsSlice(fromEventNumber, result, nextEventNumber, endOfStream); } InMemoryEventStore(@NotNull final Executor executor); @Override final InMemoryEventStore open(); @Override final void close(); @Override final boolean isSupportsCreateStream(); @Override final void createStream(final StreamId streamId); @Override final boolean streamExists(final StreamId streamId); @Override final CommonEvent readEvent(final StreamId streamId, final long eventNumber); @Override final StreamEventsSlice readEventsForward(final StreamId streamId, final long start, final int count); @Override final StreamEventsSlice readEventsBackward(final StreamId streamId, final long start, final int count); @Override final void deleteStream(final StreamId streamId, final long expected, final boolean hardDelete); @Override final void deleteStream(final StreamId streamId, final boolean hardDelete); @Override final long appendToStream(final StreamId streamId, final long expectedVersion, final List<CommonEvent> toAppend); @Override final long appendToStream(final StreamId streamId, final long expectedVersion, final CommonEvent... events); @Override final long appendToStream(final StreamId streamId, final List<CommonEvent> toAppend); @Override final long appendToStream(final StreamId streamId, final CommonEvent... events); @Override final Subscription subscribeToStream(final StreamId streamId, final long eventNumber,
            final BiConsumer<Subscription, CommonEvent> onEvent, final BiConsumer<Subscription, Exception> onDrop); @Override final void unsubscribeFromStream(final Subscription subscription); @Override final StreamState streamState(final StreamId streamId);  }
@Test public void testReadEventsBackward() { final StreamId streamId = new SimpleStreamId("MyStream"); final CommonEvent eventOne = event("5874a43c-3404-42a1-a07e-64e22209963b", "One"); final CommonEvent eventTwo = event("32383ba2-d524-4466-9ad2-f06a3fe1958f", "Two"); final CommonEvent eventThree = event("8885c8c1-6148-4709-b816-018f72111559", "Three"); final CommonEvent eventFour = event("4028e009-74eb-4a31-905a-3bdffb1046c0", "Four"); final CommonEvent eventFive = event("9bfb6129-f503-49a7-b2d1-7a2edcefae9c", "Five"); final long version = testee.appendToStream(streamId, ExpectedVersion.NO_OR_EMPTY_STREAM.getNo(), eventOne, eventTwo, eventThree, eventFour, eventFive); final StreamEventsSlice slice1 = testee.readEventsBackward(streamId, version, 2); assertThat(slice1.getEvents()).containsExactly(eventFive, eventFour); assertThat(slice1.getFromEventNumber()).isEqualTo(version); assertThat(slice1.getNextEventNumber()).isEqualTo(version - 2); assertThat(slice1.isEndOfStream()).isFalse(); final StreamEventsSlice slice2 = testee.readEventsBackward(streamId, slice1.getNextEventNumber(), 2); assertThat(slice2.getEvents()).containsExactly(eventThree, eventTwo); assertThat(slice2.getFromEventNumber()).isEqualTo(version - 2); assertThat(slice2.getNextEventNumber()).isEqualTo(version - 4); assertThat(slice2.isEndOfStream()).isFalse(); final StreamEventsSlice slice3 = testee.readEventsBackward(streamId, slice2.getNextEventNumber(), 2); assertThat(slice3.getEvents()).containsExactly(eventOne); assertThat(slice3.getFromEventNumber()).isEqualTo(version - 4); assertThat(slice3.getNextEventNumber()).isEqualTo(version - 4); assertThat(slice3.isEndOfStream()).isTrue(); }