001    package nl.cwi.sen1.modulemanager.model;
002    
003    import java.util.HashMap;
004    import java.util.Iterator;
005    import java.util.Map;
006    
007    import nl.cwi.sen1.moduleapi.Factory;
008    import nl.cwi.sen1.moduleapi.types.Attribute;
009    import nl.cwi.sen1.moduleapi.types.AttributeStore;
010    import nl.cwi.sen1.moduleapi.types.TableEntryTable;
011    import aterm.ATerm;
012    
013    public class Module {
014        private Map<ATerm, AttributeTable> attributes;
015    
016        private Map<ATerm, AttributeTable> predicates;
017    
018        private Factory factory;
019    
020        public Module(Factory factory) {
021            this.factory = factory;
022            attributes = new HashMap<ATerm, AttributeTable>();
023            predicates = new HashMap<ATerm, AttributeTable>();
024        }
025    
026        public void setAttribute(ATerm namespace, ATerm key, ATerm value) {
027            AttributeTable table = getTable(namespace);
028    
029            if (table != null) {
030                table.setEntry(key, value);
031            } else {
032                table = new AttributeTable(factory);
033                table.setEntry(key, value);
034                attributes.put(namespace, table);
035            }
036        }
037    
038        public ATerm getAttribute(ATerm namespace, ATerm key) {
039            AttributeTable table = getTable(namespace);
040    
041            if (table != null) {
042                return table.getValue(key);
043            }
044            return null;
045        }
046    
047        public AttributeTable getAttributes(ATerm namespace) {
048            return getTable(namespace);
049        }
050    
051        public AttributeStore getAttributes() {
052            AttributeStore store = factory.makeAttributeStore();
053    
054            for (Iterator<ATerm> iter = attributes.keySet().iterator(); iter
055                    .hasNext();) {
056                ATerm namespace = iter.next();
057                TableEntryTable table = getTable(namespace).getTableEntryTable();
058    
059                Attribute attribute = factory.makeAttribute_Attribute(namespace,
060                        table);
061                store = store.append(attribute);
062            }
063    
064            return store;
065        }
066    
067        public void deleteAttribute(ATerm namespace, ATerm key) {
068            AttributeTable table = getTable(namespace);
069    
070            if (table != null) {
071                table.deleteEntry(key);
072            }
073        }
074    
075        private AttributeTable getTable(ATerm namespace) {
076            AttributeTable table = attributes.get(namespace);
077            return table;
078        }
079    
080        private AttributeTable getPredicateTable(ATerm namespace) {
081            AttributeTable table = predicates.get(namespace);
082            return table;
083        }    
084        
085        public AttributeTable getPredicates(ATerm namespace) {
086            return getPredicateTable(namespace);
087        }
088    
089        public void setPredicate(ATerm namespace, ATerm key, ATerm value) {
090            AttributeTable table = getPredicateTable(namespace);
091    
092            if (table != null) {
093                table.setEntry(key, value);
094            } else {
095                table = new AttributeTable(factory);
096                table.setEntry(key, value);
097                predicates.put(namespace, table);
098            }
099        }
100    
101        public ATerm getPredicate(ATerm namespace, ATerm key) {
102            AttributeTable table = getPredicateTable(namespace);
103    
104            if (table != null) {
105                return table.getValue(key);
106            }
107            return null;
108        }
109    
110        public void deletePredicate(ATerm namespace, ATerm key) {
111            AttributeTable table = getPredicateTable(namespace);
112    
113            if (table != null) {
114                table.deleteEntry(key);
115            }
116        }
117    
118        public AttributeStore getPredicates() {
119            AttributeStore store = factory.makeAttributeStore();
120    
121            for (Iterator<ATerm> iter = predicates.keySet().iterator(); iter
122                    .hasNext();) {
123                ATerm namespace = iter.next();
124                TableEntryTable table = getPredicateTable(namespace).getTableEntryTable();
125    
126                Attribute attribute = factory.makeAttribute_Attribute(namespace,
127                        table);
128                store = store.append(attribute);
129            }
130    
131            return store;
132        }
133    }