5252import static com .google .common .base .Throwables .propagate ;
5353import static org .spine3 .base .Identifiers .idToAny ;
5454
55- //TODO:2016-02-17:alexander.yevsyukov: Define syntax of event applier methods.
5655//TODO:2016-02-17:alexander.yevsyukov: Describe dealing with command validation and throwing Failures.
5756//TODO:2016-02-17:alexander.yevsyukov: Describe (not) throwing exceptions.
5857
8483 * <em>applier methods</em>.
8584 *
8685 * <p>An event applier is a method that changes the state of the aggregate in response to an event. The aggregate
87- * must have applier methods for <em>all</em> event types it produces.
88- *
89- * <h2>Handling snapshots</h2>
90- * <p>In order to optimise the restoration of aggregates, an {@link AggregateRepository} can periodically store
91- * snapshots of aggregate state.
92- *
93- * <p>The {@code Aggregate} restores its state in {@link #restore(Snapshot)} method.
86+ * must have applier methods for <em>all</em> event types it produces. An event applier takes a single parameter
87+ * of the event message it handles and returns {@code void}. The modification of the state is done via a builder
88+ * instance obtained from {@link #getBuilder()}.
9489 *
9590 * @param <I> the type for IDs of this class of aggregates
9691 * @param <S> the type of the state held by the aggregate
9792 * @param <B> the type of the aggregate state builder
98- * @author Mikhail Melnik
93+ *
9994 * @author Alexander Yevsyukov
95+ * @author Mikhail Melnik
10096 */
10197@ SuppressWarnings ("ClassWithTooManyMethods" )
10298public abstract class Aggregate <I , S extends Message , B extends Message .Builder >
@@ -110,8 +106,11 @@ public abstract class Aggregate<I, S extends Message, B extends Message.Builder>
110106 /**
111107 * The builder for the aggregate state.
112108 *
113- * <p>This field is non-null only in when the aggregate changes its state during the command handling,
114- * or during playing events.
109+ * <p>This field is non-null only when the aggregate changes its state during command handling or playing events.
110+ *
111+ * @see #createBuilder()
112+ * @see #getBuilder()
113+ * @see #updateState()
115114 */
116115 @ Nullable
117116 private volatile B builder ;
@@ -158,16 +157,6 @@ public Aggregate(I id) {
158157 this .idAsAny = idToAny (id );
159158 }
160159
161- /**
162- * Check if the type of the builder passed as a generic parameter matches the type of the message.
163- *
164- * @throws ClassCastException if the builder type does not match the type of the state
165- */
166- private void checkBuilderType () {
167- @ SuppressWarnings ("unchecked" )
168- final B ignored = (B ) getState ().toBuilder ();
169- }
170-
171160 /**
172161 * Returns the set of the command classes handled by the passed aggregate class.
173162 *
@@ -205,8 +194,6 @@ private void init() {
205194
206195 // Register this aggregate class if it wasn't before.
207196 if (!registry .contains (thisClass )) {
208- // Check if the generic types of the class are compatible. Do it once per aggregate class.
209- checkBuilderType ();
210197 registry .register (thisClass );
211198 }
212199
@@ -242,7 +229,7 @@ private void createBuilder() {
242229 protected B getBuilder () {
243230 if (this .builder == null ) {
244231 throw new IllegalStateException (
245- "Builder is not available. Make sure to call the method only from an event applier." );
232+ "Builder is not available. Make sure to call getBuilder() only from an event applier method ." );
246233 }
247234 return builder ;
248235 }
@@ -509,12 +496,12 @@ protected void addEventContextAttributes(EventContext.Builder builder,
509496 }
510497
511498 /**
512- * Transforms the current state of the aggregate into the snapshot event .
499+ * Transforms the current state of the aggregate into the {@link Snapshot} instance .
513500 *
514501 * @return new snapshot
515502 */
516503 @ CheckReturnValue
517- public Snapshot toSnapshot () {
504+ /* package */ Snapshot toSnapshot () {
518505 final Any state = Any .pack (getState ());
519506 final int version = getVersion ();
520507 final Timestamp whenModified = whenModified ();
0 commit comments