UnaryOr

The asdk.condition.UnaryOr class is used to add or logic to two UnaryConditions. It provides an implementation of the UnaryCondition interface that allows for more than one unary condition objects to be combined returning true if any of the unary conditions are true. Similar to the previous example, a UnaryOr condition could be used to find null or empty collections before processing its elements. The example below combines the asdk.condition.NotNull and asdk.condition.PositiveNumber conditions, both wrapped within UnaryNot conditions, to determine whether an ArrayList is null or empty, or contains at least one element.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import asdk.*;
import asdk.condition.*;

var nullList : ArrayList;
var emptyList : ArrayList = new ArrayList();
var arrayList : ArrayList = new ArrayList();
arrayList.addItem( "item1" );

var notNull:UnaryCondition = new NotNull();
var isNull:UnaryCondition = new UnaryNot( notNull );
var positiveNumber:UnaryCondition = new PositiveNumber();
var isEmpty:UnaryCondition = new UnaryNot( positiveNumber );
var orCondition:UnaryCondition = new UnaryOr( isNull, isEmpty );

var listIsNull = orCondition.execute( nullList.getSize() );
trace("listIsNull: " + listIsNull );
//displays listIsNull: true in output window

var listIsEmpty = orCondition.execute( emptyList.getSize() );
trace("listIsEmpty: " + listIsEmpty );
//displays listIsEmpty: true in output window

var listIsEmpty2 = orCondition.execute( arrayList.getSize() );
trace("listIsEmpty2: " + listIsEmpty2 );
//displays listIsEmpty2: false in output window

This example is interesting because it demonstrates that arbitrarily complex conditions can be combined to produce sophisticated rules. In the example above both the NotNull and PostiveNumber conditions are passed to UnaryNot conditions that are then encapsulated within a UnaryOr object. This has the effect of returning true when the object passed in is either null or less than one which is a valuable determination often times when processing a collection.