// Path: src/main/java/org/pdfsam/eventstudio/util/RequireUtils.java
// public static void requireNotNull(Object victim) {
//     if (victim == null) {
//         throw new IllegalArgumentException("The input object cannot be null");
//     }
// }
// 
// Path: src/main/java/org/pdfsam/eventstudio/Annotations.java
// static class ReflectiveListenerDescriptor {
// 
//     private final EventListener listenerAnnotation;
//     private final Method method;
// 
//     public ReflectiveListenerDescriptor(EventListener listenerAnnotation, Method method) {
//         this.listenerAnnotation = listenerAnnotation;
//         this.method = method;
//     }
// 
//     public EventListener getListenerAnnotation() {
//         return listenerAnnotation;
//     }
// 
//     public Method getMethod() {
//         return method;
//     }
// }
// 
// Path: src/main/java/org/pdfsam/eventstudio/exception/BroadcastInterruptionException.java
// public class BroadcastInterruptionException extends EventStudioException {
// 
//     public BroadcastInterruptionException(String message) {
//         super(message);
//     }
// 
// }
// 
// Path: src/main/java/org/pdfsam/eventstudio/exception/EventStudioException.java
// public class EventStudioException extends RuntimeException {
// 
//     public EventStudioException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public EventStudioException(String message) {
//         super(message);
//     }
// 
//     public EventStudioException(Throwable cause) {
//         super(cause);
//     }
// 
// }


import org.pdfsam.eventstudio.Annotations.ReflectiveListenerDescriptor;
import org.pdfsam.eventstudio.exception.BroadcastInterruptionException;
import org.pdfsam.eventstudio.exception.EventStudioException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.pdfsam.eventstudio.util.RequireUtils.requireNotNull;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.locks.ReentrantReadWriteLock;
/*
 * This file is part of the EventStudio source code
 * Created on 11/nov/2013
 *  Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org).
 *
 * 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 org.pdfsam.eventstudio;




/**
 * A thread-safe holder for the listeners
 *
 * @author Andrea Vacondio
 */
class Listeners {

    private static final Logger LOG = LoggerFactory.getLogger(Listeners.class);

    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    private final Map<Class<?>, TreeSet<ListenerReferenceHolder>> listeners = new HashMap<>();

    <T> void add(Class<T> eventClass, Listener<T> listener, int priority, ReferenceStrength strength) {
        lock.writeLock().lock();
        try {
            TreeSet<ListenerReferenceHolder> set = nullSafeGetListenerHolders(eventClass);
            set.add(new ListenerReferenceHolder(priority, strength.getReference(new DefaultListenerWrapper(listener))));
        } finally {
            lock.writeLock().unlock();
        }
    }

// Path: src/main/java/org/pdfsam/eventstudio/util/RequireUtils.java
// public static void requireNotNull(Object victim) {
//     if (victim == null) {
//         throw new IllegalArgumentException("The input object cannot be null");
//     }
// }
// 
// Path: src/main/java/org/pdfsam/eventstudio/Annotations.java
// static class ReflectiveListenerDescriptor {
// 
//     private final EventListener listenerAnnotation;
//     private final Method method;
// 
//     public ReflectiveListenerDescriptor(EventListener listenerAnnotation, Method method) {
//         this.listenerAnnotation = listenerAnnotation;
//         this.method = method;
//     }
// 
//     public EventListener getListenerAnnotation() {
//         return listenerAnnotation;
//     }
// 
//     public Method getMethod() {
//         return method;
//     }
// }
// 
// Path: src/main/java/org/pdfsam/eventstudio/exception/BroadcastInterruptionException.java
// public class BroadcastInterruptionException extends EventStudioException {
// 
//     public BroadcastInterruptionException(String message) {
//         super(message);
//     }
// 
// }
// 
// Path: src/main/java/org/pdfsam/eventstudio/exception/EventStudioException.java
// public class EventStudioException extends RuntimeException {
// 
//     public EventStudioException(String message, Throwable cause) {
//         super(message, cause);
//     }
// 
//     public EventStudioException(String message) {
//         super(message);
//     }
// 
//     public EventStudioException(Throwable cause) {
//         super(cause);
//     }
// 
// }


// Path: src/main/java/org/pdfsam/eventstudio/Listeners.java
import org.pdfsam.eventstudio.Annotations.ReflectiveListenerDescriptor;
import org.pdfsam.eventstudio.exception.BroadcastInterruptionException;
import org.pdfsam.eventstudio.exception.EventStudioException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.pdfsam.eventstudio.util.RequireUtils.requireNotNull;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.locks.ReentrantReadWriteLock;

/*
 * This file is part of the EventStudio source code
 * Created on 11/nov/2013
 *  Copyright 2020 by Sober Lemur S.a.s di Vacondio Andrea (info@pdfsam.org).
 *
 * 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 org.pdfsam.eventstudio;




/**
 * A thread-safe holder for the listeners
 *
 * @author Andrea Vacondio
 */
class Listeners {

    private static final Logger LOG = LoggerFactory.getLogger(Listeners.class);

    private final ReentrantReadWriteLock lock = new ReentrantReadWriteLock();
    private final Map<Class<?>, TreeSet<ListenerReferenceHolder>> listeners = new HashMap<>();

    <T> void add(Class<T> eventClass, Listener<T> listener, int priority, ReferenceStrength strength) {
        lock.writeLock().lock();
        try {
            TreeSet<ListenerReferenceHolder> set = nullSafeGetListenerHolders(eventClass);
            set.add(new ListenerReferenceHolder(priority, strength.getReference(new DefaultListenerWrapper(listener))));
        } finally {
            lock.writeLock().unlock();
        }
    }

    public Set<Class<?>> addAll(Object bean, List<ReflectiveListenerDescriptor> descriptors) {