Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.joepritzel.feather.strategy.subscribe;
  
  import java.util.List;
  import java.util.Map;
 
A pretty fast and general purpose SubscribeStrategy.

Author(s):
Joe Pritzel
 
 public class FastSubscribeUnsubscribe implements SubscribeStrategy {
 
 	public void subscribe(
 			Subscriber<?> subscriber) {
 		Class<?> type = null;
 		Method[] methods = subscriber.getClass().getMethods();
 		for (Method m : methods) {
 			if (m.getName().equals("receive") && !m.isSynthetic()
 					&& !m.isBridge()) {
 				type = m.getParameterTypes()[0];
 				break;
 			}
 		}
 		
 		SubscriberParent p = getSubParent(subscriber);
 		list.add(p);
 		if (mapping.putIfAbsent(typelist) != null) {
 			mapping.get(type).add(p);
 		}
 	}
 
 	public <T> void subscribe(
 			Subscriber<?> subscriberClass<T> messageType) {
 		try {
 			subscriber.getClass().getDeclaredMethod("receive"messageType);
 		} catch (NoSuchMethodException e) {
 		} catch (SecurityException e) {
 			// Do nothing.
 			// If the issue is present, then the type mismatch will occur
 			// naturally when it's trying to cast it.
 		}
 		SubscriberParent p = getSubParent(subscriber);
 		list.add(p);
 		if (mapping.putIfAbsent(messageTypelist) != null) {
 			mapping.get(messageType).add(p);
 		}
 	}
 
 	public <T> List<Subscriber<T>> unsubscribeAll(
 			Class<T> messageType) {
 		List<SubscriberParentlist = mapping.remove(messageType);
 		List<Subscriber<T>> realList = new ArrayList<>(list.size());
 		for (SubscriberParent sp : list) {
 			@SuppressWarnings("unchecked")
 			Subscriber<T> s = (Subscriber<T>) sp.getSubscriber();
 			if (s != null) {
 				realList.add(s);
 			}
 		}
 		return realList;
 	}
 
 	public <S extends Subscriber<T>, T> boolean unsubscribeByTypes(
 			S subscriberTypeClass<T> messageType) {
 		boolean ret = false;
 		List<SubscriberParentlist = mapping.get(messageType);
 		if (list != null) {
 			for (SubscriberParent p : list) {
 				Subscriber<?> s = p.getSubscriber();
 				if (s != null
 						&& subscriberType.getClass().isAssignableFrom(
 								s.getClass())) {
 					list.remove(p);
 					ret = true;
 				}
 			}
		}
		return ret;
	}
	public <T> boolean unsubscribe(
			Subscriber<T> sClass<T> messageType) {
		List<SubscriberParentlist = mapping.get(messageType);
		if (list != null) {
			return list.remove(getSubParent(s));
		}
		return false;
	}

A map of subscribers to their subscriber parents.
	private final Map<Subscriber<?>, SubscriberParentsubscriberParents = Collections
A lock for the subscriberParents field.
	private final ReentrantLock parentLock = new ReentrantLock();

Parameters:
subscriber - The subscriber to wrap.
Returns:
Returns a com.joepritzel.feather.internal.SubscriberParent to be used in the mapping.
	private SubscriberParent getSubParent(Subscriber<?> subscriber) {
		try {
			if (p == null) {
				p = new SubscriberParent(subscriber);
				.put(subscriberp);
			}
			return p;
finally {
		}
	}
New to GrepCode? Check out our FAQ X