I have two classes, ClassA
and ClassB
, as well as a \"many to many\" AssociationClass
. I want a structure that holds the associations
Thanks for your suggestions.
I finally reinvented the wheel ... I have written a generic class for holding associations. I use two maps of maps, synchronized.
The associations holder provides the following methods
void setAssociation(LeftClass left, RightClass right, AssociationClass assoc);
AssociationClass getAssociation(LeftClass left, RightClass right);
Map getAssocationsLeft(LeftClass left);
Map getAssocationsRight(RightClass right);
void removeAssociation(LeftClass left, RightClass right);
Here is the code:
import java.util.HashMap;
/** This class holds many to many associations between two classes. */
public class AssociationHolder {
// -------------------------------------------------------
// Attributes
// -------------------------------------------------------
private HashMap> associationsLeft =
new HashMap>();
private HashMap> associationsRight =
new HashMap>();
// -------------------------------------------------------
// Methods
// -------------------------------------------------------
/**
* Set an association between two instance.
* Any prior association is overwritten.
*/
public void setAssociation(LeftClass left, RightClass right, AssociationClass association) {
// Get the map for the left
HashMap leftMap = this.associationsLeft.get(left);
// No association defined yet for this left key ? => Create new map
if (leftMap == null) {
leftMap = new HashMap();
this.associationsLeft.put(left, leftMap);
}
// Get the map for the right
HashMap rightMap = this.associationsRight.get(right);
// No association defined yet for this right key ? => Create new map
if (rightMap == null) {
rightMap = new HashMap();
this.associationsRight.put(right, rightMap);
}
// Set the assoication on both maps
leftMap.put(right, association);
rightMap.put(left, association);
}
/** @return null if no association found. */
public AssociationClass getAssociation(LeftClass left, RightClass right) {
// Use left maps (could have used the right one as well)
HashMap leftMap = this.associationsLeft.get(left);
if (leftMap == null) return null;
return leftMap.get(right);
}
/** Get all associations defined for a given Left instance. */
public HashMap getAssociationsLeft(LeftClass left) {
HashMap leftMap = this.associationsLeft.get(left);
// No map defined ? return empty one instead of null
if (leftMap == null) {
return new HashMap();
} else {
return leftMap;
}
}
/** Get all associations defined for a given Right instance. */
public HashMap getAssociationsRight(RightClass right) {
HashMap rightMap = this.associationsRight.get(right);
// No map defined ? return empty one instead of null
if (rightMap == null) {
return new HashMap();
} else {
return rightMap;
}
}
/**
* Remove an association between two instances.
*/
public void removeAssociation(LeftClass left, RightClass right) {
HashMap leftMap = this.getAssociationsLeft(left);
HashMap rightMap = this.getAssociationsRight(right);
leftMap.remove(right);
rightMap.remove(left);
}
}
I hope this can help someone in the future.