2929import com .google .protobuf .Timestamp ;
3030import com .google .protobuf .util .TimeUtil ;
3131import org .spine3 .base .CommandContext ;
32- import org .spine3 .base .CommandId ;
3332import org .spine3 .base .Event ;
3433import org .spine3 .base .EventContext ;
3534import org .spine3 .base .EventId ;
9493 * @author Alexander Yevsyukov
9594 * @author Mikhail Melnik
9695 */
97- @ SuppressWarnings ("ClassWithTooManyMethods" )
9896public abstract class Aggregate <I , S extends Message , B extends Message .Builder >
9997 extends Entity <I , S >
10098 implements CommandHandler {
@@ -366,10 +364,7 @@ private void apply(Iterable<? extends Message> messages, CommandContext commandC
366364 try {
367365 for (Message message : messages ) {
368366 apply (message );
369-
370- final int currentVersion = getVersion ();
371- final S state = getState ();
372- final EventContext eventContext = createEventContext (commandContext , state , whenModified (), currentVersion , message );
367+ final EventContext eventContext = createEventContext (commandContext , message );
373368 final Event event = Events .createEvent (message , eventContext );
374369 putUncommitted (event );
375370 }
@@ -430,7 +425,7 @@ private void putUncommitted(Event record) {
430425 * @return immutable view of records for all uncommitted events
431426 */
432427 @ CheckReturnValue
433- public List <Event > getUncommittedEvents () {
428+ /* package */ List <Event > getUncommittedEvents () {
434429 return ImmutableList .copyOf (uncommittedEvents );
435430 }
436431
@@ -439,7 +434,7 @@ public List<Event> getUncommittedEvents() {
439434 *
440435 * @return the list of event records
441436 */
442- public List <Event > commitEvents () {
437+ /* package */ List <Event > commitEvents () {
443438 final List <Event > result = ImmutableList .copyOf (uncommittedEvents );
444439 uncommittedEvents .clear ();
445440 return result ;
@@ -449,49 +444,38 @@ public List<Event> commitEvents() {
449444 * Creates a context for an event.
450445 *
451446 * <p>The context may optionally have custom attributes added by
452- * {@link #addEventContextAttributes( EventContext.Builder, CommandId, Message, Message, int )}.
447+ * {@link #extendEventContext(Message, EventContext.Builder, CommandContext )}.
453448 *
454449 *
455450 * @param commandContext the context of the command, execution of which produced the event
456- * @param currentState the state of the aggregated after the event was applied
457- * @param whenModified the moment of the aggregate modification for this event
458- * @param currentVersion the version of the aggregate after the event was applied
459451 * @param event the event for which to create the context
460452 * @return new instance of the {@code EventContext}
461- * @see #addEventContextAttributes( EventContext.Builder, CommandId, Message, Message, int )
453+ * @see #extendEventContext(Message, EventContext.Builder, CommandContext )
462454 */
463455 @ CheckReturnValue
464- protected EventContext createEventContext (CommandContext commandContext ,
465- S currentState ,
466- Timestamp whenModified ,
467- int currentVersion ,
468- Message event ) {
456+ protected EventContext createEventContext (CommandContext commandContext , Message event ) {
469457 final EventId eventId = Events .generateId ();
470- final EventContext .Builder result = EventContext .newBuilder ()
458+ final EventContext .Builder builder = EventContext .newBuilder ()
471459 .setEventId (eventId )
472460 .setCommandContext (commandContext )
473- .setTimestamp (whenModified )
474- .setVersion (currentVersion )
461+ .setTimestamp (whenModified () )
462+ .setVersion (getVersion () )
475463 .setProducerId (getIdAsAny ());
476- addEventContextAttributes ( result , commandContext . getCommandId (), event , currentState , currentVersion );
477- return result .build ();
464+ extendEventContext ( event , builder , commandContext );
465+ return builder .build ();
478466 }
479467
480468 /**
481469 * Adds custom attributes to an event context builder during the creation of the event context.
482470 *
483471 * <p>Does nothing by default. Override this method if you want to add custom attributes to the created context.
484472 *
485- * @param builder a builder for the event context
486- * @param commandId the id of the command, which cased the event
487473 * @param event the event message
488- * @param currentState the current state of the aggregate after the event was applied
489- * @param currentVersion the version of the aggregate after the event was applied
490- * @see #createEventContext(CommandContext, Message, Timestamp, int, Message)
474+ * @param builder a builder for the event context
475+ * @see #createEventContext(CommandContext, Message)
491476 */
492477 @ SuppressWarnings ({"NoopMethodInAbstractClass" , "UnusedParameters" }) // Have no-op method to avoid forced overriding.
493- protected void addEventContextAttributes (EventContext .Builder builder ,
494- CommandId commandId , Message event , S currentState , int currentVersion ) {
478+ protected void extendEventContext (Message event , EventContext .Builder builder , CommandContext commandContext ) {
495479 // Do nothing.
496480 }
497481
0 commit comments