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.graph; 021 022 import java.util.List; 023 024 import ch.bfh.algo.Direction; 025 import ch.bfh.algo.DirectionException; 026 import ch.bfh.algo.core.position.PositionList; 027 import ch.bfh.algo.core.position.PositionListAdapter; 028 import ch.bfh.algo.core.sequence.GenericLinkedPosition; 029 import ch.bfh.algo.sequence.LinkedPosition; 030 import ch.bfh.algo.sequence.LinkedSequence; 031 032 public class GenericVertex<E,V,PE extends GenericEdge<E,V,PE,PV>,PV extends GenericVertex<E,V,PE,PV>> extends GenericLinkedPosition<V,PV>{ 033 034 private LinkedPosition<PV> verticesPos; 035 036 private final LinkedSequence<PE> in=new LinkedSequence<PE>(); 037 private final LinkedSequence<PE> out=new LinkedSequence<PE>(); 038 private final LinkedSequence<PE> undirected=new LinkedSequence<PE>(); 039 private final List<PE> directed=new ListMergeAdapter<PE>(this.in,this.out); 040 private final List<PE> edges=new ListMergeAdapter<PE>(this.undirected,this.directed); 041 042 private PositionList<E,PE> inEdges; 043 private PositionList<E,PE> outEdges; 044 private PositionList<E,PE> undirectedEdges; 045 private PositionList<E,PE> directedEdges; 046 private PositionList<E,PE> allEdges; 047 048 private PositionList<V,PV> inVertices; 049 private PositionList<V,PV> outVertices; 050 private PositionList<V,PV> undirectedVertices; 051 private PositionList<V,PV> directedVertices; 052 private PositionList<V,PV> allVertices; 053 054 protected void insert(LinkedSequence<PV> vertices){ 055 this.verticesPos=vertices.insert((PV)this); 056 } 057 058 protected void delete(){ 059 this.verticesPos.container().delete(this.verticesPos); 060 for(PE e:this.edges) e.clearVertex(this); 061 } 062 063 protected void attachInDirected(GenericEdge<E,V,PE,PV> edge){ 064 edge.setDestination((PV)this,this.in); 065 } 066 067 protected void attachInUndirected(GenericEdge<E,V,PE,PV> edge){ 068 edge.setDestination((PV)this,this.undirected); 069 } 070 071 protected LinkedPosition<PE> attachOutDirected(GenericEdge<E,V,PE,PV> edge){ 072 return edge.setOrigin((PV)this,this.out); 073 } 074 075 protected LinkedPosition<PE> attachOutDirectedBefore(GenericEdge<E,V,PE,PV> edge, LinkedPosition<PE> position){ 076 return edge.setOriginBefore((PV)this,this.out,position); 077 } 078 079 protected LinkedPosition<PE> attachOutDirectedAfter(GenericEdge<E,V,PE,PV> edge, LinkedPosition<PE> position){ 080 return edge.setOriginAfter((PV)this,this.out,position); 081 } 082 083 protected LinkedPosition<PE> attachOutUndirected(GenericEdge<E,V,PE,PV> edge){ 084 return edge.setOrigin((PV)this,this.undirected); 085 } 086 087 protected LinkedPosition<PE> attachOutUndirectedBefore(GenericEdge<E,V,PE,PV> edge, LinkedPosition<PE> position){ 088 return edge.setOriginBefore((PV)this,this.undirected,position); 089 } 090 091 protected LinkedPosition<PE> attachOutUndirectedAfter(GenericEdge<E,V,PE,PV> edge, LinkedPosition<PE> position){ 092 return edge.setOriginAfter((PV)this,this.undirected,position); 093 } 094 095 protected PositionList<E,PE> incidentEdges(Direction type){ 096 switch(type){ 097 case ALL: return this.allEdges(); 098 case DIRECTED: return this.directedEdges(); 099 case UNDIRECTED: return this.undirectedEdges(); 100 case IN: return this.inEdges(); 101 case OUT: return this.outEdges(); 102 } 103 throw new DirectionException(); 104 } 105 106 private PositionList<E,PE> inEdges(){ 107 if(this.inEdges==null){ this.inEdges=new PositionListAdapter<E,PE>(this.in); } 108 return this.inEdges; 109 } 110 111 private PositionList<E,PE> outEdges(){ 112 if(this.outEdges==null){ this.outEdges=new PositionListAdapter<E,PE>(this.out); } 113 return this.outEdges; 114 } 115 116 private PositionList<E,PE> undirectedEdges(){ 117 if(this.undirectedEdges==null){ this.undirectedEdges=new PositionListAdapter<E,PE>(this.undirected); } 118 return this.undirectedEdges; 119 } 120 121 private PositionList<E,PE> directedEdges(){ 122 if(this.directedEdges==null){ this.directedEdges=new PositionListAdapter<E,PE>(this.directed); } 123 return this.directedEdges; 124 } 125 126 private PositionList<E,PE> allEdges(){ 127 if(this.allEdges==null){ this.allEdges=new PositionListAdapter<E,PE>(new ListMergeAdapter<PE>(this.undirected,new ListMergeAdapter<PE>(this.in,this.out))); } 128 return this.allEdges; 129 } 130 131 protected PositionList<V,PV> adjacentVertices(Direction type){ 132 switch(type){ 133 case ALL: return this.allVertices(); 134 case DIRECTED: return this.directedVertices(); 135 case UNDIRECTED: return this.undirectedVertices(); 136 case IN: return this.inVertices(); 137 case OUT: return this.outVertices(); 138 } 139 throw new DirectionException(); 140 } 141 142 private PositionList<V,PV> inVertices(){ 143 if(this.inVertices==null){ this.inVertices=new PositionListAdapter<V,PV>(new VertexListAdapter<PE,PV>(this.in,this)); } 144 return this.inVertices; 145 } 146 147 private PositionList<V,PV> outVertices(){ 148 if(this.outVertices==null){ this.outVertices=new PositionListAdapter<V,PV>(new VertexListAdapter<PE,PV>(this.out,this)); } 149 return this.outVertices; 150 } 151 152 private PositionList<V,PV> undirectedVertices(){ 153 if(this.undirectedVertices==null){ this.undirectedVertices=new PositionListAdapter<V,PV>(new VertexListAdapter<PE,PV>(this.undirected,this)); } 154 return this.undirectedVertices; 155 } 156 157 private PositionList<V,PV> directedVertices(){ 158 if(this.directedVertices==null){ this.directedVertices=new PositionListAdapter<V,PV>(new VertexListAdapter<PE,PV>(this.directed,this)); } 159 return this.directedVertices; 160 } 161 162 private PositionList<V,PV> allVertices(){ 163 if(this.allVertices==null){ this.allVertices=new PositionListAdapter<V,PV>(new VertexListAdapter<PE,PV>(this.edges,this)); } 164 return this.allVertices; 165 } 166 167 }