All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.dv8tion.jda.api.hooks.InterfacedEventManager Maven / Gradle / Ivy

Go to download

Java wrapper for the popular chat & VOIP service: Discord https://discord.com

There is a newer version: 5.1.0
Show newest version
/*
 * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors
 *
 * 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.dv8tion.jda.api.hooks;

import net.dv8tion.jda.api.events.GenericEvent;
import net.dv8tion.jda.internal.JDAImpl;
import net.dv8tion.jda.internal.utils.JDALogger;
import org.jetbrains.annotations.Unmodifiable;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * An {@link net.dv8tion.jda.api.hooks.IEventManager IEventManager} implementation
 * that uses the {@link net.dv8tion.jda.api.hooks.EventListener EventListener} interface for
 * event listeners.
 *
 * 

This only accepts listeners that implement {@link net.dv8tion.jda.api.hooks.EventListener EventListener} *
An adapter implementation is {@link net.dv8tion.jda.api.hooks.ListenerAdapter ListenerAdapter} which * provides methods for each individual {@link net.dv8tion.jda.api.events.Event}. * *

This is the default IEventManager used by JDA * * @see net.dv8tion.jda.api.hooks.AnnotatedEventManager * @see net.dv8tion.jda.api.hooks.IEventManager */ public class InterfacedEventManager implements IEventManager { private final CopyOnWriteArrayList listeners = new CopyOnWriteArrayList<>(); public InterfacedEventManager() { } /** * {@inheritDoc} * * @throws IllegalArgumentException * If the provided listener does not implement {@link net.dv8tion.jda.api.hooks.EventListener EventListener} */ @Override public void register(@Nonnull Object listener) { if (!(listener instanceof EventListener)) { throw new IllegalArgumentException("Listener must implement EventListener"); } listeners.add((EventListener) listener); } @Override public void unregister(@Nonnull Object listener) { if (!(listener instanceof EventListener)) { //noinspection ConstantConditions JDALogger.getLog(getClass()).warn( "Trying to remove a listener that does not implement EventListener: {}", listener == null ? "null" : listener.getClass().getName()); } //noinspection SuspiciousMethodCalls listeners.remove(listener); } @Nonnull @Override @Unmodifiable public List getRegisteredListeners() { return Collections.unmodifiableList(new ArrayList<>(listeners)); } @Override public void handle(@Nonnull GenericEvent event) { for (EventListener listener : listeners) { try { listener.onEvent(event); } catch (Throwable throwable) { JDAImpl.LOG.error("One of the EventListeners had an uncaught exception", throwable); if (throwable instanceof Error) throw (Error) throwable; } } } }