Skip to content

Instantly share code, notes, and snippets.

@naphaso
Created October 18, 2013 01:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save naphaso/cc6d7b5da4de4c301f8d to your computer and use it in GitHub Desktop.
Save naphaso/cc6d7b5da4de4c301f8d to your computer and use it in GitHub Desktop.
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.ref.WeakReference;
import java.lang.reflect.Method;
/**
* User: Stanislav Ovsyannikov
* Date: 7/31/13
* Time: 6:47 PM
*/
class EventListener {
private static final Logger logger = LoggerFactory.getLogger(EventListener.class);
private final WeakReference<Object> o;
private final Method m;
public final Class<?> eventType;
public final ThreadType type;
public EventListener(Object o, Method m, Class<?> eventType, ThreadType type) {
this.o = new WeakReference<Object>(o);
this.m = m;
this.type = type;
this.eventType = eventType;
}
public void run(Object event) {
try {
final Object object = o.get();
if(object != null)
m.invoke(object, event);
} catch (Exception e) {
logger.error("exception durning event processing", e);
}
}
public boolean isValid() {
return o.get() != null;
}
@Override
public boolean equals(Object o1) {
if (this == o1) return true;
if (o1 == null || getClass() != o1.getClass()) return false;
EventListener that = (EventListener) o1;
if (!eventType.equals(that.eventType)) return false;
if (!m.equals(that.m)) return false;
if (!o.equals(that.o)) return false;
if (type != that.type) return false;
return true;
}
@Override
public int hashCode() {
int result = o.hashCode();
result = 31 * result + m.hashCode();
result = 31 * result + eventType.hashCode();
result = 31 * result + type.hashCode();
return result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment