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

@ -2,9 +2,10 @@ package org.insa.algo.weakconnectivity;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import org.insa.algo.AbstractAlgorithm;
import org.insa.algo.AbstractSolution.Status;
@ -45,7 +46,8 @@ public class WeaklyConnectedComponentsAlgorithm extends AbstractAlgorithm<Weakly
}
/**
* Notify all observers that a new node has been found for the current component.
* Notify all observers that a new node has been found for the current
* component.
*
* @param node New node found for the current component.
*/
@ -67,7 +69,8 @@ public class WeaklyConnectedComponentsAlgorithm extends AbstractAlgorithm<Weakly
}
/**
* @return An adjacency list for the undirected graph equivalent to the stored graph.
* @return An adjacency list for the undirected graph equivalent to the stored
* graph.
*/
protected ArrayList<HashSet<Integer>> createUndirectedGraph() {
int nNodes = getInstance().getGraph().getNodes().size();
@ -89,8 +92,8 @@ public class WeaklyConnectedComponentsAlgorithm extends AbstractAlgorithm<Weakly
}
/**
* Apply a breadth first search algorithm on the given undirected graph (adjacency list),
* starting at node cur, and marking nodes in marked.
* Apply a breadth first search algorithm on the given undirected graph
* (adjacency list), starting at node cur, and marking nodes in marked.
*
* @param marked
* @param cur
@ -98,7 +101,7 @@ public class WeaklyConnectedComponentsAlgorithm extends AbstractAlgorithm<Weakly
* @return
*/
protected ArrayList<Node> bfs(ArrayList<HashSet<Integer>> ugraph, boolean[] marked, int cur) {
ArrayList<Node> nodes = getInstance().getGraph().getNodes();
List<Node> nodes = getInstance().getGraph().getNodes();
ArrayList<Node> component = new ArrayList<Node>();
// Using a queue because we are doing a BFS
@ -148,7 +151,8 @@ public class WeaklyConnectedComponentsAlgorithm extends AbstractAlgorithm<Weakly
components.add(this.bfs(ugraph, marked, cur));
// Find next non-marked
for (; cur < marked.length && marked[cur]; ++cur);
for (; cur < marked.length && marked[cur]; ++cur)
;
}
return new WeaklyConnectedComponentsSolution(getInstance(), Status.OPTIMAL, components);

View File

@ -1,20 +1,22 @@
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;
private final Node origin, destination;
// Length of the road (in meters).
private int length;
private final int length;
// Road information.
RoadInformation info;
private final RoadInformation info;
// Segments.
ArrayList<Point> points;
private final ArrayList<Point> points;
/**
* @param dest
@ -82,11 +84,12 @@ public class Arc {
}
/**
* @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).
* @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;
public List<Point> getPoints() {
return Collections.unmodifiableList(points);
}
}

View File

@ -1,14 +1,16 @@
package org.insa.graph;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class Graph {
// Map identifier.
private int mapId;
private final int mapId;
// Nodes of the graph.
private ArrayList<Node> nodes;
private final ArrayList<Node> nodes;
/**
* @param mapId
@ -22,7 +24,9 @@ public class Graph {
/**
* @return Nodes of this graph.
*/
public ArrayList<Node> getNodes() { return nodes; }
public List<Node> getNodes() {
return Collections.unmodifiableList(nodes);
}
/**
* Find the closet node to the given point.
@ -47,7 +51,9 @@ public class Graph {
/**
* @return Map ID of this graph.
*/
public int getMapId() { return mapId; }
public int getMapId() {
return mapId;
}
/**
* @return Return the transpose graph of this graph.

View File

@ -1,21 +1,23 @@
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;
private final int id;
// Point of this graph.
private Point point;
private final Point point;
// Successors.
private ArrayList<Arc> successors;
private final ArrayList<Arc> successors;
/**
* Create a new Node corresponding to the given Point with
* an empty list of successors.
* Create a new Node corresponding to the given Point with an empty list of
* successors.
*
* @param point
*/
@ -37,17 +39,23 @@ public class Node implements Comparable<Node> {
/**
* @return ID of this node.
*/
public int getId() { return id; }
public int getId() {
return id;
}
/**
* @return List of successors of this node.
*/
public ArrayList<Arc> getSuccessors() { return successors; }
public List<Arc> getSuccessors() {
return Collections.unmodifiableList(successors);
}
/**
* @return Point of this node.
*/
public Point getPoint() { return point; }
public Point getPoint() {
return point;
}
@Override
public boolean equals(Object other) {

View File

@ -26,7 +26,7 @@ public class Point {
}
// Longitude and latitude of the point.
private double longitude, latitude;
private final double longitude, latitude;
/**
*
@ -41,12 +41,16 @@ public class Point {
/**
* @return Longitude of this point (in degrees).
*/
public double getLongitude() { return longitude; }
public double getLongitude() {
return longitude;
}
/**
* @return Latitude of this point (in degrees).
*/
public double getLatitude() { return latitude; }
public double getLatitude() {
return latitude;
}
/**
* Compute the distance from this point to the given point

View File

@ -1,8 +1,7 @@
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 {
@ -11,35 +10,20 @@ 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
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;
private final RoadType type;
// One way road?
private boolean oneway;
private final boolean oneway;
// Max speed in kilometers per hour.
private int maxSpeed;
private final int maxSpeed;
// Name of the road.
private String name;
private final String name;
public RoadInformation(RoadType roadType, boolean isOneWay, int maxSpeed, String name) {
this.type = roadType;
@ -51,22 +35,30 @@ public class RoadInformation {
/**
* @return Type of the road.
*/
public RoadType getType() { return type; }
public RoadType getType() {
return type;
}
/**
* @return true if this is a one-way road.
*/
public boolean isOneWay() { return oneway; }
public boolean isOneWay() {
return oneway;
}
/**
* @return Maximum speed for this road (in km/h).
*/
public int getMaximumSpeed() { return maxSpeed; }
public int getMaximumSpeed() {
return maxSpeed;
}
/**
* @return Name of the road.
*/
public String getName() { return name; }
public String getName() {
return name;
}
@Override
public String toString() {
@ -77,9 +69,7 @@ public class RoadInformation {
if (getType() == RoadType.MOTORWAY) {
typeAsString = "highway";
}
return typeAsString + " : " + getName()
+ " " + (isOneWay() ? " (oneway) " : "")
+ maxSpeed + " km/h (max.)";
return typeAsString + " : " + getName() + " " + (isOneWay() ? " (oneway) " : "") + maxSpeed + " km/h (max.)";
}
}

View File

@ -11,9 +11,9 @@ import java.awt.event.MouseListener;
import java.awt.geom.NoninvertibleTransformException;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.swing.JPanel;
@ -238,14 +238,12 @@ public class BasicDrawing extends JPanel implements Drawing {
/**
* Draw the given arc.
*
* @param arc
* Arc to draw.
* @param palette
* Palette to use to retrieve color and width for arc, or null to use
* current settings.
* @param arc Arc to draw.
* @param palette Palette to use to retrieve color and width for arc, or null to
* use current settings.
*/
public void drawArc(Arc arc, GraphPalette palette) {
ArrayList<Point> pts = arc.getPoints();
List<Point> pts = arc.getPoints();
if (!pts.isEmpty()) {
if (palette != null) {
setColor(palette.getColorForType(arc.getInfo().getType()));

View File

@ -1,7 +1,7 @@
package org.insa.graphics.drawing.utils;
import java.awt.Color;
import java.util.ArrayList;
import java.util.List;
import org.insa.graph.Point;
import org.mapsforge.core.graphics.Canvas;
@ -41,7 +41,7 @@ public class PolylineAutoScaling extends Polyline {
/**
* @param points
*/
public void add(ArrayList<Point> points) {
public void add(List<Point> points) {
for (Point point: points) {
add(point);
}

View File

@ -1,22 +1,15 @@
package org.insa.graph.io;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.insa.graph.Graph;
import org.insa.graph.Node;
import org.insa.graph.Point;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
public class BinaryGraphReaderTest {
@ -38,7 +31,7 @@ public class BinaryGraphReaderTest {
@Test
void testMidipNodes() {
ArrayList<Node> nodes = midip.getNodes();
List<Node> nodes = midip.getNodes();
assertEquals(nodes.size(), 150827);