001    /**
002     * Copyright (c) 2010, SIB. All rights reserved.
003     * 
004     * SIB (Swiss Institute of Bioinformatics) - http://www.isb-sib.ch Host -
005     * https://sourceforge.net/projects/javaprotlib/
006     * 
007     * Redistribution and use in source and binary forms, with or without
008     * modification, are permitted provided that the following conditions are met:
009     * Redistributions of source code must retain the above copyright notice, this
010     * list of conditions and the following disclaimer. Redistributions in binary
011     * form must reproduce the above copyright notice, this list of conditions and
012     * the following disclaimer in the documentation and/or other materials provided
013     * with the distribution. Neither the name of the SIB/GENEBIO nor the names of
014     * its contributors may be used to endorse or promote products derived from this
015     * software without specific prior written permission.
016     * 
017     * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
018     * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
019     * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
020     * ARE DISCLAIMED. IN NO EVENT SHALL SIB/GENEBIO BE LIABLE FOR ANY DIRECT,
021     * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
022     * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
023     * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
024     * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025     * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
026     * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027     */
028    package org.expasy.jpl.commons.collection.symbol;
029    
030    
031    import java.util.HashMap;
032    import java.util.Map;
033    import org.expasy.jpl.commons.collection.symbol.Symbol.SymbolType;
034    import org.expasy.jpl.commons.collection.tree.TreeView;
035    
036    
037    /**
038     * A table that stores symbols grouped by alphabet type.
039     * 
040     * @author nikitin
041     * 
042     * @version 1.0
043     * 
044     */
045    public final class SymbolTable<T> {
046            
047            protected Map<SymbolType<T>, Alphabet<T>> alphabets;
048            
049            public SymbolTable() {
050                    alphabets = new HashMap<SymbolType<T>, Alphabet<T>>();
051            }
052            
053            public final void addAlphabet(AlphabetImpl<T> alphabet) {
054                    if (alphabets.containsKey(alphabet.getSymbolType())) {
055                            throw new AlphabetException(alphabet.getSymbolType()
056                                + ": alphabet already exists");
057                    }
058                    alphabets.put(alphabet.getSymbolType(), alphabet);
059            }
060            
061            public final Alphabet<T> getAlphabet(SymbolType<T> type) {
062                    return alphabets.get(type);
063            }
064            
065            public final void registerSymbolNode(
066                TreeView<? extends Symbol<T>> symbolNode) {
067                    
068                    SymbolType<T> type = symbolNode.getData().getType();
069                    
070                    if (!alphabets.containsKey(type)) {
071                            alphabets.put(type, AlphabetImpl.newInstance(type));
072                    }
073                    Alphabet<T> alphabet = alphabets.get(type);
074                    
075                    alphabet.registerSymbolNode(symbolNode);
076                    
077            }
078            
079            public final void registerAllSymbolNodes(
080                TreeView<? extends Symbol<T>> symbolNode) {
081                    
082                    /* add all other nodes and leaves */
083                    for (TreeView<? extends Symbol<T>> node : symbolNode.getNodes()) {
084                            registerSymbolNode(node);
085                    }
086            }
087            
088            public final TreeView<? extends Symbol<T>> lookUpSymbolNode(char name,
089                SymbolType<T> type) {
090                    
091                    if (!alphabets.containsKey(type)) {
092                            throw new AlphabetException(type + ": alphabet not found");
093                    }
094                    Alphabet<T> alphabet = alphabets.get(type);
095                    
096                    TreeView<? extends Symbol<T>> symbol = alphabet.lookupSymbolNode(name);
097                    
098                    if (symbol == null) {
099                            throw new SymbolNotFoundException(type + "." + name
100                                + ": symbol node not found");
101                    }
102                    return symbol;
103            }
104            
105            public final Symbol<T> lookUpSymbol(char name, SymbolType<T> type) {
106                    return lookUpSymbolNode(name, type).getData();
107            }
108            
109            public final int getSymbolNumber() {
110                    int size = 0;
111                    for (Alphabet<? extends T> alpha : alphabets.values()) {
112                            size += alpha.getSymbolNumber();
113                    }
114                    return size;
115            }
116            
117            public String toString() {
118                    StringBuilder sb = new StringBuilder();
119                    
120                    sb.append(getSymbolNumber() + " symbols: ");
121                    sb.append("{");
122                    for (SymbolType<T> type : alphabets.keySet()) {
123                            // sb.append(type);
124                            // sb.append(": ");
125                            sb.append(alphabets.get(type));
126                            sb.append(", ");
127                    }
128                    if (getSymbolNumber() > 0) {
129                            sb.delete(sb.length() - 2, sb.length());
130                    }
131                    sb.append("}");
132                    
133                    return sb.toString();
134            }
135            
136    }