|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2014 the original author or authors. |
| 2 | + * Copyright 2012-2017 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
15 | 15 | */
|
16 | 16 | package org.springframework.data.rest.core.event;
|
17 | 17 |
|
| 18 | +import lombok.EqualsAndHashCode; |
| 19 | +import lombok.RequiredArgsConstructor; |
| 20 | +import lombok.ToString; |
| 21 | + |
18 | 22 | import java.lang.annotation.Annotation;
|
19 | 23 | import java.lang.reflect.Method;
|
20 | 24 | import java.util.ArrayList;
|
| 25 | +import java.util.Collections; |
21 | 26 | import java.util.List;
|
22 | 27 |
|
23 | 28 | import org.slf4j.Logger;
|
|
26 | 31 | import org.springframework.beans.factory.config.BeanPostProcessor;
|
27 | 32 | import org.springframework.context.ApplicationEvent;
|
28 | 33 | import org.springframework.context.ApplicationListener;
|
| 34 | +import org.springframework.core.annotation.AnnotationAwareOrderComparator; |
29 | 35 | import org.springframework.core.annotation.AnnotationUtils;
|
30 | 36 | import org.springframework.data.rest.core.annotation.HandleAfterCreate;
|
31 | 37 | import org.springframework.data.rest.core.annotation.HandleAfterDelete;
|
@@ -165,37 +171,50 @@ private <T extends Annotation> void inspect(Object handler, Method method, Class
|
165 | 171 | throw new IllegalStateException(String.format(PARAMETER_MISSING, method));
|
166 | 172 | }
|
167 | 173 |
|
168 |
| - EventHandlerMethod handlerMethod = new EventHandlerMethod(parameterTypes[0], handler, method); |
| 174 | + EventHandlerMethod handlerMethod = EventHandlerMethod.of(parameterTypes[0], handler, method); |
169 | 175 |
|
170 | 176 | if (LOG.isDebugEnabled()) {
|
171 | 177 | LOG.debug("Annotated handler method found: {}", handlerMethod);
|
172 | 178 | }
|
173 | 179 |
|
174 |
| - handlerMethods.add(eventType, handlerMethod); |
| 180 | + List<EventHandlerMethod> events = handlerMethods.get(eventType); |
| 181 | + |
| 182 | + if (events == null) { |
| 183 | + events = new ArrayList<EventHandlerMethod>(); |
| 184 | + } |
| 185 | + |
| 186 | + if (events.isEmpty()) { |
| 187 | + handlerMethods.add(eventType, handlerMethod); |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + events.add(handlerMethod); |
| 192 | + Collections.sort(events); |
| 193 | + handlerMethods.put(eventType, events); |
175 | 194 | }
|
176 | 195 |
|
177 |
| - static class EventHandlerMethod { |
| 196 | + @ToString |
| 197 | + @EqualsAndHashCode |
| 198 | + @RequiredArgsConstructor |
| 199 | + static class EventHandlerMethod implements Comparable<EventHandlerMethod> { |
178 | 200 |
|
179 | 201 | final Class<?> targetType;
|
180 | 202 | final Method method;
|
181 | 203 | final Object handler;
|
182 | 204 |
|
183 |
| - private EventHandlerMethod(Class<?> targetType, Object handler, Method method) { |
184 |
| - |
185 |
| - this.targetType = targetType; |
186 |
| - this.method = method; |
187 |
| - this.handler = handler; |
| 205 | + public static EventHandlerMethod of(Class<?> targetType, Object handler, Method method) { |
188 | 206 |
|
189 |
| - ReflectionUtils.makeAccessible(this.method); |
| 207 | + ReflectionUtils.makeAccessible(method); |
| 208 | + return new EventHandlerMethod(targetType, method, handler); |
190 | 209 | }
|
191 | 210 |
|
192 |
| - /* |
| 211 | + /* |
193 | 212 | * (non-Javadoc)
|
194 |
| - * @see java.lang.Object#toString() |
| 213 | + * @see java.lang.Comparable#compareTo(java.lang.Object) |
195 | 214 | */
|
196 | 215 | @Override
|
197 |
| - public String toString() { |
198 |
| - return String.format("EventHandlerMethod{ targetType=%s, method=%s, handler=%s }", targetType, method, handler); |
| 216 | + public int compareTo(EventHandlerMethod o) { |
| 217 | + return AnnotationAwareOrderComparator.INSTANCE.compare(this.method, o.method); |
199 | 218 | }
|
200 | 219 | }
|
201 | 220 | }
|
0 commit comments