Changeset 34043 in osm for applications/editors
- Timestamp:
- 2018-02-05T20:59:52+01:00 (7 years ago)
- Location:
- applications/editors/josm/plugins/eventbus/src/org/openstreetmap/josm
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/eventbus/src/org/openstreetmap/josm/eventbus/Dispatcher.java
r34000 r34043 42 42 * thread. That is, all subscribers to a single event A will be called before any subscribers to 43 43 * any events B and C that are posted to the event bus by the subscribers to A. 44 * @return dispatcher 44 45 */ 45 46 static Dispatcher perThreadDispatchQueue() { … … 52 53 * For async dispatch, an {@linkplain #immediate() immediate} dispatcher should generally be 53 54 * preferable. 55 * @return dispatcher 54 56 */ 55 57 static Dispatcher legacyAsync() { … … 61 63 * without using an intermediate queue to change the dispatch order. This is effectively a 62 64 * depth-first dispatch order, vs. breadth-first when using a queue. 65 * @return dispatcher 63 66 */ 64 67 static Dispatcher immediate() { … … 66 69 } 67 70 68 /** Dispatches the given {@code event} to the given {@code subscribers}. */ 71 /** 72 * Dispatches the given {@code event} to the given {@code subscribers}. 73 * @param event event to dispatch 74 * @param subscribers subscribers to notify 75 */ 69 76 abstract void dispatch(Object event, Iterator<Subscriber> subscribers); 70 77 -
applications/editors/josm/plugins/eventbus/src/org/openstreetmap/josm/eventbus/EventBus.java
r34000 r34043 100 100 private final Dispatcher dispatcher; 101 101 102 /** 103 * Simple executor singleton that runs commands in the same thread. 104 */ 102 105 enum DirectExecutor implements Executor { 106 /** unique instance */ 103 107 INSTANCE; 104 108 … … 142 146 } 143 147 148 /** 149 * Constructs a new {@code EventBus}. 150 * @param identifier the identifier for this event bus. Must not be null 151 * @param executor the default executor to use for dispatching events to subscribers. Must not be null 152 * @param dispatcher the event dispatcher. Must not be null 153 * @param exceptionHandler handles the exceptions thrown by a subscriber. Must not be null 154 */ 144 155 EventBus( 145 156 String identifier, … … 163 174 } 164 175 165 /** Returns the default executor this event bus uses for dispatching events to subscribers. */ 176 /** 177 * Returns the default executor this event bus uses for dispatching events to subscribers. 178 * @return the default executor this event bus uses for dispatching events to subscribers 179 */ 166 180 final Executor executor() { 167 181 return executor; 168 182 } 169 183 170 /** Handles the given exception thrown by a subscriber with the given context. */ 184 /** 185 * Handles the given exception thrown by a subscriber with the given context. 186 * @param e exception thrown by a subscriber 187 * @param context subscriber context 188 */ 171 189 void handleSubscriberException(Throwable e, SubscriberExceptionContext context) { 172 190 Objects.requireNonNull(e); … … 229 247 /** Simple logging handler for subscriber exceptions. */ 230 248 static final class LoggingHandler implements SubscriberExceptionHandler { 249 /** unique instance */ 231 250 static final LoggingHandler INSTANCE = new LoggingHandler(); 232 251 -
applications/editors/josm/plugins/eventbus/src/org/openstreetmap/josm/eventbus/Subscriber.java
r34000 r34043 31 31 class Subscriber { 32 32 33 /** Creates a {@code Subscriber} for {@code method} on {@code listener}. */ 33 /** 34 * Creates a {@code Subscriber} for {@code method} on {@code listener}. 35 * @param bus event bus 36 * @param listener listener 37 * @param method method 38 * @return subscriber 39 */ 34 40 static Subscriber create(EventBus bus, Object listener, Method method) { 35 41 return isDeclaredThreadSafe(method) … … 59 65 } 60 66 61 /** Dispatches {@code event} to this subscriber using the proper executor. */ 67 /** 68 * Dispatches {@code event} to this subscriber using the proper executor. 69 * @param event event to dispatch 70 */ 62 71 final void dispatchEvent(final Object event) { 63 72 executor.execute( 64 new Runnable() { 65 @Override 66 public void run() { 73 () -> { 67 74 try { 68 75 invokeSubscriberMethod(event); … … 70 77 bus.handleSubscriberException(e.getCause(), context(event)); 71 78 } 72 } 73 }); 79 }); 74 80 } 75 81 … … 77 83 * Invokes the subscriber method. This method can be overridden to make the invocation 78 84 * synchronized. 85 * @param event event to dispatch 86 * @throws InvocationTargetException if the invocation fails 79 87 */ 80 88 void invokeSubscriberMethod(Object event) throws InvocationTargetException { … … 93 101 } 94 102 95 /** Gets the context for the given event. */ 103 /** 104 * Gets the context for the given event. 105 * @param event event 106 * @return context for the given event 107 */ 96 108 private SubscriberExceptionContext context(Object event) { 97 109 return new SubscriberExceptionContext(bus, event, target, method); … … 118 130 * Checks whether {@code method} is thread-safe, as indicated by the presence of the {@link 119 131 * AllowConcurrentEvents} annotation. 132 * @param method method to check 133 * @return {@code true} if {@code method} is thread-safe 120 134 */ 121 135 private static boolean isDeclaredThreadSafe(Method method) { -
applications/editors/josm/plugins/eventbus/src/org/openstreetmap/josm/eventbus/SubscriberRegistry.java
r34000 r34043 54 54 private final EventBus bus; 55 55 56 /** 57 * Constructs a new {@code SubscriberRegistry}. 58 * @param bus event bus 59 */ 56 60 SubscriberRegistry(EventBus bus) { 57 61 this.bus = Objects.requireNonNull(bus); 58 62 } 59 63 60 /** Registers all subscriber methods on the given listener object. */ 64 /** 65 * Registers all subscriber methods on the given listener object. 66 * @param listener listener 67 */ 61 68 void register(Object listener) { 62 69 MultiMap<Class<?>, Subscriber> listenerMethods = findAllSubscribers(listener); … … 78 85 } 79 86 80 /** Unregisters all subscribers on the given listener object. */ 87 /** Unregisters all subscribers on the given listener object. 88 * @param listener listener 89 */ 81 90 void unregister(Object listener) { 82 91 MultiMap<Class<?>, Subscriber> listenerMethods = findAllSubscribers(listener); … … 101 110 } 102 111 112 /** 113 * Returns subscribers for given {@code eventType}. Only used for unit tests. 114 * @param eventType event type 115 * @return subscribers for given {@code eventType}. Can be empty, but never null 116 */ 103 117 Set<Subscriber> getSubscribersForTesting(Class<?> eventType) { 104 118 return firstNonNull(subscribers.get(eventType), new HashSet<Subscriber>()); … … 108 122 * Gets an iterator representing an immutable snapshot of all subscribers to the given event at 109 123 * the time this method is called. 124 * @param event event 125 * @return subscribers iterator 110 126 */ 111 127 Iterator<Subscriber> getSubscribers(Object event) { … … 127 143 /** 128 144 * Returns all subscribers for the given listener grouped by the type of event they subscribe to. 145 * @param listener listener 146 * @return all subscribers for the given listener 129 147 */ 130 148 private MultiMap<Class<?>, Subscriber> findAllSubscribers(Object listener) { … … 175 193 * Flattens a class's type hierarchy into a set of {@code Class} objects including all 176 194 * superclasses (transitively) and all interfaces implemented by these superclasses. 195 * @param concreteClass concrete class 196 * @return set of {@code Class} objects including all superclasses and interfaces 177 197 */ 178 198 static Set<Class<?>> flattenHierarchy(Class<?> concreteClass) { … … 205 225 } 206 226 } 207 227 208 228 /** 209 229 * Returns the first of two given parameters that is not {@code null}, if either is, or otherwise … … 214 234 * Predicates.notNull())}, static importing as necessary. 215 235 * 216 * <p><b>Note:</b> if {@code first} is represented as an {@link Optional}, this can be 217 * accomplished with {@link Optional#or(Object) first.or(second)}. That approach also allows for 218 * lazy evaluation of the fallback instance, using {@link Optional#or(Supplier) 219 * first.or(supplier)}. 236 * @param <T> object type 237 * @param first first object 238 * @param second second object 220 239 * 221 240 * @return {@code first} if it is non-null; otherwise {@code second} if it is non-null … … 232 251 throw new NullPointerException("Both parameters are null"); 233 252 } 234 253 235 254 private static Set<Class<?>> getClassesAndInterfaces(Class<?> clazz) { 236 255 Set<Class<?>> result = new HashSet<>(); -
applications/editors/josm/plugins/eventbus/src/org/openstreetmap/josm/plugins/eventbus/EventBusPlugin.java
r34034 r34043 379 379 } 380 380 381 /** 382 * Registers all JOSM listeners. 383 */ 381 384 void registerAllJosmListeners() { 382 385 Main.addProjectionChangeListener(projectionChangeListener); … … 399 402 } 400 403 404 /** 405 * Unregisters all JOSM listeners. 406 */ 401 407 void unregisterAllJosmListeners() { 402 408 Main.removeProjectionChangeListener(projectionChangeListener);
Note:
See TracChangeset
for help on using the changeset viewer.