001/*
002 *                    BioJava development code
003 * This code may be freely distributed and modified under the
004 * terms of the GNU Lesser General Public Licence.  This should
005 * be distributed with the code.  If you do not have a copy,
006 * see:
007 *
008 *      http://www.gnu.org/copyleft/lesser.html
009 *
010 * Copyright for this code is held jointly by the individual
011 * authors.  These should be listed in @author doc comments.
012 *
013 * For more information on the BioJava project and its aims,
014 * or to join the biojava-l mailing list, visit the home page
015 * at:
016 *
017 *      http://www.biojava.org/
018 *
019 */
020
021package org.biojava.bio.program.homologene;
022
023import java.util.HashMap;
024import java.util.HashSet;
025import java.util.Map;
026import java.util.Set;
027
028import org.biojava.utils.ChangeEvent;
029import org.biojava.utils.ChangeSupport;
030import org.biojava.utils.ChangeVetoException;
031
032public class SimpleOrthologueSet extends AbstractOrthologueSet
033{
034
035    public class Iterator implements OrthologueSet.Iterator
036    {
037        private java.util.Iterator orthoIterator;
038
039        private Iterator(java.util.Iterator orthoIterator)
040        {
041            this.orthoIterator = orthoIterator;
042        }
043
044        public boolean hasNext()
045        {
046            return orthoIterator.hasNext();
047        }
048
049        public Orthologue nextOrthologue()
050        {
051            return (Orthologue) orthoIterator.next();
052        }
053
054    }
055
056    // every Orthologue is stored in a Set
057    private Set orthologueSet = new HashSet();
058    private Map orthologueByHomologeneID = new HashMap();
059
060    {
061        generateChangeSupport();
062    }
063
064    public void addOrthologue(Orthologue ortho)
065        throws ChangeVetoException
066    {
067        if (!hasListeners()) {
068            orthologueSet.add(ortho);
069        }
070        else {
071            // get the change support
072            ChangeSupport cs = getChangeSupport(OrthologueSet.MODIFY);
073
074            synchronized(cs) {
075                ChangeEvent ce = new ChangeEvent(this, OrthologueSet.MODIFY);
076                cs.firePreChangeEvent(ce);
077                orthologueSet.add(ortho);
078                cs.firePostChangeEvent(ce);
079            }
080        }
081    }
082
083    public void removeOrthologue(Orthologue ortho)
084        throws ChangeVetoException
085    {
086        if (!hasListeners()) {
087            orthologueSet.remove(ortho);
088        }
089        else {
090            // get the change support
091            ChangeSupport cs = getChangeSupport(OrthologueSet.MODIFY);
092
093            synchronized(cs) {
094                ChangeEvent ce = new ChangeEvent(this, OrthologueSet.MODIFY);
095                cs.firePreChangeEvent(ce);
096                orthologueSet.remove(ortho);
097                cs.firePostChangeEvent(ce);
098            }
099        }
100    }
101
102    public Orthologue getOrthologue(String homologeneID)
103    {
104        return (Orthologue) orthologueByHomologeneID.get(homologeneID);
105    }
106
107    public OrthologueSet.Iterator iterator()
108    {
109        return new Iterator(orthologueSet.iterator());
110    }
111}
112