/* Copyright 2008-2010 Gephi Authors : Mathieu Bastian <mathieu.bastian@gephi.org> Website : http://www.gephi.org This file is part of Gephi. Gephi is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. Gephi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with Gephi. If not, see <http://www.gnu.org/licenses/>. */ package org.gephi.graph.dhns.edge.iterators; import java.util.Iterator; import org.gephi.utils.collection.avl.ParamAVLIterator; import org.gephi.graph.api.Edge; import org.gephi.graph.dhns.edge.AbstractEdge; import org.gephi.graph.dhns.edge.MetaEdgeImpl; import org.gephi.graph.dhns.utils.avl.MetaEdgeTree; /** * Edge Iterator for edges linked to the given node. It gives IN, OUT or IN+OUT edges * * @author Mathieu Bastian * @see EdgeNodeIterator */ public class MetaEdgeNodeIterator extends AbstractEdgeIterator implements Iterator<Edge> { public enum EdgeNodeIteratorMode { OUT, IN, BOTH }; protected ParamAVLIterator<MetaEdgeImpl> edgeIterator; protected EdgeNodeIteratorMode mode; protected MetaEdgeImpl pointer; protected boolean undirected; protected MetaEdgeTree outTree; protected MetaEdgeTree inTree; public MetaEdgeNodeIterator(MetaEdgeTree outTree, MetaEdgeTree inTree, EdgeNodeIteratorMode mode, boolean undirected) { this.outTree = outTree; this.inTree = inTree; this.mode = mode; this.edgeIterator = new ParamAVLIterator<MetaEdgeImpl>(); if (mode.equals(EdgeNodeIteratorMode.OUT) || mode.equals(EdgeNodeIteratorMode.BOTH)) { this.edgeIterator.setNode(outTree); } else { this.edgeIterator.setNode(inTree); } this.undirected = undirected; } public boolean hasNext() { while (pointer == null || (undirected && pointer.getUndirected() != pointer)) { if (mode.equals(EdgeNodeIteratorMode.BOTH)) { boolean res = edgeIterator.hasNext(); if (res) { pointer = edgeIterator.next(); if (pointer.isSelfLoop()) { //Ignore self loop here to avoid double iteration pointer = null; } } else { this.edgeIterator.setNode(inTree); this.mode = EdgeNodeIteratorMode.IN; } } else { if (edgeIterator.hasNext()) { pointer = edgeIterator.next(); } else { return false; } } } return true; } public AbstractEdge next() { AbstractEdge e = pointer; pointer = null; return e; } public void remove() { throw new UnsupportedOperationException("Not supported yet."); } }