Public Member Functions | |
boolean | visitClasses (ClassMapVisitor v) |
Traverse through all (Class, Object) mappings. | |
Object | findFirstInstance (final Object key) |
Finds the first object which its key is assignment-compatible (an instance of) with the specified key object. | |
Object | getMappedObject (Class key) |
Finds the object which its key is assignment-compatible (an instance of) with the specified class. | |
Protected Member Functions | |
abstract Map | createMappings () |
Abstract factory method that shall create and initialize the class-object mappings. | |
Map | getMappings () |
Accessor method that returns the mappings. | |
Private Attributes | |
Map | _mappings |
Contains the (Class, Object) mappings. |
switch
construct that compares an integer variable with a set of known integers to execute the appropriate code block based on the integer.switch ... case
construct using a Map
(or hashtable) object, using the integer variable as a key to access the appropriate code block stored as values in the Map
.switch
statement, we often end up with the if .. else
equivalent of a switch
statement:if (object instanceof AnInterface) { something.doSomeThing(object); } else if(object instancof AnotherInterface) { somethingElse.doSomeThing(object); } else if(object instanceof YetAnotherInterface) { anotherThing.doSomeThing(object); } ...
if .. else
statements degrade modularity (and simply not pretty), we need a better solution to handle problems like these.switch
statement can be replaced by indexing a Map
object, we do the approximately analogical thing with a Class as the key. An object is used to "index" a Map
to see which interface is implemented by this object, and obtain the value associated with the interface implemented by the original object.GenericClassMap
, the former long string of if
statements can be shortened into a relatively small code block. This code fragment looks like this:// the following instantiation is only needed to be executed once. GenericClassMap thingMap = new ThingMap();
// do the "indexing" Thing something = (Thing) thingMap.findFirstInstance(object); if (something != null) { something.doSomeThing(object); }
if..else if
constructs, we will save coding efforts when there are more than one block of if ... else if
statements required on the same set of (interface, object) pair.GenericClassMap
and overriding the createMappings()
factory method to establish the required mappings. An example follows:public class ThingMap extends GenericClassMap { protected Map createMappings() { Map mappings = new Hashtable(); mappings.put(AnInterface.class, new SomeThing()); mappings.put(AnotherInterface.class, new SomeThingElse()); mappings.put(YetAnotherInterface.class, new AnotherThing()); return mappings; } }
if .. else
statements can be replaced by a single call to findFirstInstance() which will return the desired object based on an implemented interface.findFirstInstance()
takes linear time proportional to the amount of interfaces registered.
The code saving gained by GenericClassMap
is obtained at a cost of a minor inflexibility: we no longer have control of the ordering of the classes/interfaces being compared. If an object x that implements two interfaces A and B is used to index the class map, there is no guarantee which interface will be selected; the mapped object of any one of those interfaces may be returned by findFirstInstance()
.
Definition at line 181 of file GenericClassMap.java.
|
Abstract factory method that shall create and initialize the class-object mappings.
Referenced by com.arcle.rmt.j2se.util.GenericClassMap.getMappings(). |
|
Finds the first object which its key is assignment-compatible (an instance of) with the specified key object.
Definition at line 208 of file GenericClassMap.java. References com.arcle.rmt.xplat.util.MutableReference.value, and com.arcle.rmt.j2se.util.GenericClassMap.visitClasses(). |
|
Finds the object which its key is assignment-compatible (an instance of) with the specified class.
Definition at line 229 of file GenericClassMap.java. References com.arcle.rmt.j2se.util.GenericClassMap.getMappings(). |
|
Accessor method that returns the mappings.
Definition at line 242 of file GenericClassMap.java. References com.arcle.rmt.j2se.util.GenericClassMap._mappings, and com.arcle.rmt.j2se.util.GenericClassMap.createMappings(). Referenced by com.arcle.rmt.j2se.util.GenericClassMap.getMappedObject(), and com.arcle.rmt.j2se.util.GenericClassMap.visitClasses(). |
|
Traverse through all (Class, Object) mappings.
Definition at line 187 of file GenericClassMap.java. References com.arcle.rmt.j2se.util.GenericClassMap.getMappings(), and com.arcle.rmt.j2se.util.ClassMapVisitor.visitClassMap(). Referenced by com.arcle.rmt.j2se.util.GenericClassMap.findFirstInstance(). |
|
Contains the (Class, Object) mappings.
Definition at line 252 of file GenericClassMap.java. Referenced by com.arcle.rmt.j2se.util.GenericClassMap.getMappings(). |