cz.cuni.amis.utils.future
Class FlagFuture<Result,FlagType>

Package class diagram package FlagFuture
java.lang.Object
  extended by cz.cuni.amis.utils.future.FlagFuture<Result,FlagType>
Type Parameters:
Result -
FlagType -
All Implemented Interfaces:
FlagListener<FlagType>, IFuture<Result>, Future<Result>, EventListener

public class FlagFuture<Result,FlagType>
extends Object
implements IFuture<Result>, FlagListener<FlagType>

Future implementation that is listening on a flag and when it's terminal state is set on the flag the future completes itself with result specified in the map (under the key of the flag value).

Thread-safe implementation, not as easy as it seems ;-)

Author:
Jimmy

Nested Class Summary
 
Nested classes/interfaces inherited from interface cz.cuni.amis.utils.flag.FlagListener
FlagListener.FlagListenerNotifier<T>
 
Constructor Summary
FlagFuture(Flag<FlagType> waitFlag, FlagType terminalFlagValue, Result resultValue)
          Initializing future to wait for 'terminalFlagValue' at 'waitFlag', when that happens complete itself with result 'resultValue'.
FlagFuture(Flag<FlagType> waitFlag, Map<FlagType,Result> terminalMap)
          In constructor you have to specify a flag where the future should listen at + terminal states for the future (terminalMap).
 
Method Summary
 boolean cancel(boolean mayInterruptIfRunning)
           
 void flagChanged(FlagType changedValue)
           
 Result get()
          Waits if necessary for the computation to complete, and then retrieves its result.
 Result get(long timeout, TimeUnit unit)
          Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.
 boolean isCancelled()
           
 boolean isDone()
           
 void stop(Result result)
          Stops the future (not the task it represents!).
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FlagFuture

public FlagFuture(Flag<FlagType> waitFlag,
                  Map<FlagType,Result> terminalMap)
In constructor you have to specify a flag where the future should listen at + terminal states for the future (terminalMap).

Possible flag values and the behavior of the future:

  1. flag value is in the keys of terminalMap - the future completes with the result under the key from the flag
  2. flag value is not in the keys of terminalMap - the future ignores that value and waits for next flag change

Note that the flag value is examined during the construction of the object and if the flag has value that is in the terminalMap the future is completes itself.

Parameters:
waitFlag -
terminalMap -

FlagFuture

public FlagFuture(Flag<FlagType> waitFlag,
                  FlagType terminalFlagValue,
                  Result resultValue)
Initializing future to wait for 'terminalFlagValue' at 'waitFlag', when that happens complete itself with result 'resultValue'.

Parameters:
waitFlag -
terminalFlagValue -
resultValue -
Method Detail

stop

public void stop(Result result)
Stops the future (not the task it represents!). It raise the latch unblocking all threads waiting for the future to complete. Sets the result to desired value.

Note that this happened IFF !isDone(), e.g. if the future is already done this won't do anything (as latch is already raised and the result value has been determined).

Note that this is different behavior that cancel() should implement, therefore it's a different method.


cancel

public boolean cancel(boolean mayInterruptIfRunning)
Specified by:
cancel in interface Future<Result>

get

public Result get()
Description copied from interface: IFuture
Waits if necessary for the computation to complete, and then retrieves its result.

Specified by:
get in interface IFuture<Result>
Specified by:
get in interface Future<Result>
Returns:
the computed result

get

public Result get(long timeout,
                  TimeUnit unit)
Description copied from interface: IFuture
Waits if necessary for at most the given time for the computation to complete, and then retrieves its result, if available.

Specified by:
get in interface IFuture<Result>
Specified by:
get in interface Future<Result>
Parameters:
timeout - the maximum time to wait
unit - the time unit of the timeout argument
Returns:
the computed result

isCancelled

public boolean isCancelled()
Specified by:
isCancelled in interface Future<Result>

isDone

public boolean isDone()
Specified by:
isDone in interface Future<Result>

flagChanged

public void flagChanged(FlagType changedValue)
Specified by:
flagChanged in interface FlagListener<FlagType>


Copyright © 2014 AMIS research group, Faculty of Mathematics and Physics, Charles University in Prague, Czech Republic. All Rights Reserved.