001/*
002 *                    BioJava development code
003 *
004 * This code may be freely distributed and modified under the
005 * terms of the GNU Lesser General Public Licence.  This should
006 * be distributed with the code.  If you do not have a copy,
007 * see:
008 *
009 *      http://www.gnu.org/copyleft/lesser.html
010 *
011 * Copyright for this code is held jointly by the individual
012 * authors.  These should be listed in @author doc comments.
013 *
014 * For more information on the BioJava project and its aims,
015 * or to join the biojava-l mailing list, visit the home page
016 * at:
017 *
018 *      http://www.biojava.org/
019 *
020 */
021
022/*
023 *    SimpleSymbolPropertyTableDB.java
024 */
025package org.biojava.bio.proteomics.aaindex;
026
027import java.util.Hashtable;
028import java.util.Iterator;
029import java.util.Map;
030import java.util.NoSuchElementException;
031import java.util.Set;
032
033import org.biojava.bio.BioException;
034import org.biojava.bio.seq.db.IllegalIDException;
035import org.biojava.bio.symbol.SymbolPropertyTable;
036
037/**
038 * A simple implementation of a symbol property table database.
039 * @author <a href="mailto:Martin.Szugat@GMX.net">Martin Szugat</a>
040 * @version $Revision$
041 */
042public class SimpleSymbolPropertyTableDB implements SymbolPropertyTableDB {
043    
044    /* PRIVATE CLASSES */
045    
046    /**
047     * Iterator over symbol property tables.
048     * @author <a href="mailto:Martin.Szugat@GMX.net">Martin Szugat</a>
049     * @version $Revision$
050     */
051    private static final class SimpleSymbolPropertyTableIterator implements
052            SymbolPropertyTableIterator {
053        
054        /* PRIVATE FIELDS */
055
056        /**
057         * The internal iterator.
058         */
059        private Iterator iterator = null;
060        
061        /* PUBLIC CONSTRUCTORS */
062        
063        /**
064         * Initializes the iterator.
065         * @param iterator the internal iterator
066         * @throws NullPointerException if <code>iterator</code> is
067         * <code>null</code>.
068         */
069        public SimpleSymbolPropertyTableIterator(
070                Iterator iterator) 
071        throws NullPointerException {
072            super();
073            if (iterator == null) {
074                throw new NullPointerException("iterator is null.");
075            }
076            this.iterator = iterator;
077        }
078        
079        /* INTERFACE SymbolPropertyTableIterator */
080
081        /**
082         * {@inheritDoc}
083         */
084        public boolean hasNext() {
085            return iterator.hasNext();
086        }
087
088        /**
089         * {@inheritDoc}
090         */
091        public SymbolPropertyTable nextTable() throws NoSuchElementException, 
092        BioException {
093            return (SymbolPropertyTable) iterator.next();
094        }
095
096    }
097
098    /* PRIVATE FIELDS */
099    
100    /**
101     * Internal map of symbol property tables. 
102     */
103    private Map map = null;
104    
105    /* PUBLIC CONSTRUCTORS */
106
107    /**
108     * Initializes the database. 
109     */
110    public SimpleSymbolPropertyTableDB() {
111        super();
112        map = new Hashtable();
113    }
114    
115    /**
116     * Initializes the database by copying all symbol property tables from 
117     * a given iterator into the database.
118     * @param tableIterator an iterator over symbol property tables.
119     * @throws BioException if the symbol property tables could not be
120     * iterated.
121     */
122    public SimpleSymbolPropertyTableDB(
123            SymbolPropertyTableIterator tableIterator) 
124    throws BioException {
125        this();
126        if (tableIterator == null) {
127            throw new NullPointerException("tableIterator is null.");
128        }
129        while (tableIterator.hasNext()) {
130            addTable(tableIterator.nextTable());
131        }
132    }
133    
134    /* PUBLIC METHODS */
135    
136    /**
137     * Adds a symbol property table to the database. Overrides an existing 
138     * table entry with the same name.
139     * @param table the symbol property table to add.
140     * @throws NullPointerException if <code>table</code> is <code>null</code>.
141     */
142    public void addTable( SymbolPropertyTable table) 
143    throws NullPointerException {
144        if (table == null) {
145            throw new NullPointerException("table is null.");
146        }
147        map.put(table.getName(), table);
148    }
149    
150    /* INTERFACE SymbolPropertyTableDB */
151
152    /**
153     * {@inheritDoc}
154     */
155    public SymbolPropertyTableIterator tableIterator() {
156        return new SimpleSymbolPropertyTableIterator(map.values().iterator());
157    }
158
159    /**
160     * {@inheritDoc}
161     */
162    public int numTables() {
163        return map.size();
164    }
165
166    /**
167     * {@inheritDoc}
168     */
169    public SymbolPropertyTable table(String name) throws 
170    IllegalIDException, NullPointerException {
171        if (name == null) {
172            throw new NullPointerException("name is null.");
173        }
174        if (!map.containsKey(name)) {
175            throw new IllegalIDException("No table found with name "
176                    + name + ".");
177        }
178        return (SymbolPropertyTable) map.get(name);
179    }
180
181    /**
182     * {@inheritDoc}
183     */
184    public Set names() {
185        return map.keySet();
186    }
187}