Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) 2010-2011. Axon Framework
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.axonframework.domain;
 
 
 import java.util.List;

Container for events related to a single aggregate. All events added to this container will automatically be assigned the aggregate identifier and a sequence number.

Author(s):
Allard Buijze
Since:
0.1
See also:
DomainEvent
AbstractAggregateRoot
 
 class EventContainer implements Serializable {
 
     private static final long serialVersionUID = -39816393359395878L;
 
     private final List<DomainEventevents = new ArrayList<DomainEvent>();
     private final AggregateIdentifier aggregateIdentifier;
     private Long lastCommittedSequenceNumber;
     private transient Long lastSequenceNumber;

    
Initialize an EventContainer for an aggregate with the given aggregateIdentifier. This identifier will be attached to all incoming events.

Parameters:
aggregateIdentifier the aggregate identifier to assign to this container
 
     public EventContainer(AggregateIdentifier aggregateIdentifier) {
         this. = aggregateIdentifier;
     }

    
Add an event to this container.

Events should either be already assigned to the aggregate with the same identifier as this container, or have no aggregate assigned yet. If an event has a sequence number assigned, it must follow directly upon the sequence number of the event that was previously added.

Parameters:
event the event to add to this container
 
     public void addEvent(DomainEvent event) {
         Assert.isTrue(event.getSequenceNumber() == null
                               || getLastSequenceNumber() == null
                               || event.getSequenceNumber().equals(getLastSequenceNumber() + 1),
                       "The given event's sequence number is discontinuous");
 
         Assert.isTrue(event.getAggregateIdentifier() == null
                               || .equals(event.getAggregateIdentifier()),
                       "The Identifier of the event does not match the Identifier of the EventContainer");
 
         if (event.getAggregateIdentifier() == null) {
             event.setAggregateIdentifier();
         }
 
         if (event.getSequenceNumber() == null) {
             event.setSequenceNumber(newSequenceNumber());
         }
          = event.getSequenceNumber();
         .add(event);
     }

    
Read the events inside this container using a DomainEventStream. The returned stream is a snapshot of the uncommitted events in the aggregate at the time of the invocation. Once returned, newly applied events are not accessible from the returned event stream.

Returns:
a DomainEventStream providing access to the events in this container
 
     public DomainEventStream getEventStream() {
         return new SimpleDomainEventStream();
     }

    
Returns the aggregate identifier assigned to this container.

Returns:
the aggregate identifier assigned to this container
        return ;
    }

    
Sets the first sequence number that should be assigned to an incoming event.

Parameters:
lastKnownSequenceNumber the sequence number of the last known event
    public void initializeSequenceNumber(Long lastKnownSequenceNumber) {
        Assert.state(.size() == 0, "Cannot set first sequence number if events have already been added");
         = lastKnownSequenceNumber;
    }

    
Returns the sequence number of the event last added to this container.

Returns:
the sequence number of the last event
    public Long getLastSequenceNumber() {
        if (.isEmpty()) {
            return ;
        } else if ( == null) {
             = .get(.size() - 1).getSequenceNumber();
        }
        return ;
    }

    
Returns the sequence number of the last committed event, or null if no events have been committed.

Returns:
the sequence number of the last committed event
        return ;
    }

    
Clears the events in this container. The sequence number is not modified by this call.
    public void commit() {
        .clear();
    }

    
Returns the number of events currently inside this container.

Returns:
the number of events in this container
    public int size() {
        return .size();
    }
    private long newSequenceNumber() {
        Long currentSequenceNumber = getLastSequenceNumber();
        if (currentSequenceNumber == null) {
            return 0;
        }
        return currentSequenceNumber + 1;
    }

    
Returns an unmodifiable version of the backing list of events.

Returns:
a list containing the events in this container
    public List<DomainEventgetEventList() {
        return Collections.unmodifiableList();
    }
New to GrepCode? Check out our FAQ X