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.classEach 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 newClassMapping
instance with the specified default values. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addClassMapping
(Class key, Class value) Adds a mapping between the keyClass
and the specifiedvalue
.void
addClassMapping
(Class key, Class value, Object instance) Adds a mapping between the keyClass
and both the specifiedvalue
and specified object instance..void
addClassMapping
(Object obj, Class value) Adds a mapping between theClass
type of the specifiedObject
and the specifiedvalue
.getClassInstance
(Class key) Returns theObject
instance associated with the specifiedClass
.getClassMapping
(Class key) Returns theClass
associated with the specifiedClass
.getClassMapping
(Object obj) Returns theClass
associated with theClass
of the specifiedObject
.Returns the defaultObject
used for situations in which there is no internal instance mapping.Returns the defaultClass
used for situations in which there is no internal class mapping.removeClassMapping
(Class key) Removes any existing class mappings for theClass
type of the specifiedObject
.removeClassMapping
(Object obj) Removes any existing class mappings for theClass
type of the specifiedObject
.
-
Constructor Details
-
ClassMapping
Creates a newClassMapping
instance with the specified default values. All calls togetClassMapping(Class key)
for thisClassMapping
in 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 thisClassMapping
defaultInstance
- the default object instance used by thisClassMapping
-
-
Method Details
-
addClassMapping
Adds a mapping between theClass
type of the specifiedObject
and the specifiedvalue
. This method callsgetClass()
on the specifiedObject
and dispatches toaddClassMapping(Class key, Class value)
. If eitherobj
orvalue
arenull
, then this method returns with no action taken. Thevalue
class may later be retrieved by callinggetClassMapping(Class key)
using the specifiedkey
class (obj.getClass()
) or any subclass thereof for which a specific class mapping does not already exist.- Parameters:
obj
- theObject
whoseClass
will be mapped to the specifiedvalue
.value
- theClass
to be associated with the specified key- See Also:
-
addClassMapping
Adds a mapping between the keyClass
and the specifiedvalue
. If eitherkey
orvalue
arenull
, then this method returns with no action taken. This method creates an association between the specifiedkey
Class
and all strict, non-specific subclasses and the specifiedvalue
Class
. Thevalue
class may later be retrieved by calling getClassMapping(Class key) using the specifiedkey
class or any subclass thereof for which a specific class mapping does not already exist.- Parameters:
key
- theClass
to be mapped to the specifiedvalue
.value
- theClass
to be associated with the specified key- See Also:
-
addClassMapping
Adds a mapping between the keyClass
and both the specifiedvalue
and specified object instance.. If eitherkey
orvalue
arenull
, then this method returns with no action taken. This method creates an association between the specifiedkey
Class
and all strict, non-specific subclasses and the specifiedvalue
Class
. Thevalue
class may later be retrieved by callinggetClassMapping(Class key)
using the specifiedkey
class or any subclass thereof for which a specific class mapping does not already exist.This method also creates an optional mapping between the
key
and a particular object instance, defined by theinstance
parameter. Ifinstance
is non-null
, then a mapping is defined betweenkey
and all strict, non-specific subclasses and the object instance itself. Theinstance
object may later be retrieved by callinggetClassInstance(Class key)
using the specifiedkey
class or any subclass thereof for which a specific instance mapping does not already exist. Ifinstance
isnull
, then no instance mapping is created.- Parameters:
key
- theClass
to be mapped to the specifiedvalue
.value
- theClass
to 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 theClass
type of the specifiedObject
. This method callsgetClass()
on the specifiedObject
and dispatches toremoveClassMapping(Class key)
. Ifobj
isnull
, then this method returnsnull
.Removing the mapping for the specified
Class
will also remove it for all non-specific subclasses. This means that subclasses of the specifiedClass
will 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
- theObject
whoseClass
will be removed from the internal mapping- Returns:
- the
Class
whose mapping has been removed - See Also:
-
removeClassMapping
Removes any existing class mappings for theClass
type of the specifiedObject
. This method callsgetClass()
on the specifiedObject
and dispatches toremoveClassMapping(Class key)
. Ifobj
isnull
, then this method returnsnull
.Removing the mapping for the specified
Class
will also remove it for all non-specific subclasses. This means that subclasses of the specifiedClass
will 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
- theClass
whose internal mapping will be removed- Returns:
- the
Class
whose mapping has been removed - See Also:
-
getClassMapping
Returns theClass
associated with theClass
of the specifiedObject
. Ifobj
isnull
, 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.Object
is reached. If a mapping is found anywhere within the superclass hierarchy, then the mappedClass
is returned. Otherwise, the value returned bygetDefaultMapping()
is returned.- Parameters:
obj
- theObject
whoseClass's
internal mapping will be returned- Returns:
- the
Class
that is mapped internally to the specified keyClass
- See Also:
-
getClassMapping
Returns theClass
associated with the specifiedClass
. Ifkey
isnull
, 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.Object
is reached. If a mapping is found anywhere within the superclass hierarchy, then the mappedClass
is returned. Otherwise, the value returned bygetDefaultMapping()
is returned.- Parameters:
key
- theClass
whose internal mapping will be returned- Returns:
- the
Class
that is mapped internally to the specifiedkey
- See Also:
-
getClassInstance
Returns theObject
instance associated with the specifiedClass
. Ifkey
isnull
, 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.Object
is reached. If an instance mapping is found anywhere within the superclass hierarchy, then the mappedObject
is returned. Otherwise, the value returned bygetDefaultInstance()
is returned.- Parameters:
key
- theClass
whose internal mapping will be returned- Returns:
- the
Object
instance that is mapped internally to the specifiedkey
- See Also:
-
getDefaultMapping
Returns the defaultClass
used for situations in which there is no internal class mapping. This property is read-only and is initialized within theClassMapping
constructor.- Returns:
- the default
Class
used for situations in which there is no internal class mapping. - See Also:
-
getDefaultInstance
Returns the defaultObject
used for situations in which there is no internal instance mapping. This property is read-only and is initialized within theClassMapping
constructor.- Returns:
- the default
Object
used for situations in which there is no internal instance mapping. - See Also:
-