com.elusive_code.newsboy.EventServiceHelper Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of NewsBoy Show documentation
Show all versions of NewsBoy Show documentation
Asynchronous publish-subscribe communication library.
Features:
1. Listeners are stored using WeakReferences to prevent memory leaks when they are not unsubscribed.
2. Uses fork-join framework for concurrent event delivery.
3. Publish methods returns collection of Futures that represent event notifications.
4. Supports ordered publishing: guaranteed to notify of the events in order they were published.
5. Provides EventSource and EventService interfaces for better integration with IOC containers and alternative implementations.
The newest version!
/*
* Copyright 2014. Vladislav Dolgikh
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.elusive_code.newsboy;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Set;
/**
* Helper class that contain static methods
* @author Vladislav Dolgikh
*/
public class EventServiceHelper {
private EventServiceHelper(){}
/**
* Looks for objects' methods marked with {@link com.elusive_code.newsboy.Subscribe} annotation and
* creates a list of {@link com.elusive_code.newsboy.WeakEventHandler} for this object
*
* @param object for
* @return list of created event handlers
*/
public static LinkedList createObjectEventHandlers(Object object) {
Class clazz = object.getClass();
LinkedList handlers = new LinkedList<>();
for (Method m : clazz.getMethods()) {
for (Annotation a : m.getAnnotations()) {
if (Subscribe.class.equals(a.annotationType())) {
handlers.add(new WeakEventHandler(object, m));
}
}
}
return handlers;
}
/**
* Recursively collects class hierarchy.
*
* Convenience overload for {@link #collectClassHierarchy(Class, java.util.Set)}
* with second parameter set to null.
*
* @param clazz class which hierarchy to collect
* @return collection of classes that form hierarchy of supplied class
*/
public static Set collectClassHierarchy(Class clazz) {
return collectClassHierarchy(clazz,null);
}
/**
* Recursively collects all parent classes and interfaces of the supplied class.
*
* If second {@link java.util.Set} parameter is not null it puts collected classes into it and returns it,
* otherwise creates new set.
*
* @param clazz class which hierarchy to collect
* @param result set to put collected classes into, if null new one created
* @return {@link java.util.Set} of classes that form hierarchy of supplied class
*/
public static Set collectClassHierarchy(Class clazz, Set result) {
if (result == null) result = new HashSet<>();
result.add(clazz);
//interfaces
for (Class c : clazz.getInterfaces()) {
collectClassHierarchy(c, result);
}
//parent class
Class c = clazz.getSuperclass();
if (c != null) {
collectClassHierarchy(c, result);
}
return result;
}
}