|
| 1 | +/* |
| 2 | + * Copyright 2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.modulith.events; |
| 17 | + |
| 18 | +import java.util.Objects; |
| 19 | + |
| 20 | +import org.springframework.core.ResolvableType; |
| 21 | +import org.springframework.core.ResolvableTypeProvider; |
| 22 | +import org.springframework.lang.Nullable; |
| 23 | +import org.springframework.util.Assert; |
| 24 | + |
| 25 | +/** |
| 26 | + * An infrastructure event signaling that an application event has been externalized with a particular, broker-specific |
| 27 | + * result. |
| 28 | + * |
| 29 | + * @author Oliver Drotbohm |
| 30 | + * @since 1.1 |
| 31 | + */ |
| 32 | +public class EventExternalized<S, T> implements ResolvableTypeProvider { |
| 33 | + |
| 34 | + private final S event; |
| 35 | + private final Object mapped; |
| 36 | + private final RoutingTarget target; |
| 37 | + private final @Nullable T brokerResult; |
| 38 | + private final ResolvableType type; |
| 39 | + |
| 40 | + /** |
| 41 | + * Creates a new {@link EventExternalized} event for the given source event, its mapped derivative, |
| 42 | + * {@link RoutingTarget} and broker result. |
| 43 | + * |
| 44 | + * @param event must not be {@literal null}. |
| 45 | + * @param mapped must not be {@literal null}. |
| 46 | + * @param target must not be {@literal null}. |
| 47 | + * @param brokerResult can be {@literal null} |
| 48 | + */ |
| 49 | + public EventExternalized(S event, Object mapped, RoutingTarget target, @Nullable T brokerResult) { |
| 50 | + |
| 51 | + Assert.notNull(event, "Source event must not be null!"); |
| 52 | + Assert.notNull(mapped, "Mapped event must not be null!"); |
| 53 | + Assert.notNull(target, "Routing target must not be null!"); |
| 54 | + |
| 55 | + this.event = event; |
| 56 | + this.mapped = mapped; |
| 57 | + this.target = target; |
| 58 | + this.brokerResult = brokerResult; |
| 59 | + |
| 60 | + this.type = ResolvableType.forClassWithGenerics(EventExternalized.class, ResolvableType.forInstance(event), |
| 61 | + brokerResult == null ? ResolvableType.forClass(Object.class) : ResolvableType.forInstance(brokerResult)); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Returns the source event. |
| 66 | + * |
| 67 | + * @return will never be {@literal null}. |
| 68 | + */ |
| 69 | + public S getEvent() { |
| 70 | + return event; |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Returns the type of the source event. |
| 75 | + * |
| 76 | + * @return will never be {@literal null}. |
| 77 | + */ |
| 78 | + @SuppressWarnings("unchecked") |
| 79 | + public Class<S> getEventType() { |
| 80 | + return (Class<S>) type.getGeneric(0).resolve(Object.class); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Returns the mapped event. |
| 85 | + * |
| 86 | + * @return will never be {@literal null}. |
| 87 | + */ |
| 88 | + public Object getMapped() { |
| 89 | + return mapped; |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Returns the routing target. |
| 94 | + * |
| 95 | + * @return will never be {@literal null}. |
| 96 | + */ |
| 97 | + public RoutingTarget getTarget() { |
| 98 | + return target; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Returns the broker result. |
| 103 | + * |
| 104 | + * @return can be {@literal null}. |
| 105 | + */ |
| 106 | + public T getBrokerResult() { |
| 107 | + return brokerResult; |
| 108 | + } |
| 109 | + |
| 110 | + /* |
| 111 | + * (non-Javadoc) |
| 112 | + * @see org.springframework.core.ResolvableTypeProvider#getResolvableType() |
| 113 | + */ |
| 114 | + @Override |
| 115 | + public ResolvableType getResolvableType() { |
| 116 | + return type; |
| 117 | + } |
| 118 | + |
| 119 | + /* |
| 120 | + * (non-Javadoc) |
| 121 | + * @see java.lang.Object#equals(java.lang.Object) |
| 122 | + */ |
| 123 | + @Override |
| 124 | + public boolean equals(Object obj) { |
| 125 | + |
| 126 | + if (obj == this) { |
| 127 | + return true; |
| 128 | + } |
| 129 | + |
| 130 | + if (!(obj instanceof EventExternalized that)) { |
| 131 | + return false; |
| 132 | + } |
| 133 | + |
| 134 | + return Objects.equals(this.event, that.event) |
| 135 | + && Objects.equals(this.mapped, that.mapped) |
| 136 | + && Objects.equals(this.brokerResult, that.brokerResult); |
| 137 | + } |
| 138 | + |
| 139 | + /* |
| 140 | + * (non-Javadoc) |
| 141 | + * @see java.lang.Object#hashCode() |
| 142 | + */ |
| 143 | + @Override |
| 144 | + public int hashCode() { |
| 145 | + return Objects.hash(this.event, this.mapped, this.brokerResult); |
| 146 | + } |
| 147 | +} |
0 commit comments