Enforce immutability via final and unmodifiableList.

This commit is contained in:
Holt59
2018-02-24 21:02:04 +01:00
parent 7640fa34c7
commit d00e012497
9 changed files with 504 additions and 498 deletions

View File

@@ -1,92 +1,95 @@
package org.insa.graph;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Arc {
// Destination node.
private Node origin, destination;
// Length of the road (in meters).
private int length;
// Road information.
RoadInformation info;
// Segments.
ArrayList<Point> points;
/**
* @param dest
* @param length
* @param roadInformation
* @param points
*/
public Arc(Node origin, Node dest, int length, RoadInformation roadInformation) {
this.origin = origin;
this.destination = dest;
this.length = length;
this.info = roadInformation;
this.points = new ArrayList<Point>();
origin.addSuccessor(this);
}
// Destination node.
private final Node origin, destination;
/**
* @param dest
* @param length
* @param roadInformation
* @param points
*/
public Arc(Node origin, Node dest, int length, RoadInformation roadInformation, ArrayList<Point> points) {
this.origin = origin;
this.destination = dest;
this.length = length;
this.info = roadInformation;
this.points = points;
origin.addSuccessor(this);
}
/**
* @return Origin node of this arc.
*/
public Node getOrigin() {
return origin;
}
/**
* @return Destination node of this arc.
*/
public Node getDestination() {
return destination;
}
// Length of the road (in meters).
private final int length;
/**
* @return Length of this arc, in meters.
*/
public int getLength() {
return length;
}
/**
* @return Minimum time required to travel this arc, in seconds.
*/
public double getMinimumTravelTime() {
return getLength() * 3600.0 / (info.getMaximumSpeed() * 1000.0);
}
// Road information.
private final RoadInformation info;
/**
* @return Road information for this arc.
*/
public RoadInformation getInfo() {
return info;
}
// Segments.
private final ArrayList<Point> points;
/**
* @return Points representing segments of this arc. This function may return an empty
* ArrayList if the segments are stored in the reversed arc (for two-ways road).
*/
public ArrayList<Point> getPoints() {
return points;
}
/**
* @param dest
* @param length
* @param roadInformation
* @param points
*/
public Arc(Node origin, Node dest, int length, RoadInformation roadInformation) {
this.origin = origin;
this.destination = dest;
this.length = length;
this.info = roadInformation;
this.points = new ArrayList<Point>();
origin.addSuccessor(this);
}
/**
* @param dest
* @param length
* @param roadInformation
* @param points
*/
public Arc(Node origin, Node dest, int length, RoadInformation roadInformation, ArrayList<Point> points) {
this.origin = origin;
this.destination = dest;
this.length = length;
this.info = roadInformation;
this.points = points;
origin.addSuccessor(this);
}
/**
* @return Origin node of this arc.
*/
public Node getOrigin() {
return origin;
}
/**
* @return Destination node of this arc.
*/
public Node getDestination() {
return destination;
}
/**
* @return Length of this arc, in meters.
*/
public int getLength() {
return length;
}
/**
* @return Minimum time required to travel this arc, in seconds.
*/
public double getMinimumTravelTime() {
return getLength() * 3600.0 / (info.getMaximumSpeed() * 1000.0);
}
/**
* @return Road information for this arc.
*/
public RoadInformation getInfo() {
return info;
}
/**
* @return Points representing segments of this arc. This function may return an
* empty ArrayList if the segments are stored in the reversed arc (for
* two-ways road).
*/
public List<Point> getPoints() {
return Collections.unmodifiableList(points);
}
}

View File

@@ -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;
}
}

View File

@@ -1,65 +1,73 @@
package org.insa.graph;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Node implements Comparable<Node> {
// ID of the node.
private int id;
// Point of this graph.
private Point point;
// Successors.
private ArrayList<Arc> successors;
/**
* Create a new Node corresponding to the given Point with
* an empty list of successors.
*
* @param point
*/
public Node(int id, Point point) {
this.id = id;
this.point = point;
this.successors = new ArrayList<Arc>();
}
// ID of the node.
private final int id;
/**
* Add a successor to this node.
*
* @param arc Arc to the successor.
*/
protected void addSuccessor(Arc arc) {
successors.add(arc);
}
/**
* @return ID of this node.
*/
public int getId() { return id; }
/**
* @return List of successors of this node.
*/
public ArrayList<Arc> getSuccessors() { return successors; }
/**
* @return Point of this node.
*/
public Point getPoint() { return point; }
@Override
public boolean equals(Object other) {
if (other instanceof Node) {
return getId() == ((Node) other).getId();
}
return false;
}
// Point of this graph.
private final Point point;
// Successors.
private final ArrayList<Arc> successors;
/**
* Create a new Node corresponding to the given Point with an empty list of
* successors.
*
* @param point
*/
public Node(int id, Point point) {
this.id = id;
this.point = point;
this.successors = new ArrayList<Arc>();
}
/**
* Add a successor to this node.
*
* @param arc Arc to the successor.
*/
protected void addSuccessor(Arc arc) {
successors.add(arc);
}
/**
* @return ID of this node.
*/
public int getId() {
return id;
}
/**
* @return List of successors of this node.
*/
public List<Arc> getSuccessors() {
return Collections.unmodifiableList(successors);
}
/**
* @return Point of this node.
*/
public Point getPoint() {
return point;
}
@Override
public boolean equals(Object other) {
if (other instanceof Node) {
return getId() == ((Node) other).getId();
}
return false;
}
@Override
public int compareTo(Node other) {
return Integer.compare(getId(), other.getId());
}
@Override
public int compareTo(Node other) {
return Integer.compare(getId(), other.getId());
}
}

View File

@@ -5,62 +5,66 @@ package org.insa.graph;
*
*/
public class Point {
// Earth radius, in meters;
private static final double EARTH_RADIUS = 6378137.0 ;
/**
* Compute the distance between the two given points.
*
* @param long1
* @param lat1
* @param long2
* @param lat2
* @return
*/
public static double distance(Point p1, Point p2) {
double sinLat = Math.sin(Math.toRadians(p1.getLatitude()))*Math.sin(Math.toRadians(p2.getLatitude()));
double cosLat = Math.cos(Math.toRadians(p1.getLatitude()))*Math.cos(Math.toRadians(p2.getLatitude()));
double cosLong = Math.cos(Math.toRadians(p2.getLongitude() - p1.getLongitude()));
return EARTH_RADIUS * Math.acos(sinLat+cosLat*cosLong);
}
// Longitude and latitude of the point.
private double longitude, latitude;
/**
*
* @param longitude Longitude of the point, in degrees.
* @param latitude Latitude of the point, in degrees.
*/
public Point(double longitude, double latitude) {
this.longitude = longitude;
this.latitude = latitude;
}
/**
* @return Longitude of this point (in degrees).
*/
public double getLongitude() { return longitude; }
/**
* @return Latitude of this point (in degrees).
*/
public double getLatitude() { return latitude; }
// Earth radius, in meters;
private static final double EARTH_RADIUS = 6378137.0;
/**
* Compute the distance from this point to the given point
*
* @param target Target point.
*
* @return Distane between this point and the target point, in meters.
*/
public double distanceTo(Point target) {
return distance(this, target);
}
/**
* Compute the distance between the two given points.
*
* @param long1
* @param lat1
* @param long2
* @param lat2
* @return
*/
public static double distance(Point p1, Point p2) {
double sinLat = Math.sin(Math.toRadians(p1.getLatitude())) * Math.sin(Math.toRadians(p2.getLatitude()));
double cosLat = Math.cos(Math.toRadians(p1.getLatitude())) * Math.cos(Math.toRadians(p2.getLatitude()));
double cosLong = Math.cos(Math.toRadians(p2.getLongitude() - p1.getLongitude()));
return EARTH_RADIUS * Math.acos(sinLat + cosLat * cosLong);
}
@Override
public String toString() {
return String.format("Point(%f, %f)", getLongitude(), getLatitude());
}
// Longitude and latitude of the point.
private final double longitude, latitude;
/**
*
* @param longitude Longitude of the point, in degrees.
* @param latitude Latitude of the point, in degrees.
*/
public Point(double longitude, double latitude) {
this.longitude = longitude;
this.latitude = latitude;
}
/**
* @return Longitude of this point (in degrees).
*/
public double getLongitude() {
return longitude;
}
/**
* @return Latitude of this point (in degrees).
*/
public double getLatitude() {
return latitude;
}
/**
* Compute the distance from this point to the given point
*
* @param target Target point.
*
* @return Distane between this point and the target point, in meters.
*/
public double distanceTo(Point target) {
return distance(this, target);
}
@Override
public String toString() {
return String.format("Point(%f, %f)", getLongitude(), getLatitude());
}
}

View File

@@ -1,85 +1,75 @@
package org.insa.graph ;
package org.insa.graph;
/**
* Class containing information for road that may be shared
* by multiple arcs.
* Class containing information for road that may be shared by multiple arcs.
*
*/
public class RoadInformation {
/**
* Road type.
*/
public enum RoadType {
MOTORWAY,
TRUNK,
PRIMARY,
SECONDARY,
MOTORWAY_LINK,
TRUNK_LINK,
PRIMARY_LINK,
SECONDARY_LINK,
TERTIARY,
RESIDENTIAL,
UNCLASSIFIED,
ROAD,
LIVING_STREET,
SERVICE,
ROUNDABOUT,
COASTLINE
}
// Type of the road (see above).
private RoadType type;
/**
* Road type.
*/
public enum RoadType {
MOTORWAY, TRUNK, PRIMARY, SECONDARY, MOTORWAY_LINK, TRUNK_LINK, PRIMARY_LINK, SECONDARY_LINK, TERTIARY, RESIDENTIAL, UNCLASSIFIED, ROAD, LIVING_STREET, SERVICE, ROUNDABOUT, COASTLINE
}
// One way road?
private boolean oneway;
// Type of the road (see above).
private final RoadType type;
// Max speed in kilometers per hour.
private int maxSpeed;
// One way road?
private final boolean oneway;
// Name of the road.
private String name;
// Max speed in kilometers per hour.
private final int maxSpeed;
public RoadInformation(RoadType roadType, boolean isOneWay, int maxSpeed, String name) {
this.type = roadType;
this.oneway = isOneWay;
this.maxSpeed = maxSpeed;
this.name = name;
}
/**
* @return Type of the road.
*/
public RoadType getType() { return type; }
/**
* @return true if this is a one-way road.
*/
public boolean isOneWay() { return oneway; }
/**
* @return Maximum speed for this road (in km/h).
*/
public int getMaximumSpeed() { return maxSpeed; }
/**
* @return Name of the road.
*/
public String getName() { return name; }
// Name of the road.
private final String name;
@Override
public String toString() {
String typeAsString = "road";
if (getType() == RoadType.COASTLINE) {
typeAsString = "coast";
}
if (getType() == RoadType.MOTORWAY) {
typeAsString = "highway";
}
return typeAsString + " : " + getName()
+ " " + (isOneWay() ? " (oneway) " : "")
+ maxSpeed + " km/h (max.)";
}
public RoadInformation(RoadType roadType, boolean isOneWay, int maxSpeed, String name) {
this.type = roadType;
this.oneway = isOneWay;
this.maxSpeed = maxSpeed;
this.name = name;
}
/**
* @return Type of the road.
*/
public RoadType getType() {
return type;
}
/**
* @return true if this is a one-way road.
*/
public boolean isOneWay() {
return oneway;
}
/**
* @return Maximum speed for this road (in km/h).
*/
public int getMaximumSpeed() {
return maxSpeed;
}
/**
* @return Name of the road.
*/
public String getName() {
return name;
}
@Override
public String toString() {
String typeAsString = "road";
if (getType() == RoadType.COASTLINE) {
typeAsString = "coast";
}
if (getType() == RoadType.MOTORWAY) {
typeAsString = "highway";
}
return typeAsString + " : " + getName() + " " + (isOneWay() ? " (oneway) " : "") + maxSpeed + " km/h (max.)";
}
}