Skip to content

Commit cd88dc9

Browse files
Move DefaultStateRegistry to the package level.
It makes its existence more obvious. Also, removed validation of state in the `OrderAggregate`. It's too late to validate anything at the stage of applying event data. It should be done in a command handler instead.
1 parent ce99314 commit cd88dc9

File tree

3 files changed

+89
-63
lines changed

3 files changed

+89
-63
lines changed

examples/src/main/java/org/spine3/examples/aggregate/server/OrderAggregate.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ private void event(OrderCreated event) {
8282
.setStatus(Order.Status.NEW)
8383
.build();
8484

85-
validate(newState);
8685
incrementState(newState);
8786
}
8887

@@ -96,7 +95,6 @@ private void event(OrderLineAdded event) {
9695
.setTotal(currentState.getTotal() + orderLine.getTotal())
9796
.build();
9897

99-
validate(newState);
10098
incrementState(newState);
10199
}
102100

@@ -108,7 +106,6 @@ private void event(OrderPaid event) {
108106
.setStatus(Order.Status.PAID)
109107
.build();
110108

111-
validate(newState);
112109
incrementState(newState);
113110
}
114111

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Copyright 2016, TeamDev Ltd. All rights reserved.
3+
*
4+
* Redistribution and use in source and/or binary forms, with or without
5+
* modification, must retain the above copyright notice and the following
6+
* disclaimer.
7+
*
8+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
9+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
10+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
11+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
12+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
14+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
15+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
16+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
17+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
18+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
19+
*/
20+
21+
package org.spine3.server.entity;
22+
23+
import com.google.protobuf.Message;
24+
25+
import javax.annotation.CheckReturnValue;
26+
import java.util.Map;
27+
28+
import static com.google.common.collect.Maps.newHashMap;
29+
30+
/**
31+
* A wrapper for the map from entity classes to entity default states.
32+
*
33+
* @author Alexander Yevsyukov
34+
*/
35+
/* package */ class DefaultStateRegistry {
36+
37+
private final Map<Class<? extends Entity>, Message> defaultStates = newHashMap();
38+
39+
/**
40+
* Specifies if the entity state of this class is already registered.
41+
*
42+
* @param entityClass the class to check
43+
* @return {@code true} if there is a state for the passed class, {@code false} otherwise
44+
*/
45+
@CheckReturnValue
46+
/* package */ boolean contains(Class<? extends Entity> entityClass) {
47+
final boolean result = defaultStates.containsKey(entityClass);
48+
return result;
49+
}
50+
51+
/**
52+
* Saves a state.
53+
*
54+
* @param entityClass an entity class
55+
* @param state a default state of the entity
56+
* @throws IllegalArgumentException if the state of this class is already registered
57+
*/
58+
/* package */ void put(Class<? extends Entity> entityClass, Message state) {
59+
if (contains(entityClass)) {
60+
throw new IllegalArgumentException("This class is registered already: " + entityClass.getName());
61+
}
62+
defaultStates.put(entityClass, state);
63+
}
64+
65+
/**
66+
* Obtains a state for the passed class..
67+
*
68+
* @param entityClass an entity class
69+
*/
70+
@CheckReturnValue
71+
/* package */ Message get(Class<? extends Entity> entityClass) {
72+
final Message state = defaultStates.get(entityClass);
73+
return state;
74+
}
75+
76+
/* package */ static DefaultStateRegistry getInstance() {
77+
return Singleton.INSTANCE.value;
78+
}
79+
80+
private enum Singleton {
81+
INSTANCE;
82+
@SuppressWarnings("NonSerializableFieldInSerializableClass")
83+
private final DefaultStateRegistry value = new DefaultStateRegistry();
84+
}
85+
}

server/src/main/java/org/spine3/server/entity/Entity.java

Lines changed: 4 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,10 @@
3232
import javax.annotation.Nonnull;
3333
import java.lang.reflect.Constructor;
3434
import java.lang.reflect.InvocationTargetException;
35-
import java.util.Map;
3635

3736
import static com.google.api.client.util.Throwables.propagate;
3837
import static com.google.common.base.Preconditions.checkNotNull;
3938
import static com.google.common.collect.Iterables.transform;
40-
import static com.google.common.collect.Maps.newHashMap;
4139
import static com.google.protobuf.util.TimeUtil.getCurrentTime;
4240

4341
/**
@@ -120,15 +118,15 @@ protected S getDefaultState() {
120118
final Class<? extends Entity> entityClass = getClass();
121119
final DefaultStateRegistry registry = DefaultStateRegistry.getInstance();
122120
if (!registry.contains(entityClass)) {
123-
final S state = retrieveDefaultState();
121+
final S state = createDefaultState();
124122
registry.put(entityClass, state);
125123
}
126124
@SuppressWarnings("unchecked") // cast is safe because this type of messages is saved to the map
127125
final S defaultState = (S) registry.get(entityClass);
128126
return defaultState;
129127
}
130128

131-
private S retrieveDefaultState() {
129+
private S createDefaultState() {
132130
final Class<S> stateClass = Classes.getGenericParameterType(getClass(), STATE_CLASS_GENERIC_INDEX);
133131
try {
134132
final Constructor<S> constructor = stateClass.getDeclaredConstructor();
@@ -154,8 +152,8 @@ public S getState() {
154152
/**
155153
* Validates the passed state.
156154
*
157-
* <p>Does nothing by default. Aggregate roots may override this method to
158-
* specify logic of validating initial or intermediate state of the root.
155+
* <p>Does nothing by default. Aggregates may override this method to
156+
* specify logic of validating initial or intermediate state.
159157
*
160158
* @param state a state object to replace the current state
161159
* @throws IllegalStateException if the state is not valid
@@ -307,58 +305,4 @@ public String apply(Class<?> clazz) {
307305
return result;
308306
}
309307

310-
/**
311-
* A wrapper for the map from entity classes to entity default states.
312-
*/
313-
private static class DefaultStateRegistry {
314-
315-
private final Map<Class<? extends Entity>, Message> defaultStates = newHashMap();
316-
317-
/**
318-
* Specifies if the entity state of this class is already registered.
319-
*
320-
* @param entityClass the class to check
321-
* @return {@code true} if there is a state for the passed class, {@code false} otherwise
322-
*/
323-
@CheckReturnValue
324-
private boolean contains(Class<? extends Entity> entityClass) {
325-
final boolean result = defaultStates.containsKey(entityClass);
326-
return result;
327-
}
328-
329-
/**
330-
* Saves a state.
331-
*
332-
* @param entityClass an entity class
333-
* @param state a default state of the entity
334-
* @throws IllegalArgumentException if the state of this class is already registered
335-
*/
336-
private void put(Class<? extends Entity> entityClass, Message state) {
337-
if (contains(entityClass)) {
338-
throw new IllegalArgumentException("This class is registered already: " + entityClass.getName());
339-
}
340-
defaultStates.put(entityClass, state);
341-
}
342-
343-
/**
344-
* Obtains a state for the passed class..
345-
*
346-
* @param entityClass an entity class
347-
*/
348-
@CheckReturnValue
349-
private Message get(Class<? extends Entity> entityClass) {
350-
final Message state = defaultStates.get(entityClass);
351-
return state;
352-
}
353-
354-
private static DefaultStateRegistry getInstance() {
355-
return Singleton.INSTANCE.value;
356-
}
357-
358-
private enum Singleton {
359-
INSTANCE;
360-
@SuppressWarnings("NonSerializableFieldInSerializableClass")
361-
private final DefaultStateRegistry value = new DefaultStateRegistry();
362-
}
363-
}
364308
}

0 commit comments

Comments
 (0)