001    package toolbus.environment;
002    
003    import java.util.Iterator;
004    import java.util.LinkedList;
005    import java.util.List;
006    
007    /**
008     * ListBindings uses a simple LinkedList to implement the Bindings interface.
009     */
010    class ListBindings implements Bindings{
011            private final LinkedList<Binding> bindings;
012            
013            public ListBindings(){
014                    bindings = new LinkedList<Binding>();
015            }
016            
017            protected ListBindings(ListBindings listBindings){
018                    bindings = new LinkedList<Binding>();
019                    bindings.addAll(listBindings.bindings);
020            }
021            
022            public Bindings clone(){
023                    return new ListBindings(this);
024            }
025            
026            public Binding get(String key){
027                    Iterator<Binding> bindingsIterator = bindings.iterator();
028                    while(bindingsIterator.hasNext()){
029                            Binding b = bindingsIterator.next();
030                            
031                            if(b.getName().equals(key)){
032                                    return b;
033                            }
034                    }
035                    return null;
036            }
037            
038            public int size(){
039                    return bindings.size();
040            }
041            
042            public void put(String key, Binding b){
043                    bindings.addFirst(b);
044            }
045            
046            public void remove(String key){
047                    Iterator<Binding> bindingIterator = bindings.iterator();
048                    while(bindingIterator.hasNext()){
049                            Binding b = bindingIterator.next();
050                            if(b.getName().equals(key)){
051                                    bindingIterator.remove();
052                                    return;
053                            }
054                    }
055                    System.err.println("remove:" + key + " is not present\n" + this);
056            }
057            
058            public List<Binding> getBindingsAsList(){
059                    List<Binding> newBindings = new LinkedList<Binding>();
060                    newBindings.addAll(bindings);
061                    
062                    return newBindings;
063            }
064            
065            public String toString(){
066                    String res = "{", sep = "";
067                    
068                    Iterator<Binding> bindingsIterator = bindings.iterator();
069                    while(bindingsIterator.hasNext()){
070                            Binding b = bindingsIterator.next();
071                            
072                            res += sep + b;
073                            sep = ", ";
074                    }
075                    return res + "}";
076            }
077    }