DispatchingStateRepository { public static <T> StateRepository<T> using(EventSource eventSource, Class<? extends T> stateClass) { StateClassInfo<T> stateClassInfo = StateClassInfo.forStateClass(stateClass); final Supplier<StateBuilder<T>> aggregateStateBuilderSupplier = () -> DispatchingStateBuilder.dispatching( stateClassInfo.getInitialEventDispatcher(), stateClassInfo.getUpdateEventDispatcher()); return EventSourcingStateRepository.using( aggregateStateBuilderSupplier, eventSource, stateClassInfo.getEventTypeBinding(), stateClassInfo.getCausalOrder()); } private  DispatchingStateRepository(); static StateRepository<T> using(EventSource eventSource, Class<? extends T> stateClass);  }
@Test public void buildStates() { String id1 = "id1"; String id2 = "id2"; StreamTimestamp createTimestamp = nextTimestamp(); StreamTimestamp updateTimestamp = nextTimestamp(); eventBus.dispatch(PersonEvents.class, batch -> { batch.createdV2(createTimestamp, id1, "Arthur Putey", 41); batch.createdV1(createTimestamp, id2, "Arthur Dent"); batch.nameUpdated(updateTimestamp, id1, "Arthur Daley"); batch.nameUpdated(updateTimestamp, id2, "Arthur Mumby"); }); StateRepository<PersonState> cache = DispatchingStateRepository.using(eventSource, PersonState.class); Map<String, PersonState> statesBeforeUpdate = cache.getStates(id1, id2); assertThat(statesBeforeUpdate.get(id1).getName(), equalTo("Arthur Daley")); assertThat(statesBeforeUpdate.get(id2).getName(), equalTo("Arthur Mumby")); eventBus.dispatch(PersonEvents.class, batch -> { batch.nameUpdated(updateTimestamp.subStream("substream"), id2, "Arthur, King of the Britons"); }); Map<String, PersonState> states = cache.getStates(id1, id2); assertThat(states.get(id1).getName(), equalTo("Arthur Daley")); assertThat(states.get(id2).getName(), equalTo("Arthur, King of the Britons")); }