|
| 1 | +package io.github.dddplus.buddy; |
| 2 | + |
| 3 | +import lombok.NonNull; |
| 4 | + |
| 5 | +import java.util.LinkedList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.Optional; |
| 8 | +import java.util.stream.Collectors; |
| 9 | + |
| 10 | +/** |
| 11 | + * 脏数据备忘录,服务于聚合根,帮助它对内部变化的对象进行追踪. |
| 12 | + * |
| 13 | + * <p>DDD里,entity在内存操作,改变状态,最后{@code repo.save(entity);} </p> |
| 14 | + * <p>{@code DirtyMemento}被Entity用来(主动)通知Repository哪些状态改变了(变脏了)</p> |
| 15 | + * <p>通过这个机制,{@code repo.save(entity);}可以通过一个save方法支持所有的状态改变场景</p> |
| 16 | + */ |
| 17 | +public final class DirtyMemento { |
| 18 | + private final List<IDirtyHint> hints = new LinkedList<>(); |
| 19 | + |
| 20 | + /** |
| 21 | + * 注册一个脏数据通知. |
| 22 | + * |
| 23 | + * @param hint the dirty hint with necessary data |
| 24 | + */ |
| 25 | + public void registerHint(@NonNull IDirtyHint hint) { |
| 26 | + hints.add(hint); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * 合并脏数据通知:如未注册则注册,否则与现有那一个hint合并. |
| 31 | + * |
| 32 | + * <p>保证备忘录里,该{@link IMergeableDirtyHint}只保留一个</p> |
| 33 | + * <p>Merge on Write</p> |
| 34 | + * |
| 35 | + * @param hint 脏数据通知 |
| 36 | + */ |
| 37 | + public void registerOrMerge(@NonNull IMergeableDirtyHint hint) { |
| 38 | + for (IDirtyHint existingHint : hints) { |
| 39 | + if (!existingHint.getClass().equals(hint.getClass())) { |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + IMergeableDirtyHint existingMergeableHint = (IMergeableDirtyHint) existingHint; |
| 44 | + if (hint.getId().equals(existingMergeableHint.getId())) { |
| 45 | + // found it, trigger the hook |
| 46 | + hint.onMerge(existingMergeableHint); |
| 47 | + return; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + // not found |
| 52 | + registerHint(hint); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * 当前的所有脏数据. |
| 57 | + * |
| 58 | + * @return all dirty hints |
| 59 | + */ |
| 60 | + public List<IDirtyHint> dirtyHints() { |
| 61 | + return hints; |
| 62 | + } |
| 63 | + |
| 64 | + public void clear() { |
| 65 | + hints.clear(); |
| 66 | + } |
| 67 | + |
| 68 | + public boolean isEmpty() { |
| 69 | + return hints.isEmpty(); |
| 70 | + } |
| 71 | + |
| 72 | + public int size() { |
| 73 | + return hints.size(); |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * 根据指定hint class type,找到已经注册的第一个脏数据通知. |
| 78 | + * |
| 79 | + * @param hintClass concrete type of {@link IDirtyHint} |
| 80 | + * @param <T> hint class type |
| 81 | + * @return null if not found. ONLY returns the first registered hint of specified type |
| 82 | + */ |
| 83 | + public <T extends IDirtyHint> T firstHintOf(Class<T> hintClass) { |
| 84 | + Optional<IDirtyHint> dirtyHint = hints.stream().filter(hintClass::isInstance).findFirst(); |
| 85 | + if (dirtyHint.isPresent()) { |
| 86 | + return (T) dirtyHint.get(); |
| 87 | + } else { |
| 88 | + return null; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * 根据指定hint class type,找到已经注册的最后一个脏数据通知. |
| 94 | + * |
| 95 | + * <p>例如,在一个请求内状态多次修改,只希望获取最新(最后)状态.</p> |
| 96 | + * |
| 97 | + * @param hintClass concrete type of {@link IDirtyHint} |
| 98 | + * @param <T> hint class type |
| 99 | + * @return null if not found. ONLY returns the last registered hint of specified type |
| 100 | + */ |
| 101 | + public <T extends IDirtyHint> T lastHintOf(Class<T> hintClass) { |
| 102 | + List<T> dirtyHints = dirtyHintsOf(hintClass); |
| 103 | + if (dirtyHints.isEmpty()) { |
| 104 | + return null; |
| 105 | + } |
| 106 | + |
| 107 | + return dirtyHints.get(dirtyHints.size() - 1); |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * 根据指定hint class type,找到其所有的脏数据通知. |
| 112 | + * |
| 113 | + * @param hintClass concrete type of {@link IDirtyHint} |
| 114 | + * @param <T> hint class type |
| 115 | + * @return will never returns null, but might be an empty list |
| 116 | + */ |
| 117 | + @NonNull |
| 118 | + public <T extends IDirtyHint> List<T> dirtyHintsOf(Class<T> hintClass) { |
| 119 | + return (List<T>) hints.stream().filter(hintClass::isInstance).collect(Collectors.toList()); |
| 120 | + } |
| 121 | +} |
0 commit comments