io.basestar.event.Handlers Maven / Gradle / Ivy
package io.basestar.event;
/*-
* #%L
* basestar-event
* %%
* Copyright (C) 2019 - 2020 Basestar.IO
* %%
* 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.
* #L%
*/
import com.google.common.collect.ImmutableList;
import lombok.Data;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;
public class Handlers {
@Data
public static class Mapping {
private final Class event;
private final UnboundHandler handler;
}
public interface UnboundHandler {
CompletableFuture> handle(T self, E event);
}
private final List> mappings;
public Handlers(final List> mappings) {
this.mappings = ImmutableList.copyOf(mappings);
}
@SuppressWarnings("unchecked")
public CompletableFuture> handle(final T self, final Event event) {
final Collection> handlers = mappings.stream()
.filter(e -> e.getEvent().equals(event.getClass()))
.map(Mapping::getHandler)
.collect(Collectors.toList());
final List> futures = new ArrayList<>();
for(final UnboundHandler handler : handlers) {
futures.add(((UnboundHandler)handler).handle(self, event));
}
return CompletableFuture.allOf(futures.toArray(new CompletableFuture>[0]));
}
public static class Builder {
private final List> mappings = new ArrayList<>();
public Builder on(final Class event, final UnboundHandler handler) {
mappings.add(new Mapping<>(event, handler));
return this;
}
public Builder on(final Class event, final Handler handler) {
mappings.add(new Mapping<>(event, (ignored, e) -> handler.handle(e)));
return this;
}
public Handlers build() {
return new Handlers<>(mappings);
}
}
public static Builder builder() {
return new Builder<>();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy