/*
 * Decompiled with CFR 0.152.
 */
package org.jivesoftware.smack.util;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class EventManger<K, R, E extends Exception> {
    private final Map<K, Reference<R>> events = new ConcurrentHashMap<K, Reference<R>>();

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public R performActionAndWaitForEvent(K eventKey, long timeout, Callback<E> action) throws InterruptedException, E {
        Reference reference = new Reference();
        this.events.put(eventKey, reference);
        try {
            Reference reference2 = reference;
            synchronized (reference2) {
                action.action();
                reference.wait(timeout);
            }
            reference2 = reference.eventResult;
            return (R)reference2;
        }
        finally {
            this.events.remove(eventKey);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public boolean signalEvent(K eventKey, R eventResult) {
        Reference<R> reference = this.events.get(eventKey);
        if (reference == null) {
            return false;
        }
        reference.eventResult = eventResult;
        Reference<R> reference2 = reference;
        synchronized (reference2) {
            reference.notifyAll();
        }
        return true;
    }

    public static interface Callback<E extends Exception> {
        public void action() throws E;
    }

    private static class Reference<V> {
        volatile V eventResult;

        private Reference() {
        }
    }
}
