DomainEventMessage next = events.next();
validateIdentifier(next.getAggregateIdentifier().getClass());
if (!storedEvents.isEmpty()) {
DomainEventMessage lastEvent = storedEvents.peekLast();
if (!lastEvent.getAggregateIdentifier().equals(next.getAggregateIdentifier())) {
throw new EventStoreException("Writing events for an unexpected aggregate. This could "
+ "indicate that a wrong aggregate is being triggered.");
} else if (lastEvent.getSequenceNumber() != next.getSequenceNumber() - 1) {
throw new EventStoreException(format("Unexpected sequence number on stored event. "
+ "Expected %s, but got %s.",
lastEvent.getSequenceNumber() + 1,
next.getSequenceNumber()));
}
}