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, EventManger.Reference<R>> events = new ConcurrentHashMap();

   public R performActionAndWaitForEvent(K eventKey, long timeout, EventManger.Callback<E> action) throws InterruptedException, E {
      EventManger.Reference<R> reference = new EventManger.Reference();
      this.events.put(eventKey, reference);

      Object var6;
      try {
         synchronized(reference) {
            action.action();
            reference.wait(timeout);
         }

         var6 = reference.eventResult;
      } finally {
         this.events.remove(eventKey);
      }

      return var6;
   }

   public boolean signalEvent(K eventKey, R eventResult) {
      EventManger.Reference<R> reference = (EventManger.Reference)this.events.get(eventKey);
      if (reference == null) {
         return false;
      } else {
         reference.eventResult = eventResult;
         synchronized(reference) {
            reference.notifyAll();
            return true;
         }
      }
   }

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

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

      private Reference() {
      }

      // $FF: synthetic method
      Reference(Object x0) {
         this();
      }
   }
}
