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.core.sequence;
021    
022    
023    public class GenericLinkedPosition<E,P extends GenericLinkedPosition<E,P>> extends ConcretePosition<E,P> implements Observable{
024    
025        private P previous;
026        private P next;
027        
028            private ObserverSequence before;
029            private ObserverSequence after;
030            
031            protected GenericLinkedPosition(){ this(true); }
032            protected GenericLinkedPosition(boolean observable){ if(observable){this.initObservable();} }
033            
034            public GenericLinkedSequence<E,P> container(){
035                    return (GenericLinkedSequence<E,P>)super.container();
036            }
037            
038            private void initObservable(){
039                    this.before=new ObserverSequence();
040                    this.after=new ObserverSequence();
041            }
042            
043        protected P getPrevious(){
044            return this.previous;
045        }
046        
047        protected void setPrevious(P previous){
048            this.previous=previous;
049        }
050        
051        protected P getNext(){
052            return this.next;
053        }
054        
055        protected void setNext(P next){
056            this.next=next;
057        }
058        
059            public ObserverPosition attachBefore(Observer observer){
060                    return this.before.insert(observer);
061            }
062            
063            public ObserverPosition attachAfter(Observer observer){
064                    return this.after.insert(observer);
065            }
066    
067            public void detachBefore(ObserverPosition position){
068                    if(this.before.encloses(position)){ 
069                            this.before.delete(position);
070                    }
071            }
072            
073            public void detachAfter(ObserverPosition position){
074                    if(this.after.encloses(position)){
075                            this.after.delete(position);
076                    }
077            }
078            
079            public void notifyBefore(){
080                    for(Observer i:this.before) i.updateBefore();
081            }
082            
083            public void notifyAfter(){
084                    for(Observer i:this.after) i.updateAfter();
085            }    
086    }