Class ClassMapping
- java.lang.Object
-
- org.flexdock.util.ClassMapping
-
public class ClassMapping extends java.lang.ObjectThis class manages associations between classes and object instances. It allows for mappings between a class and its subclasses and another associated class, or an associated instance of a class.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, andBaz, whereBarextendsFooandBazextendsBar.Foo.class |-Bar.class |-Baz.classEach of these classes is ultimately a type ofFoo. Some operation is performed on instances ofFooand a set of handler classes are used to handle different types ofFoo. Adding a mapping betweenFoo.classandHandler1.classwill create an association betweenFooand all strict, non-specific subclasses ofFooandHandler1.class.This means that given any instance of
Foo, callinggetClassMapping(Object obj)will returnHandler1.classas the class responsible for handling theFooinstance. This includesBarandBaz. All types ofFoonow have a implicit association withHandler1.classHowever, if this method is subsequently called with arguments of
Baz.classandHandler2.class, then a specific subclass mapping has been introduced forBaz. Associations apply to the given class and non-specific subclasses. Thus, theHandler1.classassociation remains forFooandBar, but no longer forBaz. CallinggetClassMapping(Object obj)with an instance ofBazwill now returnHandler2.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 theHandler1.classmapping forFoo,Bar, and all non-specific subclasses will hold true. However, ifFoohappens to implement the interfaceQwerty, the class mapping relationship will not hold true for all implementations ofQwerty. Only subclasses ofFoo.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
Constructors Constructor Description ClassMapping(java.lang.Class defaultClass, java.lang.Object defaultInstance)Creates a newClassMappinginstance with the specified default values.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddClassMapping(java.lang.Class key, java.lang.Class value)Adds a mapping between the keyClassand the specifiedvalue.voidaddClassMapping(java.lang.Class key, java.lang.Class value, java.lang.Object instance)Adds a mapping between the keyClassand both the specifiedvalueand specified object instance..voidaddClassMapping(java.lang.Object obj, java.lang.Class value)Adds a mapping between theClasstype of the specifiedObjectand the specifiedvalue.java.lang.ObjectgetClassInstance(java.lang.Class key)Returns theObjectinstance associated with the specifiedClass.java.lang.ClassgetClassMapping(java.lang.Class key)Returns theClassassociated with the specifiedClass.java.lang.ClassgetClassMapping(java.lang.Object obj)Returns theClassassociated with theClassof the specifiedObject.java.lang.ObjectgetDefaultInstance()Returns the defaultObjectused for situations in which there is no internal instance mapping.java.lang.ClassgetDefaultMapping()Returns the defaultClassused for situations in which there is no internal class mapping.java.lang.ClassremoveClassMapping(java.lang.Class key)Removes any existing class mappings for theClasstype of the specifiedObject.java.lang.ClassremoveClassMapping(java.lang.Object obj)Removes any existing class mappings for theClasstype of the specifiedObject.
-
-
-
Constructor Detail
-
ClassMapping
public ClassMapping(java.lang.Class defaultClass, java.lang.Object defaultInstance)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 Detail
-
addClassMapping
public void addClassMapping(java.lang.Object obj, java.lang.Class value)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(Object, Class),getClassMapping(Object),getClassMapping(Class),removeClassMapping(Object),removeClassMapping(Class)
-
addClassMapping
public void addClassMapping(java.lang.Class key, java.lang.Class value)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(Class, Class, Object),getClassMapping(Class),removeClassMapping(Class)
-
addClassMapping
public void addClassMapping(java.lang.Class key, java.lang.Class value, java.lang.Object instance)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:
getClassMapping(Class),getClassInstance(Class),removeClassMapping(Class)
-
removeClassMapping
public java.lang.Class removeClassMapping(java.lang.Object obj)
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(Class),addClassMapping(Object, Class),getClassMapping(Object),getClassInstance(Class)
-
removeClassMapping
public java.lang.Class removeClassMapping(java.lang.Class key)
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:
addClassMapping(Class, Class),addClassMapping(Class, Class, Object),getClassMapping(Object),getClassInstance(Class)
-
getClassMapping
public java.lang.Class getClassMapping(java.lang.Object obj)
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:
getDefaultMapping(),addClassMapping(Object, Class),removeClassMapping(Object)
-
getClassMapping
public java.lang.Class getClassMapping(java.lang.Class key)
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:
getDefaultMapping(),addClassMapping(Class, Class),removeClassMapping(Class)
-
getClassInstance
public java.lang.Object getClassInstance(java.lang.Class key)
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:
getDefaultInstance(),addClassMapping(Class, Class, Object),removeClassMapping(Class)
-
getDefaultMapping
public java.lang.Class 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:
ClassMapping(Class, Object)
-
getDefaultInstance
public java.lang.Object 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:
ClassMapping(Class, Object)
-
-