Java tutorial
/* * Helium - a set of useful stuff for java. * * Helium is free software: you can redistribute it and/or modify * it under the terms of the New BSD license as published by * Invenzzia Group. * * Helium is distributed in the hope that it will be useful, * but 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. * * You should have received a copy of the new BSD license * along with Helium. If not, see <http://invenzzia.org/license/new-bsd>. */ package org.invenzzia.helium.domain.relation; import com.google.common.base.Preconditions; import com.google.common.collect.LinkedListMultimap; import com.google.common.collect.Multimap; import java.util.LinkedHashMap; import java.util.Map; import org.invenzzia.helium.domain.MetaRelationship; import org.invenzzia.helium.domain.exception.RelationshipException; /** * Most of the classes in the domain model are connected by some form of * relationship. This class simplifies creating relationships by providing * a unified access interface for them that allows the manipulation and * retrieval of data. It simplifies the design, and reduces the amount * of code and tests that need to be done. * * The relationship object requires two generic arguments: master (the * owner and subject of the relationship) and servant, which is the object. * Each relationship can have many servants but only one master. * * @author Tomasz Jdrzejewski * @param P master object type * @param V servant object type */ public class Relationship<P, V> extends BaseRelationship<P, V> { public static final int DEFAULT_EXPECTED_KEYS = 100; /** * Stores 1:n relationship connections. */ private Multimap<P, V> relationship; public Relationship(MetaRelationship info) { this(info, Relationship.DEFAULT_EXPECTED_KEYS); } public Relationship(MetaRelationship info, int expectedKeys) { super(info); this.relationship = LinkedListMultimap.create(expectedKeys); this.inverseRelationship = new LinkedHashMap<>(expectedKeys); } @Override public void put(P master, V servant) throws RelationshipException { this.filterDuplicates(servant, master.getClass()); this.relationship.put(master, servant); this.putIntoInversedMap(servant, master); } @Override public void update(P newMaster, V servant) throws RelationshipException { this.filterNotUpdateable(); P oldMaster = (P) this.relationshipInfo.getServantParentProperty().getValue(servant); if (null == oldMaster) { this.put(newMaster, servant); } else { this.relationship.remove(oldMaster, servant); this.relationship.put(newMaster, servant); this.putIntoInversedMap(servant, newMaster); } } @Override public void remove(P master, V servant) throws RelationshipException { this.filterNotAMaster(master, servant); if (this.inverseRelationship.remove(servant) != null) { this.relationship.remove(master, servant); this.relationshipInfo.getServantParentProperty().setValue(servant, null); } } @Override public void remove(V servant) { P master = (P) this.relationshipInfo.getServantParentProperty().getValue(servant); if (this.inverseRelationship.remove(servant) != null) { this.relationship.remove(master, servant); this.relationshipInfo.getServantParentProperty().setValue(servant, null); } } @Override public int sizeFor(P master) { return this.relationship.get(master).size(); } }