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.forest;
021    
022    import ch.bfh.algo.InvalidAccessorException;
023    import ch.bfh.algo.Position;
024    import ch.bfh.algo.core.GenericBinaryForest;
025    import ch.bfh.algo.forest.ForestFactory;
026    
027    public class GenericBinaryForestGraph<E,P extends GenericBinaryNode<E,P>> extends GenericForestGraph<E,P> implements GenericBinaryForest<E,P>{
028    
029            public GenericBinaryForestGraph(final ForestFactory<P> factory){
030                    super(factory);
031            }
032    
033            public P childLeft(Position<?> parent){
034                    try{return this.childLeft(this.factory.castVertex(parent));}
035                    catch(ClassCastException e){ throw new InvalidAccessorException(); }
036            }
037    
038            public P childLeft(P parent){
039                    this.validate(parent);
040                    return parent.left();
041            }
042    
043            public P childRight(Position<?> parent) {
044                    try{return this.childRight(this.factory.castVertex(parent));}
045                    catch(ClassCastException e){ throw new InvalidAccessorException(); }
046            }
047    
048            public P childRight(P parent){
049                    this.validate(parent);
050                    return parent.right();
051            }
052    
053            public void link(P parent, P child){
054                    throw new UnsupportedOperationException();
055            }
056    
057            public void linkAfter(P parent, P child){
058                    throw new UnsupportedOperationException();
059            }
060    
061            public void linkBefore(P parent, P child){
062                    throw new UnsupportedOperationException();
063            }
064    
065            public void linkLeft(Position<?> parent, Position<?> child) {
066                    try{this.linkLeft(this.factory.castVertex(parent),this.factory.castVertex(child));}
067                    catch(ClassCastException e){ throw new InvalidAccessorException(); }
068            }
069    
070            public void linkLeft(P parent, P child){
071                    P old=this.parent(child);
072                    super.link(parent, child);
073                    child.cleanParent(old);
074                    parent.setLeft(child);
075            }
076    
077            public void linkRight(Position<?> parent, Position<?> child) {
078                    try{this.linkRight(this.factory.castVertex(parent),this.factory.castVertex(child));}
079                    catch(ClassCastException e){ throw new InvalidAccessorException(); }
080            }
081            
082            public void linkRight(P parent, P child) {
083                    P old=this.parent(child);
084                    super.link(parent, child);
085                    child.cleanParent(old);
086                    parent.setRight(child);
087            }
088    
089            public void cut(P child){
090                    child.cleanParent(this.parent(child));
091                    super.cut(child);
092            }
093    
094            public E deleteVertex(P node){
095                    node.cleanParent(this.parent(node));
096                    return super.deleteVertex(node);
097            }
098            
099    }