net.kuujo.copycat.internal.event.DefaultEventContext Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2014 the original author or authors.
*
* 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 net.kuujo.copycat.internal.event;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import net.kuujo.copycat.event.Event;
import net.kuujo.copycat.event.EventContext;
import net.kuujo.copycat.event.EventHandler;
import net.kuujo.copycat.event.EventHandlerRegistry;
import net.kuujo.copycat.internal.util.concurrent.NamedThreadFactory;
/**
* Default event context.
*
* @author Jordan Halterman
*
* @param The event type.
*/
public class DefaultEventContext implements EventContext, EventHandler {
private static final ThreadFactory THREAD_FACTORY = new NamedThreadFactory("event-handler-%s");
private final Executor executor = Executors.newCachedThreadPool(THREAD_FACTORY);
private HandlerHolder handler;
private class HandlerHolder {
private final EventHandler handler;
private final boolean once;
private final boolean async;
private HandlerHolder(EventHandler handler, boolean once, boolean async) {
this.handler = handler;
this.once = once;
this.async = async;
}
public void run(E event) {
if (async) {
executor.execute(() -> handler.handle(event));
} else {
handler.handle(event);
}
if (once) {
DefaultEventContext.this.handler = null;
}
}
}
public DefaultEventContext(EventHandlerRegistry registry) {
registry.registerHandler(this);
}
@Override
public void handle(E event) {
if (handler != null) {
handler.run(event);
}
}
@Override
public EventContext run(EventHandler handler) {
this.handler = new HandlerHolder(handler, false, false);
return this;
}
@Override
public EventContext runAsync(EventHandler handler) {
this.handler = new HandlerHolder(handler, false, true);
return this;
}
@Override
public EventContext runOnce(EventHandler handler) {
this.handler = new HandlerHolder(handler, true, false);
return this;
}
@Override
public EventContext runOnceAsync(EventHandler handler) {
this.handler = new HandlerHolder(handler, true, true);
return this;
}
@Override
public String toString() {
return getClass().getSimpleName();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy