Enforce immutability via final and unmodifiableList.
This commit is contained in:
@@ -1,60 +1,66 @@
|
||||
package org.insa.graph ;
|
||||
package org.insa.graph;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
public class Graph {
|
||||
|
||||
// Map identifier.
|
||||
private int mapId;
|
||||
|
||||
// Nodes of the graph.
|
||||
private ArrayList<Node> nodes;
|
||||
// Map identifier.
|
||||
private final int mapId;
|
||||
|
||||
/**
|
||||
* @param mapId
|
||||
* @param nodes
|
||||
*/
|
||||
public Graph(int mapId, ArrayList<Node> nodes) {
|
||||
this.mapId = mapId;
|
||||
this.nodes = nodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Nodes of this graph.
|
||||
*/
|
||||
public ArrayList<Node> getNodes() { return nodes; }
|
||||
|
||||
/**
|
||||
* Find the closet node to the given point.
|
||||
*
|
||||
* @param point
|
||||
*
|
||||
* @return Closest node to the given point.
|
||||
*/
|
||||
public Node findClosestNode(Point point) {
|
||||
Node node = null;
|
||||
double minDis = Double.POSITIVE_INFINITY;
|
||||
for (int n = 0 ; n < nodes.size(); ++n) {
|
||||
double dis = point.distanceTo(nodes.get(n).getPoint());
|
||||
if (dis < minDis) {
|
||||
node = nodes.get(n);
|
||||
minDis = dis;
|
||||
}
|
||||
// Nodes of the graph.
|
||||
private final ArrayList<Node> nodes;
|
||||
|
||||
/**
|
||||
* @param mapId
|
||||
* @param nodes
|
||||
*/
|
||||
public Graph(int mapId, ArrayList<Node> nodes) {
|
||||
this.mapId = mapId;
|
||||
this.nodes = nodes;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Nodes of this graph.
|
||||
*/
|
||||
public List<Node> getNodes() {
|
||||
return Collections.unmodifiableList(nodes);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find the closet node to the given point.
|
||||
*
|
||||
* @param point
|
||||
*
|
||||
* @return Closest node to the given point.
|
||||
*/
|
||||
public Node findClosestNode(Point point) {
|
||||
Node node = null;
|
||||
double minDis = Double.POSITIVE_INFINITY;
|
||||
for (int n = 0; n < nodes.size(); ++n) {
|
||||
double dis = point.distanceTo(nodes.get(n).getPoint());
|
||||
if (dis < minDis) {
|
||||
node = nodes.get(n);
|
||||
minDis = dis;
|
||||
}
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Map ID of this graph.
|
||||
*/
|
||||
public int getMapId() { return mapId; }
|
||||
|
||||
/**
|
||||
* @return Return the transpose graph of this graph.
|
||||
*/
|
||||
public Graph transpose() {
|
||||
// TODO:
|
||||
return null;
|
||||
}
|
||||
return node;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Map ID of this graph.
|
||||
*/
|
||||
public int getMapId() {
|
||||
return mapId;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Return the transpose graph of this graph.
|
||||
*/
|
||||
public Graph transpose() {
|
||||
// TODO:
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user