/**
 *
 * Node object for graph programming. For example used in dijkstra's 
 * algorithm java applet interactive demo.
 *
 * www.tech-algorithm.com
 *
 */
import java.awt.Point;
import java.util.Iterator;
import java.util.LinkedList;

public class Node {
    
    private String name ;
    private Node parent ;
    private float overallCost ;
    private float costFromParent ;
    private LinkedList paths ;
    private boolean open, closed ;
    private Point point ;
    
    public Node(String name) {
        this.name = name ;
        parent = null ;
        overallCost = 0.0f ;
        costFromParent = 0.0f ;
        paths = new LinkedList() ;
        // by default a node is not open or closed
        setOpen(false) ;
        setClosed(false) ;
        point = null ;
    }
    
    public void connectNode(Node toNode,float cost) {
        // check if already connected to avoid duplicate connection
        Iterator i = paths.listIterator() ;
        Path path ;
        while (i.hasNext()) {
            path = (Path)i.next() ;
            if ((path.getEndA().equals(toNode)) || (path.getEndB().equals(toNode))) {
                System.out.println(getName()+" is already connected to "+toNode.getName()) ;
                return ; // already connected to the node
            }
        }
        
        // connect this node with toNode
        path = new Path(this, toNode, cost) ;
        paths.add(path) ;        
        toNode.addPath(path) ;
    }
    
    public void addPath(Path path) {
        // check if path already exist to avoid duplicate
        Iterator i = paths.listIterator() ;
        while (i.hasNext()) {
            Path p = (Path)i.next() ;
            if (p.equals(path)) {
                System.out.println("The path already exists!") ;
                return ;
            }
        }        
        paths.add(path) ;
    }
    
    public Iterator getNeighbors() {
        return paths.listIterator() ;
    }
    
    public void setParent(Node newParent) {
        parent = newParent ;
    }
    
    public Node getParent() {
        return parent ;
    }
    
    public void setOverallCost(float newCost) {
        overallCost = newCost ;
    }
    
    public float getOverallCost() {
        return overallCost ;
    }
    
    public boolean isOpen() {
        return open;
    }
    
    public void setOpen(boolean open) {
        this.open = open;
    }
    
    public boolean isClosed() {
        return closed;
    }
    
    public void setClosed(boolean closed) {
        this.closed = closed;
    }
    
    public String getName() {
        return name;
    }
    
    public float getCostFromParent() {
        return costFromParent ;
    }
    
    public void setCostFromParent(float val) {
        costFromParent = val ;
    }
    
    public void setPoint(Point point) {
        this.point = point ;
    }
    
    public Point getPoint() {
        return point ;
    }
    
}
