Class ClassMapping
This class is useful for "handler" type logic in which a handler class must
be mapped to the classes it is designed to handle. Consider the class
hierarchy of Foo, Bar, and Baz, where Bar
extends Foo and Baz extends Bar.
Foo.class
|-Bar.class
|-Baz.class
Each of these classes is ultimately a type of Foo. Some operation is
performed on instances of Foo and a set of handler classes are used
to handle different types of Foo. Adding a mapping between
Foo.class and Handler1.class will create an association
between Foo and all strict, non-specific subclasses of
Foo and Handler1.class.
This means that given any instance of Foo, calling
getClassMapping(Object obj) will return Handler1.class as the
class responsible for handling the Foo instance. This includes
Bar and Baz. All types of Foo now have a implicit
association with Handler1.class
However, if this method is subsequently called with arguments of
Baz.class and Handler2.class, then a specific
subclass mapping has been introduced for Baz. Associations apply to
the given class and non-specific subclasses. Thus, the
Handler1.class association remains for Foo and Bar,
but no longer for Baz. Calling getClassMapping(Object obj)
with an instance of Baz will now return Handler2.class.
Foo.class ---------------> (maps to Handler1.class)
|-Bar.class -----------> (maps to Handler1.class)
|-Baz.class -------> (maps to Handler2.class)
Polymorphic identity within the class association uses strict
subclasses. This means that the Handler1.class mapping for
Foo, Bar, and all non-specific subclasses will hold true.
However, if Foo happens to implement the interface Qwerty,
the class mapping relationship will not hold true for all implementations of
Qwerty. Only subclasses of Foo.
Foo.class (implements Qwerty) ----------------> (maps to Handler1.class)
|-Bar.class (implements Qwerty) ------------> (maps to Handler1.class)
|-Baz.class (implements Qwerty) --------> (maps to Handler2.class)
Asdf.class (implements Qwerty) ---------------> (maps to nothing)
- Author:
- Christopher Butler
-
Constructor Summary
ConstructorsConstructorDescriptionClassMapping(Class defaultClass, Object defaultInstance) Creates a newClassMappinginstance with the specified default values. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddClassMapping(Class key, Class value) Adds a mapping between the keyClassand the specifiedvalue.voidaddClassMapping(Class key, Class value, Object instance) Adds a mapping between the keyClassand both the specifiedvalueand specified object instance..voidaddClassMapping(Object obj, Class value) Adds a mapping between theClasstype of the specifiedObjectand the specifiedvalue.getClassInstance(Class key) Returns theObjectinstance associated with the specifiedClass.getClassMapping(Class key) Returns theClassassociated with the specifiedClass.getClassMapping(Object obj) Returns theClassassociated with theClassof the specifiedObject.Returns the defaultObjectused for situations in which there is no internal instance mapping.Returns the defaultClassused for situations in which there is no internal class mapping.removeClassMapping(Class key) Removes any existing class mappings for theClasstype of the specifiedObject.removeClassMapping(Object obj) Removes any existing class mappings for theClasstype of the specifiedObject.
-
Constructor Details
-
ClassMapping
Creates a newClassMappinginstance with the specified default values. All calls togetClassMapping(Class key)for thisClassMappingin which a specific mapping cannot be found will return the specifieddefaultClass. All calls togetClassInstance(Class key)in which a specific mapping cannot be found will return the specifieddefaultInstance.- Parameters:
defaultClass- the default class used by thisClassMappingdefaultInstance- the default object instance used by thisClassMapping
-
-
Method Details
-
addClassMapping
Adds a mapping between theClasstype of the specifiedObjectand the specifiedvalue. This method callsgetClass()on the specifiedObjectand dispatches toaddClassMapping(Class key, Class value). If eitherobjorvaluearenull, then this method returns with no action taken. Thevalueclass may later be retrieved by callinggetClassMapping(Class key)using the specifiedkeyclass (obj.getClass()) or any subclass thereof for which a specific class mapping does not already exist.- Parameters:
obj- theObjectwhoseClasswill be mapped to the specifiedvalue.value- theClassto be associated with the specified key- See Also:
-
addClassMapping
Adds a mapping between the keyClassand the specifiedvalue. If eitherkeyorvaluearenull, then this method returns with no action taken. This method creates an association between the specifiedkeyClassand all strict, non-specific subclasses and the specifiedvalueClass. Thevalueclass may later be retrieved by calling getClassMapping(Class key) using the specifiedkeyclass or any subclass thereof for which a specific class mapping does not already exist.- Parameters:
key- theClassto be mapped to the specifiedvalue.value- theClassto be associated with the specified key- See Also:
-
addClassMapping
Adds a mapping between the keyClassand both the specifiedvalueand specified object instance.. If eitherkeyorvaluearenull, then this method returns with no action taken. This method creates an association between the specifiedkeyClassand all strict, non-specific subclasses and the specifiedvalueClass. Thevalueclass may later be retrieved by callinggetClassMapping(Class key)using the specifiedkeyclass or any subclass thereof for which a specific class mapping does not already exist.This method also creates an optional mapping between the
keyand a particular object instance, defined by theinstanceparameter. Ifinstanceis non-null, then a mapping is defined betweenkeyand all strict, non-specific subclasses and the object instance itself. Theinstanceobject may later be retrieved by callinggetClassInstance(Class key)using the specifiedkeyclass or any subclass thereof for which a specific instance mapping does not already exist. Ifinstanceisnull, then no instance mapping is created.- Parameters:
key- theClassto be mapped to the specifiedvalue.value- theClassto be associated with the specified keyinstance- the object instance to be associated with the specified key- See Also:
-
removeClassMapping
Removes any existing class mappings for theClasstype of the specifiedObject. This method callsgetClass()on the specifiedObjectand dispatches toremoveClassMapping(Class key). Ifobjisnull, then this method returnsnull.Removing the mapping for the specified
Classwill also remove it for all non-specific subclasses. This means that subclasses of the specifiedClasswill require specific mappings if the it is desired for the existing mapping behavior for these classes to remain the same.If any instance mappings exist for the specified
Class, they are also removed. This means non-specific subclass instance mappings will also be removed.- Parameters:
obj- theObjectwhoseClasswill be removed from the internal mapping- Returns:
- the
Classwhose mapping has been removed - See Also:
-
removeClassMapping
Removes any existing class mappings for theClasstype of the specifiedObject. This method callsgetClass()on the specifiedObjectand dispatches toremoveClassMapping(Class key). Ifobjisnull, then this method returnsnull.Removing the mapping for the specified
Classwill also remove it for all non-specific subclasses. This means that subclasses of the specifiedClasswill require specific mappings if the it is desired for the existing mapping behavior for these classes to remain the same.If any instance mappings exist for the specified
Class, they are also removed. This means non-specific subclass instance mappings will also be removed.- Parameters:
key- theClasswhose internal mapping will be removed- Returns:
- the
Classwhose mapping has been removed - See Also:
-
getClassMapping
Returns theClassassociated with theClassof the specifiedObject. Ifobjisnull, this method will return the value retrieved fromgetDefaultMapping(). Otherwise, this method callsobj.getClass()and dispatches togetClassMapping(Class key).If no mapping has been defined for the specified
Class, then it's superclass is checked, and then that classes' superclass, and so on untiljava.lang.Objectis reached. If a mapping is found anywhere within the superclass hierarchy, then the mappedClassis returned. Otherwise, the value returned bygetDefaultMapping()is returned.- Parameters:
obj- theObjectwhoseClass'sinternal mapping will be returned- Returns:
- the
Classthat is mapped internally to the specified keyClass - See Also:
-
getClassMapping
Returns theClassassociated with the specifiedClass. Ifkeyisnull, this method will return the value retrieved fromgetDefaultMapping(). If no mapping has been defined for the specifiedClass, then it's superclass is checked, and then that classes' superclass, and so on untiljava.lang.Objectis reached. If a mapping is found anywhere within the superclass hierarchy, then the mappedClassis returned. Otherwise, the value returned bygetDefaultMapping()is returned.- Parameters:
key- theClasswhose internal mapping will be returned- Returns:
- the
Classthat is mapped internally to the specifiedkey - See Also:
-
getClassInstance
Returns theObjectinstance associated with the specifiedClass. Ifkeyisnull, this method will return the value retrieved fromgetDefaultInstance(). If no mapping has been defined for the specifiedClass, then it's superclass is checked, and then that classes' superclass, and so on untiljava.lang.Objectis reached. If an instance mapping is found anywhere within the superclass hierarchy, then the mappedObjectis returned. Otherwise, the value returned bygetDefaultInstance()is returned.- Parameters:
key- theClasswhose internal mapping will be returned- Returns:
- the
Objectinstance that is mapped internally to the specifiedkey - See Also:
-
getDefaultMapping
Returns the defaultClassused for situations in which there is no internal class mapping. This property is read-only and is initialized within theClassMappingconstructor.- Returns:
- the default
Classused for situations in which there is no internal class mapping. - See Also:
-
getDefaultInstance
Returns the defaultObjectused for situations in which there is no internal instance mapping. This property is read-only and is initialized within theClassMappingconstructor.- Returns:
- the default
Objectused for situations in which there is no internal instance mapping. - See Also:
-