org.malai.mapping
Class MappingRegistry

java.lang.Object
  extended by org.malai.mapping.MappingRegistry
All Implemented Interfaces:
IMappingRegistry

public final class MappingRegistry
extends java.lang.Object
implements IMappingRegistry

This registry allows to map source objects to target objects using mappings. When a source object is modified, its corresponding mappings are executed to update the corresponding target objects.

This file is part of Malai.
Copyright (c) 2009-2012 Arnaud BLOUIN

Malai is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
Malai is distributed without any warranty; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

05/15/2010

Since:
0.2
Version:
0.2
Author:
Arnaud BLOUIN

Field Summary
private  java.util.Map<java.lang.Object,java.util.List<IMapping>> invertedMultiMappings
           
private  java.util.Map<java.lang.Object,IMapping> invertedUniqueMappings
           
private  java.util.Map<java.lang.Object,java.util.List<IMapping>> multiMappings
          Contains objects and their mappings.
static IMappingRegistry REGISTRY
          The singleton of the class.
private  java.util.Map<java.lang.Object,IMapping> uniqueMappings
          Contains objects and their unique mapping.
 
Constructor Summary
private MappingRegistry()
          Creates a mapping registry.
 
Method Summary
 void addMapping(IMapping mapping)
          Adds a mapping to the registry.
private static void addMappings(IMapping mapping, java.lang.Object object, java.util.Map<java.lang.Object,IMapping> uniqueMap, java.util.Map<java.lang.Object,java.util.List<IMapping>> multiMap)
          Is used by method addMapping(mapping).
private static java.util.List<IMapping> getMapping(java.lang.Object object, java.util.Map<java.lang.Object,IMapping> uniqueMap, java.util.Map<java.lang.Object,java.util.List<IMapping>> multiMap)
           
 java.util.List<IMapping> getMappingFromSource(java.lang.Object source)
           
 java.util.List<IMapping> getMappingFromTarget(java.lang.Object target)
           
<T> T
getSourceFromTarget(java.lang.Object target, java.lang.Class<T> sourceType)
           
<T> T
getSrcOrTarFromTarOrSrc(boolean fromSource, java.lang.Object object, java.lang.Class<T> type)
          Allows to get the source or the target object of a mapping using the target or the source object.
<T> T
getTargetFromSource(java.lang.Object source, java.lang.Class<T> targetType)
           
 void initMappings()
          At start, mappings must be executed one time to initialise the mapping.
 void onListCleaned(java.lang.Object list)
          Executes the mapping when all the elements of the source were removed.
<T> void
onObjectAdded(IActiveList<T> list, T object, int index)
          Executes mappings related to the given list for the addition of an object.
 void onObjectModified(java.lang.Object object)
          Executes mappings related to the given list for the modification of an object.
<T> void
onObjectMoved(IActiveList<T> list, T object, int srcIndex, int targetIndex)
          Executes mappings related to the given list for the move of an object.
 void onObjectRemoved(IActiveList<?> list, java.lang.Object object, int index)
          Executes mappings related to the given list for the deletion of an object.
<T> void
onObjectReplaced(IUnary<T> object, T replacedObject)
          Executes the mapping when an object has been replaced by an other one.
private static void printMappings(java.lang.StringBuilder buf, java.util.Map<java.lang.Object,IMapping> uMappings, java.util.Map<java.lang.Object,java.util.List<IMapping>> mMappings)
          This method is used by the toString function to print the register.
protected  void removeMappings(java.lang.Object object, java.lang.Class<? extends IMapping> clazz, java.util.Map<java.lang.Object,IMapping> uniqueMaps, java.util.Map<java.lang.Object,java.util.List<IMapping>> multiMaps, boolean removeUsingTarget, boolean removeOppositeMappings)
          Remove the mappings of the given type and using the given object from the hash maps.
 void removeMappingsUsingSource(java.lang.Object source)
          Removes the mappings that use the given object as source.
 void removeMappingsUsingSource(java.lang.Object source, java.lang.Class<? extends IMapping> clazz)
          Removes the mappings of the given type that use the given object as source.
protected  void removeMappingsUsingSource(java.lang.Object source, java.lang.Class<? extends IMapping> clazz, boolean removeTargetMappings)
          Idem than removeMappingsUsingSource.
 void removeMappingsUsingTarget(java.lang.Object target)
          Removes the mappings that use the given object as target.
 void removeMappingsUsingTarget(java.lang.Object target, java.lang.Class<? extends IMapping> clazz)
          Removes the mappings that use the given object as target.
protected  void removeMappingsUsingTarget(java.lang.Object target, java.lang.Class<? extends IMapping> clazz, boolean removeSourceMappings)
          Idem than removeMappingsUsingTarget.
private  void removeOppositeMapping(IMapping mapping, boolean removeUsingTarget)
           
 java.lang.String toString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

REGISTRY

public static final IMappingRegistry REGISTRY
The singleton of the class.


uniqueMappings

private java.util.Map<java.lang.Object,IMapping> uniqueMappings
Contains objects and their unique mapping.


multiMappings

private java.util.Map<java.lang.Object,java.util.List<IMapping>> multiMappings
Contains objects and their mappings. The difference with uniqueMappings is that multiMappings is not used by default (i.e. when only one mapping is attributed to an object) to save memory.


invertedUniqueMappings

private java.util.Map<java.lang.Object,IMapping> invertedUniqueMappings

invertedMultiMappings

private java.util.Map<java.lang.Object,java.util.List<IMapping>> invertedMultiMappings
Constructor Detail

MappingRegistry

private MappingRegistry()
Creates a mapping registry.

Since:
0.2
Method Detail

initMappings

public void initMappings()
Description copied from interface: IMappingRegistry
At start, mappings must be executed one time to initialise the mapping.

Specified by:
initMappings in interface IMappingRegistry

getTargetFromSource

public <T> T getTargetFromSource(java.lang.Object source,
                                 java.lang.Class<T> targetType)
Specified by:
getTargetFromSource in interface IMappingRegistry
Parameters:
source - The source object used to find the target object matching the given class.
targetType - The class of the target object to find.
Returns:
The target object found using the given source object, or null.

getSourceFromTarget

public <T> T getSourceFromTarget(java.lang.Object target,
                                 java.lang.Class<T> sourceType)
Specified by:
getSourceFromTarget in interface IMappingRegistry
Parameters:
target - The target object used to find the source object matching the given class.
sourceType - The class of the source object to find.
Returns:
The source object found using the given target object, or null.

getSrcOrTarFromTarOrSrc

public <T> T getSrcOrTarFromTarOrSrc(boolean fromSource,
                                     java.lang.Object object,
                                     java.lang.Class<T> type)
Allows to get the source or the target object of a mapping using the target or the source object.

Parameters:
fromSource - True: the target object while be searched. Otherwise, the source object.
object - The source or target object of the mapping used to get the target or source object.
type - The class of the object to return.
Returns:
The found object or null.
Since:
0.2

getMappingFromSource

public java.util.List<IMapping> getMappingFromSource(java.lang.Object source)
Parameters:
source - This object is used to find the mapping that uses is as a source object.
Returns:
The found mapping or null.
Since:
0.2

getMappingFromTarget

public java.util.List<IMapping> getMappingFromTarget(java.lang.Object target)
Parameters:
target - This object is used to find the mapping that uses is as a target object.
Returns:
The found mapping or null.
Since:
0.2

getMapping

private static java.util.List<IMapping> getMapping(java.lang.Object object,
                                                   java.util.Map<java.lang.Object,IMapping> uniqueMap,
                                                   java.util.Map<java.lang.Object,java.util.List<IMapping>> multiMap)

addMapping

public void addMapping(IMapping mapping)
Description copied from interface: IMappingRegistry
Adds a mapping to the registry.

Specified by:
addMapping in interface IMappingRegistry
Parameters:
mapping - The mapping to add.

addMappings

private static void addMappings(IMapping mapping,
                                java.lang.Object object,
                                java.util.Map<java.lang.Object,IMapping> uniqueMap,
                                java.util.Map<java.lang.Object,java.util.List<IMapping>> multiMap)
Is used by method addMapping(mapping).

Parameters:
mapping - The mapping to add.
object - The object that will be used as key in the map.
uniqueMap - The map that contains unique mappings.
multiMap - The map that contains multiple mappings.
Since:
0.2

onObjectAdded

public <T> void onObjectAdded(IActiveList<T> list,
                              T object,
                              int index)
Description copied from interface: IMappingRegistry
Executes mappings related to the given list for the addition of an object.

Specified by:
onObjectAdded in interface IMappingRegistry
Parameters:
list - The modified source list.
object - The added object.
index - The position where the object has been added.

onObjectRemoved

public void onObjectRemoved(IActiveList<?> list,
                            java.lang.Object object,
                            int index)
Description copied from interface: IMappingRegistry
Executes mappings related to the given list for the deletion of an object.

Specified by:
onObjectRemoved in interface IMappingRegistry
Parameters:
list - The modified source list.
object - The deleted object.
index - The position where the object has been deleted.

onListCleaned

public void onListCleaned(java.lang.Object list)
Description copied from interface: IMappingRegistry
Executes the mapping when all the elements of the source were removed.

Specified by:
onListCleaned in interface IMappingRegistry
Parameters:
list - The list before the cleaning, i.e. it still contains all the elements in order to know which elements have been removed.

onObjectMoved

public <T> void onObjectMoved(IActiveList<T> list,
                              T object,
                              int srcIndex,
                              int targetIndex)
Description copied from interface: IMappingRegistry
Executes mappings related to the given list for the move of an object.

Specified by:
onObjectMoved in interface IMappingRegistry
Parameters:
list - The modified source list.
object - The move object.
srcIndex - The source position of the object.
targetIndex - The target position of the object.

onObjectReplaced

public <T> void onObjectReplaced(IUnary<T> object,
                                 T replacedObject)
Description copied from interface: IMappingRegistry
Executes the mapping when an object has been replaced by an other one.

Specified by:
onObjectReplaced in interface IMappingRegistry
Parameters:
object - The singleton that contains the new object.
replacedObject - The old object contained in the singleton that has been replaced.

onObjectModified

public void onObjectModified(java.lang.Object object)
Description copied from interface: IMappingRegistry
Executes mappings related to the given list for the modification of an object.

Specified by:
onObjectModified in interface IMappingRegistry
Parameters:
object - The modified object.

removeMappingsUsingSource

public void removeMappingsUsingSource(java.lang.Object source,
                                      java.lang.Class<? extends IMapping> clazz)
Description copied from interface: IMappingRegistry
Removes the mappings of the given type that use the given object as source.

Specified by:
removeMappingsUsingSource in interface IMappingRegistry
Parameters:
source - A source object of the mappings to remove.
clazz - The type of the mappings to remove.

removeMappingsUsingSource

protected void removeMappingsUsingSource(java.lang.Object source,
                                         java.lang.Class<? extends IMapping> clazz,
                                         boolean removeTargetMappings)
Idem than removeMappingsUsingSource.

Parameters:
removeTargetMappings - True: the inverted hash maps will be cleaned too.
Since:
0.2

removeMappingsUsingTarget

public void removeMappingsUsingTarget(java.lang.Object target,
                                      java.lang.Class<? extends IMapping> clazz)
Description copied from interface: IMappingRegistry
Removes the mappings that use the given object as target.

Specified by:
removeMappingsUsingTarget in interface IMappingRegistry
Parameters:
target - A target object of the mappings to remove.
clazz - The type of the mappings to remove.

removeMappingsUsingTarget

protected void removeMappingsUsingTarget(java.lang.Object target,
                                         java.lang.Class<? extends IMapping> clazz,
                                         boolean removeSourceMappings)
Idem than removeMappingsUsingTarget.

Parameters:
removeSourceMappings - True: the standard hash maps will be cleaned too.
Since:
0.2

removeMappings

protected void removeMappings(java.lang.Object object,
                              java.lang.Class<? extends IMapping> clazz,
                              java.util.Map<java.lang.Object,IMapping> uniqueMaps,
                              java.util.Map<java.lang.Object,java.util.List<IMapping>> multiMaps,
                              boolean removeUsingTarget,
                              boolean removeOppositeMappings)
Remove the mappings of the given type and using the given object from the hash maps.

Parameters:
object - The source or target object of the mappings to remove.
clazz - The type of the mappings to remove.
uniqueMaps - The hash map that contains the unique mappings.
multiMaps - The hash map that contains the mappings contained into a list.
removeUsingTarget - True: will remove the mappings by considering the given object as a target object of the mappings.
removeOppositeMappings - True: mappings contains is the opposite hash maps will be removed too.
Since:
0.2

toString

public java.lang.String toString()
Overrides:
toString in class java.lang.Object

printMappings

private static void printMappings(java.lang.StringBuilder buf,
                                  java.util.Map<java.lang.Object,IMapping> uMappings,
                                  java.util.Map<java.lang.Object,java.util.List<IMapping>> mMappings)
This method is used by the toString function to print the register.

Since:
0.2

removeOppositeMapping

private void removeOppositeMapping(IMapping mapping,
                                   boolean removeUsingTarget)

removeMappingsUsingSource

public void removeMappingsUsingSource(java.lang.Object source)
Description copied from interface: IMappingRegistry
Removes the mappings that use the given object as source.

Specified by:
removeMappingsUsingSource in interface IMappingRegistry
Parameters:
source - A source object of the mappings to remove.

removeMappingsUsingTarget

public void removeMappingsUsingTarget(java.lang.Object target)
Description copied from interface: IMappingRegistry
Removes the mappings that use the given object as target.

Specified by:
removeMappingsUsingTarget in interface IMappingRegistry
Parameters:
target - A target object of the mappings to remove.