gausssoft.util
Class InstanceChecker

java.lang.Object
  |
  +--gausssoft.util.InstanceChecker
All Implemented Interfaces:
Cloneable, CloneableObject, Serializable

public class InstanceChecker
extends Object
implements CloneableObject, Serializable

The InstanceChecker class is an object that can be used to check the runtime type of an object.

The most usual use of this object would normally to check a group of objects to ensure they are of a certain type. Take this example code:

    public ExampleObject (Collection coll) {
       List args = new ArrayList(3);
       args[0] = java.lang.String.class;
       args[1] = java.lang.Integer.class;
       args[2] = getClass();
       InstanceChecker ic = new InstanceChecker (args, false);
       if (ic.matchesIn(coll)) {throw new IllegalArgumentException();}
       else {....}
    }
 
An InstanceChecker is created, which uses the Class objects of java.lang.String, java.lang.Integer and the ExampleObject class for comparison. It is set, so that if an object is compared against it, and the object being compared is not a String, Integer or ExampleObject, it returns true.

Each InstanceChecker is immutable after creation. An InstanceChecker is considered to be inclusive if the matches method returns true if an object is an instance of at least one of the classes specified. Otherwise, it is exclusive.

When arguments are passed at construction time to the InstanceChecker, the only allowed object types are Class objects.

Since:
GSDK 1.1
Author:
Allan Crooks
See Also:
Class, matches(Object), matchesIn(Collection), matchesIn(Object[]), Serialized Form

Constructor Summary
InstanceChecker(Class clazz)
          Creates an inclusive InstanceChecker from a single class.
InstanceChecker(Class[] classes)
          Creates an inclusive InstanceChecker from an array of classes.
InstanceChecker(Class[] classes, boolean incl)
          Creates an InstanceChecker from an array of classes, also specifying if this InstanceChecker is inclusive or not.
InstanceChecker(Class clazz, boolean incl)
          Creates an InstanceChecker from a single class, also specifying if this InstanceChecker is inclusive or not.
InstanceChecker(Collection coll)
          Creates an inclusive InstanceChecker from a collection of classes.
InstanceChecker(Collection coll, boolean incl)
          Creates an InstanceChecker from a collection of classes, also specifying if this InstanceChecker is inclusive or not.
InstanceChecker(InstanceChecker ic)
          Creates an InstanceChecker using the same data as another InstanceChecker.
 
Method Summary
 Set classes()
          Returns an immutable set containing all the classes this InstanceChecker represents.
 Object clone()
          Clones this InstanceChecker.
 boolean equals(Object obj)
          Compares this InstanceChecker against another object.
 int hashCode()
          Generates a hash code for this InstanceChecker.
 boolean isInclusive()
          Return true if this InstanceChecker is inclusive.
 boolean matches(Object obj)
          Checks to see if the supplied object matches the criteria by this InstanceChecker.
 boolean matchesIn(Collection coll)
          Checks to see if the supplied collection has an object that matches the criteria set by this InstanceChecker.
 boolean matchesIn(Object[] array)
          Checks to see if the supplied array has an object that matches the criteria set by this InstanceChecker.
 String toString()
          Creates a String representation of this object.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

InstanceChecker

public InstanceChecker(InstanceChecker ic)
Creates an InstanceChecker using the same data as another InstanceChecker.
Parameters:
ic - The InstanceChecker to use.
Throws:
NullPointerException - If ic is null.

InstanceChecker

public InstanceChecker(Class clazz)
Creates an inclusive InstanceChecker from a single class.
Parameters:
clazz - A class object to use.
Throws:
NullPointerException - If clazz is null.

InstanceChecker

public InstanceChecker(Class clazz,
                       boolean incl)
Creates an InstanceChecker from a single class, also specifying if this InstanceChecker is inclusive or not.
Parameters:
clazz - A class object to use.
incl - true if this object should be inclusive.
Throws:
NullPointerException - If clazz is null.

InstanceChecker

public InstanceChecker(Class[] classes)
Creates an inclusive InstanceChecker from an array of classes.
Parameters:
classes - An array of class objects to use.
incl - true if this object should be inclusive.
Throws:
NullPointerException - If classes or an element in classes is null.

InstanceChecker

public InstanceChecker(Class[] classes,
                       boolean incl)
Creates an InstanceChecker from an array of classes, also specifying if this InstanceChecker is inclusive or not.
Parameters:
classes - An array of class objects to use.
incl - true if this object should be inclusive.
Throws:
NullPointerException - If classes or an element in classes is null.

InstanceChecker

public InstanceChecker(Collection coll)
Creates an inclusive InstanceChecker from a collection of classes.
Parameters:
coll - A collection of class objects to use.
Throws:
NullPointerException - If coll or an element in coll is null.
ClassCastException - If an element in coll is not a Class object.

InstanceChecker

public InstanceChecker(Collection coll,
                       boolean incl)
Creates an InstanceChecker from a collection of classes, also specifying if this InstanceChecker is inclusive or not.
Parameters:
coll - A collection of class objects to use.
incl - true if this object should be inclusive.
Throws:
NullPointerException - If coll or an element in coll is null.
ClassCastException - If an element in coll is not a Class object.
Method Detail

classes

public final Set classes()
Returns an immutable set containing all the classes this InstanceChecker represents.
Returns:
A set containing all the classes this InstanceChecker represents.

isInclusive

public final boolean isInclusive()
Return true if this InstanceChecker is inclusive.
Returns:
true if this InstanceChecker is inclusive.

toString

public String toString()
Creates a String representation of this object.
Overrides:
toString in class Object
Returns:
A string representation of this object.

clone

public Object clone()
Clones this InstanceChecker.
Specified by:
clone in interface CloneableObject
Overrides:
clone in class Object
Returns:
A copy of this object.

hashCode

public int hashCode()
Generates a hash code for this InstanceChecker.
Overrides:
hashCode in class Object
Returns:
A hash code for this InstanceChecker.

matches

public final boolean matches(Object obj)
Checks to see if the supplied object matches the criteria by this InstanceChecker.
Parameters:
obj - The object to compare it against.
Returns:
true if this object matches the criteria set by this InstanceChecker.

matchesIn

public final boolean matchesIn(Object[] array)
Checks to see if the supplied array has an object that matches the criteria set by this InstanceChecker.
Parameters:
array - The supplied array.
Returns:
true if this array contains an object that matches the criteria set by this InstanceChecker.
Throws:
NullPointerException - If array is null.

matchesIn

public final boolean matchesIn(Collection coll)
Checks to see if the supplied collection has an object that matches the criteria set by this InstanceChecker.
Parameters:
coll - The supplied collection.
Returns:
true if this collection contains an object that matches the criteria set by this InstanceChecker.
Throws:
NullPointerException - If coll is null.

equals

public boolean equals(Object obj)
Compares this InstanceChecker against another object.

This object returns true if the object provided is an InstanceChecker, which has the same values as the supplied InstanceChecker.

Overrides:
equals in class Object
Parameters:
obj - The object to test for comparison.
Returns:
true if the object provided matches the criteria of this InstanceChecker.