Use an IfClosure
, supplying a
Predicate
and two Closure
objects. If the Predicate
evaluates to true
, the first Closure
is executed; if the Predicate
evaluates to false
, the second Closure
is executed. The following closure
deals with a financial decision; a Predicate
, isWinning
, evaluates a Stock
object. If the purchase price is less
than the current price, the stock is a winner, causing the buy
Closure
to be executed. If the purchase price is higher than the current price,
the stock is a loser and it is sold by passing it to the sell
Closure
:
Closure sell = new Closure( ) { public void execute(Object input) { Stock stock = (Stock) input; stock.sell( stock.getShares( ) ); System.out.println( "\tSold all shares" ); } } Closure buy = new Closure( ) { public void execute(Object input) { Stock stock = (Stock) input; int buyShares = stock.getShares( ) * 0.10; stock.buy( buyShares ); System.out.println( "\tBought " + buyShares ); } } Predicate isWinning = new Predicate( ) { public boolean evaluate(Object object) { Stock stock = (Stock) object; if( stock.getPurchasePrice( ) < stock.getCurrentPrice( ) ) { System.out.println( stock.getSymbol( ) + " is a winner"; return true; } else { System.out.println( stock.getSymbol( ) + " is a loser"; return false; } } } Closure stockAction = new IfClosure( isWinning, buy, sell ); Stock yahoo = new Stock("YHOO"); yahoo.setPurchasePrice( 10.0 ); yahoo.setCurrentPrice( 20.0 ); yahoo.setShares( 100 ); Stock tibco = new Stock("TIB"); tibco.setPurchasePrice( 50.0 ); tibco.setCurrentPrice( 30.0 ); tibco.setShares( 50 ); // Execute the IfClosure, take action on stocks based on performance stockAction.execute( yahoo ); stockAction.execute( tibco );
The example evaluates two stocks, a winner and a loser. The following output is generated:
YHOO is a winner Bought 10 shares TIB is a loser Sold All Shares
Because an IfClosure
is an
implementation of a Closure
, you can
nest IfClosures
within other IfClosures
. The following code uses the
Closure
objects and the Predicate
defined in the Solution, adding a
third Closure
, isUnchanged
, to create a nested IfClosure--sellOrHold
:
Predicate isUnchanged = new Predicate( ) { public boolean evaluate(Object object) { Stock stock = (Stock) object; if( stock.getPurchasePrice( ) == stock.getCurrentPrice( ) ) { System.out.println( stock.getSymbol( ) + " is unchanged"; return true; } return false; } } Closure sellOrHold = new IfClosure( isUnchanged, new NOPClosure( ), sell ); Closure stockAction = new IfClosure( isWinning, buy, sellOrHold ); Stock tibco = new Stock("TIB"); tibco.setPurchasePrice( 50.0 ); tibco.setCurrentPrice( 30.0 ); tibco.setShares( 50 ); Stock lucent = new Stock("LU"); tibco.setPurchasePrice( 30.0 ); tibco.setCurrentPrice( 30.0 ); tibco.setShares( 150 ); stockAction.execute( tibco ); stockAction.execute( lucent );
When stockAction
is executed,
and a Stock
does not satisfy the
isWinning
Predicate
, it is passed to the sellOrHold
Closure
, which is another IfClosure
. The sellOrHold
Closure
then evaluates the Stock
to see if it is a loser or unchanged. If
the stock price is unchanged, it is passed to a NOPClosure
, which performs no operation on the
Stock
object. If the stock price is
less than the purchase price, the Stock
is passed to the sell
Closure
.