package org.apache.wicket.atmosphere;

import java.lang.reflect.Method;
import java.util.Objects;

import org.apache.wicket.Component;
import org.apache.wicket.WicketRuntimeException;

import com.google.common.base.Predicate;
import com.google.common.base.Predicates;

public class EventSubscription
{
	private String componentPath;

	private String methodName;

	private Predicate<Object> filter;

	public EventSubscription(Component component, Method method)
	{
		componentPath = component.getPageRelativePath();
		Class< ? > eventType = method.getParameterTypes()[1];
		filter = Predicates.and(Predicates.instanceOf(eventType), createFilter(method));
		methodName = method.getName();
	}

	@SuppressWarnings("unchecked")
	private static Predicate<Object> createFilter(Method method)
	{
		Subscribe subscribe = method.getAnnotation(Subscribe.class);
		try
		{
			return (Predicate<Object>) subscribe.filter().newInstance();
		}
		catch (InstantiationException e)
		{
			throw new WicketRuntimeException(e);
		}
		catch (IllegalAccessException e)
		{
			throw new WicketRuntimeException(e);
		}
	}

	public String getComponentPath()
	{
		return componentPath;
	}

	public Predicate<Object> getFilter()
	{
		return filter;
	}

	public String getMethodName()
	{
		return methodName;
	}

	@Override
	public int hashCode()
	{
		return Objects.hash(componentPath, methodName);
	}

	@Override
	public boolean equals(Object obj)
	{
		if (obj instanceof EventSubscription)
		{
			EventSubscription other = (EventSubscription) obj;
			return Objects.equals(componentPath, other.getComponentPath())
				&& Objects.equals(methodName, other.getMethodName());
		}
		return false;
	}
}

@@ -68,34 +68,8 @@
 hod;
-%0Aimport java.util.Objects;
 %0A%0Aim
@@ -152,16 +152,55 @@
 ption;%0A%0A
+import com.google.common.base.Objects;%0A
 import c
@@ -533,11 +533,9 @@
 ass%3C
- ? 
+?
 %3E ev
@@ -894,17 +894,16 @@
 Object%3E)
- 
 subscrib
@@ -1346,16 +1346,20 @@
 cts.hash
+Code
 (compone
@@ -1524,17 +1524,16 @@
 ription)
- 
 obj;%0A%09%09%09
@@ -1552,17 +1552,16 @@
 ts.equal
-s
 (compone
@@ -1597,16 +1597,16 @@
 h())
+ &&
 %0A%09%09%09%09
-&& 
 Obje
@@ -1614,17 +1614,16 @@
 ts.equal
-s
 (methodN

use guava, not java 7 api