001    /*
002     *  www.ti.bfh.ch
003     *
004     *  Copyright 2007, Berne University of Applied Sciences, 
005     *  School of Engineering and Information Technology
006     *  and individual contributors as indicated by the @authors tag.
007     *
008     *  This is free software; you can redistribute it and/or modify it under the terms of the 
009     *  GNU Lesser General Public License as published by the Free Software Foundation; 
010     *  either version 3 of the License, or (at your option) any later version.
011     *
012     *  This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
013     *  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
014     *  See the GNU Lesser General Public License for more details.
015     *
016     *  You should have received a copy of the GNU Lesser General Public License along with this software; 
017     *  if not, see <http://www.gnu.org/licenses/>.
018     *
019     */
020    package ch.bfh.algo.example;
021    
022    import java.util.Iterator;
023    
024    import ch.bfh.algo.Position;
025    import ch.bfh.algo.Sequence;
026    import ch.bfh.algo.sequence.LinkedPosition;
027    import ch.bfh.algo.sequence.LinkedSequence;
028    import ch.bfh.algo.sequence.SequenceFactory;
029    
030    
031    public class DecoratedSequenceExample<E>{
032            
033            private Sequence<E> sequence;
034    
035            class DecoratedPosition extends LinkedPosition<E>{
036                    private int n; 
037                    public void set(int n){ this.n=n; }
038                    public int get(){ return this.n; }
039            }
040    
041            public DecoratedSequenceExample(){
042                    this.sequence=new LinkedSequence<E>(new SequenceFactory<LinkedPosition<E>>(){
043                            public LinkedPosition<E> createPosition(){ return new DecoratedPosition(); }
044                            public LinkedPosition<E> cast(Position<?> position){ return (LinkedPosition<E>)position; }    
045                    });
046            }
047            
048            public void fill(int n){
049                    while(n-->0){
050                            DecoratedPosition p=(DecoratedPosition)this.sequence.insert(null);
051                            p.set(n);
052                    }
053            }
054            
055            public int sum(){
056                    int sum=0;
057                    Iterator<Position<E>> i=sequence.positionIterator();
058                    while(i.hasNext()) sum+=((DecoratedPosition)i.next()).get();
059                    return sum;
060            }
061    
062            public void example(){
063                    this.fill(100);
064                    System.out.println(this.sum());
065            }
066    
067            public static void main(String[] args){
068                    new DecoratedSequenceExample<Object>().example();
069            }
070    }
071    
072