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    import ch.bfh.algo.InvalidAccessorException;
023    import ch.bfh.algo.core.GenericContainer;
024    import ch.bfh.algo.core.GenericPosition;
025    
026    public class ConcretePosition<E,P extends ConcretePosition<E,P>> implements GenericPosition<E,P>{
027            
028            private GenericContainer<E,P> container;
029            private ConcreteLocator<E,P> locator;
030            
031            public ConcreteLocator<E,P> locator(){
032                    return this.locator;
033            }
034            
035            public E element(){
036                    if(this.locator==null) throw new InvalidAccessorException();
037                    else return this.locator.element;
038            }
039            
040            protected void setLocator(ConcreteLocator<E,P> locator){
041                    this.locator=locator;
042            }
043            
044            protected GenericContainer<E,P> container(){
045                    return this.container;
046            }
047            
048            protected void setContainer(GenericContainer<E,P> container){
049                    this.container=container;
050            }
051            
052            protected void swap(P position0, P position1){
053            ConcreteLocator<E,P> locator0=position0.locator();
054            ConcreteLocator<E,P> locator1=position1.locator();
055            position0.setLocator(locator1);
056            locator1.setPosition(position0);
057            position1.setLocator(locator0);
058            locator0.setPosition(position1);
059            }
060            
061            protected E replace(P position, E element){
062                    ConcreteLocator<E,P> locator=position.locator();
063                    E old=locator.element;
064                    locator.setPosition(null); 
065            locator=new ConcreteLocator<E,P>();
066            locator.setElement(element);
067            locator.setPosition(position);
068            position.setLocator(locator);
069            return old;
070            }
071    }