Packagenet.guttershark.util
Classpublic final class Assertions

The Assertions class is a singleton that provides assertions for conditionals, and relieve's defensive programming for methods that require arguments.

All of the methods can be used for conditional assertions.


Example
Using assertions for conditionals:
 
  var ast:Assertions = Assertions.gi();
  var t:String = " ";
  if(ast.emptyString(t)) trace("it's empty");
  t = "word";
  if(ast.emptyString(t)) trace("doh"); //this wont trace, because the assertion evaluates to false.
  

You can also use the assertion class as defense when checking for valid method arguments and throw errors.

Using the assertions class for method argument defense:
 
  var ast:Assertions = Assertions.gi();
  function setSomething(val:Number):void
  {
      ast.notNull(val,"Parameter val cannot be null"); //throws argument error if val is null
      somthing = val;
  }
  

You can also change the exception type that get's thrown if an assertion fails.

Changing the exception type:
 
  var ast:Assertions = Assertions.gi();
  function setSomething(mc:MovieClip):void
  {
     ast.compatible(mc,MovieClip,"Parameter val must be a MovieClip",TypeError);
  }
  

Extended example:
 
  public class MyMC extends CoreClip
  {
      
      //protected var ast:Assertion; //ast is a protected variable on CoreClip, and CoreSprite.
      
      public function setSomeValue(value:Object):void
      {
          ast.notNil(value,"Parameter value cannot be null"); //argument error if value is null
      }
      
      public function setSomeArray(array:Array):void
      {
          ast.notNilOrEmpty(array,"Parameter array cannot be null"); //argument error if array is null or length==0
      }
  }
  



Public Properties
 PropertyDefined by
  onAssertionFail : Function
A function delegate you can define, which will be called when an assertion fails - this is intended to be used if you wanted to post the assertion to an http service for logging errors.
Assertions
Public Methods
 MethodDefined by
  
compatible(obj:*, type:Class, message:String = null, exceptionType:Class = null):Boolean
Assert if an object is compatible with another type.
Assertions
  
contains(str:String, pat:String, message:String = null, exceptionType:Class = null):Boolean
Assert that a string contains another string.
Assertions
  
different(value:*, otherValue:String, message:Class = null, exceptionType:* = null):Boolean
Assert that a value is defferent from another value.
Assertions
  
emptyString(str:String, message:String = null, exceptionType:Class = null):Boolean
Assert a string as being empty (zero characters or all spaces).
Assertions
  
equal(value:*, otherValue:String, message:Class = null, exceptionType:* = null):Boolean
Assert that a value is equal to another value.
Assertions
  
[static] Singleton access.
Assertions
  
greater(value:Number, minimum:Number = -1, message:String = null, exceptionType:Class = null):Boolean
Assert that a value is greater than a minimum number.
Assertions
  
nil(obj:*, message:String = null, exceptionType:Class = null):Boolean
Assert if an object is nil.
Assertions
  
nilOrEmpty(array:Array, message:String = null, exceptionType:Class = null):Boolean
Assert if an Array is nil or empty.
Assertions
  
notCompatible(obj:*, type:Class, message:String = null, exceptionType:Class = null):Boolean
Assert if an object is not compatible with another type.
Assertions
  
notEmptyString(str:String, message:String = null, exceptionType:Class = null):Boolean
Assert a string as being not empty (zero characters or all spaces).
Assertions
  
notNil(object:*, message:String = null, exceptionType:Class = null):Boolean
Assert if an object is not nil.
Assertions
  
notNilOrEmpty(array:Array, message:String = null, exceptionType:Class = null):Boolean
Assert if an Array is not nil or empty.
Assertions
  
notNumberString(str:String, message:String = null, exceptionType:Class = null):Boolean
Assert that a string does not have all number characters.
Assertions
  
numberString(str:String, message:String = null, exceptionType:Class = null):Boolean
Assert that a string has all number characters.
Assertions
  
smaller(value:Number, maxmimum:Number = 0, message:String = null, exceptionType:Class = null):Boolean
Assert that a value is smaller than a maximum number.
Assertions
Property detail
onAssertionFailproperty
public var onAssertionFail:Function

A function delegate you can define, which will be called when an assertion fails - this is intended to be used if you wanted to post the assertion to an http service for logging errors.


Example
Using the onAssertionFail delegate:
 
   var ast:Assertions = Assertions.gi();
   ast.onAssertionFail = onAssertionFail;
   private function onAssertionFail(msg:String):void
   {
       trace(msg);
   }
   

Method detail
compatible()method
public function compatible(obj:*, type:Class, message:String = null, exceptionType:Class = null):Boolean

Assert if an object is compatible with another type.

Parameters
obj:* — The object to evaluate.
 
type:Class — A message to throw if the assertion evaluates to false.
 
message:String (default = null) — The exceptionType to throw if an exception is being thrown.
 
exceptionType:Class (default = null)

Returns
Boolean
contains()method 
public function contains(str:String, pat:String, message:String = null, exceptionType:Class = null):Boolean

Assert that a string contains another string.

Parameters
str:String — The string to search in.
 
pat:String — The pattern that will be searched for in str.
 
message:String (default = null)
 
exceptionType:Class (default = null)

Returns
Boolean
different()method 
public function different(value:*, otherValue:String, message:Class = null, exceptionType:* = null):Boolean

Assert that a value is defferent from another value.

Parameters
value:* — The value to test.
 
otherValue:String — The other value to compare with value (this is actually typed as *, but asdocs changes it to String for some reason).
 
message:Class (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:* (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean
emptyString()method 
public function emptyString(str:String, message:String = null, exceptionType:Class = null):Boolean

Assert a string as being empty (zero characters or all spaces).

Parameters
str:String — The string to evaluate.
 
message:String (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:Class (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean
equal()method 
public function equal(value:*, otherValue:String, message:Class = null, exceptionType:* = null):Boolean

Assert that a value is equal to another value.

Parameters
value:* — The value to test.
 
otherValue:String — The other value to compare with value (this is actually typed as *, but asdocs changes it to String for some reason).
 
message:Class (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:* (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean
gi()method 
public static function gi():Assertions

Singleton access.

Returns
Assertions
greater()method 
public function greater(value:Number, minimum:Number = -1, message:String = null, exceptionType:Class = null):Boolean

Assert that a value is greater than a minimum number.

Parameters
value:Number — The value to test.
 
minimum:Number (default = -1) — The minimum number that the value must be greater than.
 
message:String (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:Class (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean
nil()method 
public function nil(obj:*, message:String = null, exceptionType:Class = null):Boolean

Assert if an object is nil.

Parameters
obj:* — The object to evaluate.
 
message:String (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:Class (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean
nilOrEmpty()method 
public function nilOrEmpty(array:Array, message:String = null, exceptionType:Class = null):Boolean

Assert if an Array is nil or empty.

Parameters
array:Array — The array to evaluate.
 
message:String (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:Class (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean
notCompatible()method 
public function notCompatible(obj:*, type:Class, message:String = null, exceptionType:Class = null):Boolean

Assert if an object is not compatible with another type.

Parameters
obj:* — The object to evaluate.
 
type:Class — A message to throw if the assertion evaluates to false.
 
message:String (default = null) — The exceptionType to throw if an exception is being thrown.
 
exceptionType:Class (default = null)

Returns
Boolean
notEmptyString()method 
public function notEmptyString(str:String, message:String = null, exceptionType:Class = null):Boolean

Assert a string as being not empty (zero characters or all spaces).

Parameters
str:String — The string to evaluate.
 
message:String (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:Class (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean
notNil()method 
public function notNil(object:*, message:String = null, exceptionType:Class = null):Boolean

Assert if an object is not nil.

Parameters
object:* — The object to evaluate.
 
message:String (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:Class (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean
notNilOrEmpty()method 
public function notNilOrEmpty(array:Array, message:String = null, exceptionType:Class = null):Boolean

Assert if an Array is not nil or empty.

Parameters
array:Array — The array to evaluate.
 
message:String (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:Class (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean
notNumberString()method 
public function notNumberString(str:String, message:String = null, exceptionType:Class = null):Boolean

Assert that a string does not have all number characters.

Parameters
str:String — The string to evaluate.
 
message:String (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:Class (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean
numberString()method 
public function numberString(str:String, message:String = null, exceptionType:Class = null):Boolean

Assert that a string has all number characters.

Parameters
str:String — The string to evaluate.
 
message:String (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:Class (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean
smaller()method 
public function smaller(value:Number, maxmimum:Number = 0, message:String = null, exceptionType:Class = null):Boolean

Assert that a value is smaller than a maximum number.

Parameters
value:Number — The value to test.
 
maxmimum:Number (default = 0) — The maximum number that the value must be smaller than.
 
message:String (default = null) — A message to throw if the assertion evaluates to false.
 
exceptionType:Class (default = null) — The exceptionType to throw if an exception is being thrown.

Returns
Boolean