EventManger.java

  1. /**
  2.  *
  3.  * Copyright 2015 Florian Schmaus
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smack.util;

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

  20. /**
  21.  * The event manager class is used to perform actions and wait for an event, which is usually caused by the action (or maybe never occurs).
  22.  * <p>
  23.  * Events are distinguished by an unique event key. They can produce an event result, which can simply be null.
  24.  * </p>
  25.  * <p>
  26.  * The action is able to throw an exception.
  27.  * </p>
  28.  *
  29.  * @param <K> the event key.
  30.  * @param <R> the event result.
  31.  * @param <E> the exception which could be thrown by the action.
  32.  */
  33. public class EventManger<K, R, E extends Exception> {

  34.     private final Map<K, Reference<R>> events = new ConcurrentHashMap<>();

  35.     /**
  36.      * Perform an action and wait for an event.
  37.      * <p>
  38.      * The event is signaled with {@link #signalEvent(Object, Object)}.
  39.      * </p>
  40.      *
  41.      * @param eventKey the event key, must not be null.
  42.      * @param timeout the timeout to wait for the event in milliseconds.
  43.      * @param action the action to perform prior waiting for the event, must not be null.
  44.      * @return the event value, may be null.
  45.      * @throws InterruptedException if interrupted while waiting for the event.
  46.      * @throws E depending on the concrete use case.
  47.      */
  48.     public R performActionAndWaitForEvent(K eventKey, long timeout, Callback<E> action) throws InterruptedException, E {
  49.         final Reference<R> reference = new Reference<>();
  50.         events.put(eventKey, reference);
  51.         try {
  52.             synchronized (reference) {
  53.                 action.action();
  54.                 reference.wait(timeout);
  55.             }
  56.             return reference.eventResult;
  57.         }
  58.         finally {
  59.             events.remove(eventKey);
  60.         }
  61.     }

  62.     /**
  63.      * Signal an event and the event result.
  64.      * <p>
  65.      * This method will return <code>false</code> if the event was not created with
  66.      * {@link #performActionAndWaitForEvent(Object, long, Callback)}.
  67.      * </p>
  68.      *
  69.      * @param eventKey the event key, must not be null.
  70.      * @param eventResult the event result, may be null.
  71.      * @return true if the event was found and signaled, false otherwise.
  72.      */
  73.     public boolean signalEvent(K eventKey, R eventResult) {
  74.         final Reference<R> reference = events.get(eventKey);
  75.         if (reference == null) {
  76.             return false;
  77.         }
  78.         reference.eventResult = eventResult;
  79.         synchronized (reference) {
  80.             reference.notifyAll();
  81.         }
  82.         return true;
  83.     }

  84.     private static class Reference<V> {
  85.         volatile V eventResult;
  86.     }

  87.     public interface Callback<E extends Exception> {
  88.         void action() throws E;
  89.     }
  90. }