[model] Clean Java classes.

This commit is contained in:
Mikaël Capelle 2020-03-27 13:10:36 +01:00 committed by Mikael CAPELLE
parent c9ff67b10e
commit 2f936d44ec
11 changed files with 71 additions and 78 deletions

View File

@ -170,7 +170,7 @@ public class AccessRestrictions {
* Create a new AccessRestrictions instances with the given restrictions. * Create a new AccessRestrictions instances with the given restrictions.
* *
* @param restrictions Map of restrictions for this instance of * @param restrictions Map of restrictions for this instance of
* AccessRestrictions. * AccessRestrictions.
*/ */
public AccessRestrictions(EnumMap<AccessMode, AccessRestriction> restrictions) { public AccessRestrictions(EnumMap<AccessMode, AccessRestriction> restrictions) {
this.restrictions = restrictions; this.restrictions = restrictions;
@ -191,7 +191,7 @@ public class AccessRestrictions {
* Check if the restriction associated with the given mode is one of the given * Check if the restriction associated with the given mode is one of the given
* restrictions. * restrictions.
* *
* @param mode Mode for which to check the restrictions. * @param mode Mode for which to check the restrictions.
* @param restrictions List of queried restrictions for the mode. * @param restrictions List of queried restrictions for the mode.
* *
* @return {@code true} if the restriction of the given mode is one of the given * @return {@code true} if the restriction of the given mode is one of the given
@ -205,7 +205,7 @@ public class AccessRestrictions {
* Check if the restriction for the given mode corresponds to the given * Check if the restriction for the given mode corresponds to the given
* restriction. * restriction.
* *
* @param mode Mode for which the restriction should be checked. * @param mode Mode for which the restriction should be checked.
* @param restriction Restriction to check against. * @param restriction Restriction to check against.
* *
* @return {@code true} if the restriction of the given mode corresponds to the * @return {@code true} if the restriction of the given mode corresponds to the
@ -219,7 +219,7 @@ public class AccessRestrictions {
* Check if the restriction associated to each given mode is one of the * Check if the restriction associated to each given mode is one of the
* restrictions. The restriction may not be the same for all modes. * restrictions. The restriction may not be the same for all modes.
* *
* @param modes Modes for which restrictions should be checked. * @param modes Modes for which restrictions should be checked.
* @param restrictions Set of wanted restrictions for the modes. * @param restrictions Set of wanted restrictions for the modes.
* *
* @return {@code true} if all the given modes are allowed for any of the given * @return {@code true} if all the given modes are allowed for any of the given
@ -227,10 +227,6 @@ public class AccessRestrictions {
*/ */
public boolean areAllAllowedForAny(EnumSet<AccessMode> modes, public boolean areAllAllowedForAny(EnumSet<AccessMode> modes,
EnumSet<AccessRestriction> restrictions) { EnumSet<AccessRestriction> restrictions) {
boolean allowed = true; return modes.stream().allMatch(mode -> isAllowedForAny(mode, restrictions));
for (AccessMode mode: modes) {
allowed = allowed && isAllowedForAny(mode, restrictions);
}
return allowed;
} }
} }

View File

@ -47,7 +47,8 @@ class ArcBackward extends Arc {
@Override @Override
public List<Point> getPoints() { public List<Point> getPoints() {
List<Point> pts = new ArrayList<>(this.originalArc.getPoints()); // There does not seem to be a cleaner way to do this using Java 8 streams.
final List<Point> pts = new ArrayList<>(this.originalArc.getPoints());
Collections.reverse(pts); Collections.reverse(pts);
return pts; return pts;
} }

View File

@ -32,9 +32,9 @@ public final class Graph {
/** /**
* Create a new graph with the given ID, name, nodes and information. * Create a new graph with the given ID, name, nodes and information.
* *
* @param mapId ID of the map corresponding to this graph. * @param mapId ID of the map corresponding to this graph.
* @param mapName Name of the map corresponding to this graph. * @param mapName Name of the map corresponding to this graph.
* @param nodes List of nodes for this graph. * @param nodes List of nodes for this graph.
* @param graphStatistics Information for this graph. * @param graphStatistics Information for this graph.
*/ */
public Graph(String mapId, String mapName, List<Node> nodes, GraphStatistics graphStatistics) { public Graph(String mapId, String mapName, List<Node> nodes, GraphStatistics graphStatistics) {
@ -98,20 +98,20 @@ public final class Graph {
* @return Transpose graph of this graph. * @return Transpose graph of this graph.
*/ */
public Graph transpose() { public Graph transpose() {
ArrayList<Node> trNodes = new ArrayList<>(nodes.size()); final ArrayList<Node> trNodes = new ArrayList<>(nodes.size());
for (Node node: nodes) { for (Node node: nodes) {
trNodes.add(new Node(node.getId(), node.getPoint())); trNodes.add(new Node(node.getId(), node.getPoint()));
} }
for (Node node: nodes) { for (Node node: nodes) {
Node orig = trNodes.get(node.getId()); final Node orig = trNodes.get(node.getId());
for (Arc arc: node.getSuccessors()) { for (Arc arc: node.getSuccessors()) {
if (arc.getRoadInformation().isOneWay()) { if (arc.getRoadInformation().isOneWay()) {
Node dest = trNodes.get(arc.getDestination().getId()); final Node dest = trNodes.get(arc.getDestination().getId());
dest.addSuccessor(new ArcBackward(new ArcForward(orig, dest, arc.getLength(), dest.addSuccessor(new ArcBackward(new ArcForward(orig, dest, arc.getLength(),
arc.getRoadInformation(), arc.getPoints()))); arc.getRoadInformation(), arc.getPoints())));
} }
else if (arc instanceof ArcForward) { else if (arc instanceof ArcForward) {
Node dest = trNodes.get(arc.getDestination().getId()); final Node dest = trNodes.get(arc.getDestination().getId());
Arc newArc = new ArcForward(orig, dest, arc.getLength(), Arc newArc = new ArcForward(orig, dest, arc.getLength(),
arc.getRoadInformation(), arc.getPoints()); arc.getRoadInformation(), arc.getPoints());
dest.addSuccessor(new ArcBackward(newArc)); dest.addSuccessor(new ArcBackward(newArc));

View File

@ -33,23 +33,23 @@ public final class Node implements Comparable<Node> {
* corresponding backward arc is created and add to destination. * corresponding backward arc is created and add to destination.
* </p> * </p>
* *
* @param origin Origin of the arc. * @param origin Origin of the arc.
* @param destination Destination of the arc. * @param destination Destination of the arc.
* @param length Length of the arc. * @param length Length of the arc.
* @param roadInformation Information corresponding to the arc. * @param roadInformation Information corresponding to the arc.
* @param points Points for the arc. * @param points Points for the arc.
* *
* @return The newly created forward arc (origin to destination). * @return The newly created forward arc (origin to destination).
*/ */
public static Arc linkNodes(Node origin, Node destination, float length, public static Arc linkNodes(Node origin, Node destination, float length,
RoadInformation roadInformation, ArrayList<Point> points) { RoadInformation roadInformation, ArrayList<Point> points) {
Arc arc = null; final Arc arc;
if (roadInformation.isOneWay()) { if (roadInformation.isOneWay()) {
arc = new ArcForward(origin, destination, length, roadInformation, points); arc = new ArcForward(origin, destination, length, roadInformation, points);
origin.addSuccessor(arc); origin.addSuccessor(arc);
} }
else { else {
Arc d2o; final Arc d2o;
if (origin.getId() < destination.getId()) { if (origin.getId() < destination.getId()) {
arc = new ArcForward(origin, destination, length, roadInformation, points); arc = new ArcForward(origin, destination, length, roadInformation, points);
d2o = new ArcBackward(arc); d2o = new ArcBackward(arc);
@ -78,7 +78,7 @@ public final class Node implements Comparable<Node> {
* Create a new Node with the given ID corresponding to the given Point with an * Create a new Node with the given ID corresponding to the given Point with an
* empty list of successors. * empty list of successors.
* *
* @param id ID of the node. * @param id ID of the node.
* @param point Position of the node. * @param point Position of the node.
*/ */
public Node(int id, Point point) { public Node(int id, Point point) {

View File

@ -12,7 +12,7 @@ public class BadFormatException extends IOException {
/** /**
* *
*/ */
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1270945933549613579L;
/** /**
* *

View File

@ -7,15 +7,15 @@ import java.util.EnumMap;
import java.util.List; import java.util.List;
import org.insa.graphs.model.AccessRestrictions; import org.insa.graphs.model.AccessRestrictions;
import org.insa.graphs.model.AccessRestrictions.AccessMode;
import org.insa.graphs.model.AccessRestrictions.AccessRestriction;
import org.insa.graphs.model.Arc; import org.insa.graphs.model.Arc;
import org.insa.graphs.model.Graph; import org.insa.graphs.model.Graph;
import org.insa.graphs.model.GraphStatistics; import org.insa.graphs.model.GraphStatistics;
import org.insa.graphs.model.GraphStatistics.BoundingBox;
import org.insa.graphs.model.Node; import org.insa.graphs.model.Node;
import org.insa.graphs.model.Point; import org.insa.graphs.model.Point;
import org.insa.graphs.model.RoadInformation; import org.insa.graphs.model.RoadInformation;
import org.insa.graphs.model.AccessRestrictions.AccessMode;
import org.insa.graphs.model.AccessRestrictions.AccessRestriction;
import org.insa.graphs.model.GraphStatistics.BoundingBox;
import org.insa.graphs.model.RoadInformation.RoadType; import org.insa.graphs.model.RoadInformation.RoadType;
/** /**
@ -32,7 +32,7 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
protected static final int MAP_ID_FIELD_LENGTH = 32; protected static final int MAP_ID_FIELD_LENGTH = 32;
// List of observers // List of observers
protected List<GraphReaderObserver> observers = new ArrayList<>(); protected final List<GraphReaderObserver> observers = new ArrayList<>();
/** /**
* Parse the given long value into a new instance of AccessRestrictions. * Parse the given long value into a new instance of AccessRestrictions.
@ -63,7 +63,7 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
AccessMode.MOTORCAR, AccessMode.HEAVY_GOODS, null, AccessMode.PUBLIC_TRANSPORT }; AccessMode.MOTORCAR, AccessMode.HEAVY_GOODS, null, AccessMode.PUBLIC_TRANSPORT };
// fill maps... // fill maps...
EnumMap<AccessMode, AccessRestriction> restrictions = new EnumMap<>(AccessMode.class); final EnumMap<AccessMode, AccessRestriction> restrictions = new EnumMap<>(AccessMode.class);
long copyAccess = access; long copyAccess = access;
for (AccessMode mode: allModes) { for (AccessMode mode: allModes) {
if (mode == null) { if (mode == null) {
@ -155,11 +155,12 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
checkVersionOrThrow(dis.readInt()); checkVersionOrThrow(dis.readInt());
// Read map id. // Read map id.
String mapId; final String mapId;
String mapName = ""; final String mapName;
if (getCurrentVersion() < 6) { if (getCurrentVersion() < 6) {
mapId = "0x" + Integer.toHexString(dis.readInt()); mapId = "0x" + Integer.toHexString(dis.readInt());
mapName = ""; // No map name for older versions.
} }
else { else {
mapId = readFixedLengthString(MAP_ID_FIELD_LENGTH, "UTF-8"); mapId = readFixedLengthString(MAP_ID_FIELD_LENGTH, "UTF-8");
@ -169,15 +170,15 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
observers.forEach((observer) -> observer.notifyStartReading(mapId)); observers.forEach((observer) -> observer.notifyStartReading(mapId));
// Number of descriptors and nodes. // Number of descriptors and nodes.
int nbDesc = dis.readInt(); final int nbDesc = dis.readInt();
int nbNodes = dis.readInt(); final int nbNodes = dis.readInt();
// Number of successors for each nodes. // Number of successors for each nodes.
int[] nbSuccessors = new int[nbNodes]; final int[] nbSuccessors = new int[nbNodes];
int nbTotalSuccessors = 0; int nbTotalSuccessors = 0;
// Construct an array list with initial capacity of nbNodes. // Construct an array list with initial capacity of nbNodes.
ArrayList<Node> nodes = new ArrayList<Node>(nbNodes); final ArrayList<Node> nodes = new ArrayList<Node>(nbNodes);
// Read nodes. // Read nodes.
float minLongitude = Float.POSITIVE_INFINITY, minLatitude = Float.POSITIVE_INFINITY, float minLongitude = Float.POSITIVE_INFINITY, minLatitude = Float.POSITIVE_INFINITY,
@ -185,8 +186,8 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
observers.forEach((observer) -> observer.notifyStartReadingNodes(nbNodes)); observers.forEach((observer) -> observer.notifyStartReadingNodes(nbNodes));
for (int node = 0; node < nbNodes; ++node) { for (int node = 0; node < nbNodes; ++node) {
// Read longitude / latitude. // Read longitude / latitude.
float longitude = ((float) dis.readInt()) / 1E6f; final float longitude = ((float) dis.readInt()) / 1E6f;
float latitude = ((float) dis.readInt()) / 1E6f; final float latitude = ((float) dis.readInt()) / 1E6f;
// Update minimum / maximum. // Update minimum / maximum.
minLongitude = Math.min(longitude, minLongitude); minLongitude = Math.min(longitude, minLongitude);
@ -208,7 +209,7 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
checkByteOrThrow(255); checkByteOrThrow(255);
// Read descriptors. // Read descriptors.
RoadInformation[] descs = new RoadInformation[nbDesc]; final RoadInformation[] descs = new RoadInformation[nbDesc];
// Read // Read
observers.forEach((observer) -> observer.notifyStartReadingDescriptors(nbDesc)); observers.forEach((observer) -> observer.notifyStartReadingDescriptors(nbDesc));
@ -234,10 +235,10 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
for (int succ = 0; succ < nbSuccessors[node]; ++succ) { for (int succ = 0; succ < nbSuccessors[node]; ++succ) {
// Read target node number. // Read target node number.
int destNode = this.read24bits(); final int destNode = this.read24bits();
// Read information number. // Read information number.
int descrNum = this.read24bits(); final int descrNum = this.read24bits();
// Length of the arc. // Length of the arc.
float length; float length;
@ -253,10 +254,10 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
nodes.get(destNode).getPoint())); nodes.get(destNode).getPoint()));
// Number of segments. // Number of segments.
int nbSegments = dis.readUnsignedShort(); final int nbSegments = dis.readUnsignedShort();
// Chain of points corresponding to the segments. // Chain of points corresponding to the segments.
ArrayList<Point> points = new ArrayList<Point>(nbSegments + 2); final ArrayList<Point> points = new ArrayList<Point>(nbSegments + 2);
points.add(nodes.get(node).getPoint()); points.add(nodes.get(node).getPoint());
for (int seg = 0; seg < nbSegments; ++seg) { for (int seg = 0; seg < nbSegments; ++seg) {
@ -271,12 +272,12 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
points.add(nodes.get(destNode).getPoint()); points.add(nodes.get(destNode).getPoint());
RoadInformation info = descs[descrNum]; final RoadInformation info = descs[descrNum];
Node orig = nodes.get(node); final Node orig = nodes.get(node);
Node dest = nodes.get(destNode); final Node dest = nodes.get(destNode);
// Add successor to initial arc. // Add successor to initial arc.
Arc arc = Node.linkNodes(orig, dest, length, info, points); final Arc arc = Node.linkNodes(orig, dest, length, info, points);
if (info.isOneWay()) { if (info.isOneWay()) {
nbOneWayRoad++; nbOneWayRoad++;
} }
@ -289,8 +290,6 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader {
observers.forEach((observer) -> observer.notifyEndReading()); observers.forEach((observer) -> observer.notifyEndReading());
this.dis.close();
return new Graph(mapId, mapName, nodes, return new Graph(mapId, mapName, nodes,
new GraphStatistics( new GraphStatistics(
new BoundingBox(new Point(minLongitude, maxLatitude), new BoundingBox(new Point(minLongitude, maxLatitude),

View File

@ -35,27 +35,25 @@ public class BinaryPathReader extends BinaryReader implements PathReader {
checkVersionOrThrow(dis.readInt()); checkVersionOrThrow(dis.readInt());
// Read map ID and check against graph. // Read map ID and check against graph.
String mapId = readFixedLengthString(BinaryGraphReader.MAP_ID_FIELD_LENGTH, "UTF-8"); final String mapId = readFixedLengthString(BinaryGraphReader.MAP_ID_FIELD_LENGTH, "UTF-8");
if (!mapId.equals(graph.getMapId())) { if (!mapId.equals(graph.getMapId())) {
throw new MapMismatchException(mapId, graph.getMapId()); throw new MapMismatchException(mapId, graph.getMapId());
} }
// Number of nodes in the path (without first and last). // Number of nodes in the path (without first and last).
int nbNodes = dis.readInt(); final int nbNodes = dis.readInt();
// Skip (duplicate) first and last node // Skip (duplicate) first and last node
readNode(graph); readNode(graph);
readNode(graph); readNode(graph);
// Read intermediate nodes: // Read intermediate nodes:
ArrayList<Node> nodes = new ArrayList<Node>(); final ArrayList<Node> nodes = new ArrayList<Node>();
for (int i = 0; i < nbNodes; ++i) { for (int i = 0; i < nbNodes; ++i) {
nodes.add(readNode(graph)); nodes.add(readNode(graph));
} }
this.dis.close();
return Path.createFastestPathFromNodes(graph, nodes); return Path.createFastestPathFromNodes(graph, nodes);
} }
@ -66,7 +64,7 @@ public class BinaryPathReader extends BinaryReader implements PathReader {
* *
* @return The next node in the input stream. * @return The next node in the input stream.
* *
* @throws IOException if something occurs while reading the note. * @throws IOException if something occurs while reading the note.
* @throws IndexOutOfBoundsException if the node is not in the graph. * @throws IndexOutOfBoundsException if the node is not in the graph.
*/ */
protected Node readNode(Graph graph) throws IOException { protected Node readNode(Graph graph) throws IOException {

View File

@ -30,7 +30,7 @@ public class BinaryPathWriter extends BinaryWriter implements PathWriter {
dos.writeInt(BinaryPathReader.VERSION); dos.writeInt(BinaryPathReader.VERSION);
// Write map id. // Write map id.
byte[] bytes = Arrays.copyOf(path.getGraph().getMapId().getBytes("UTF-8"), final byte[] bytes = Arrays.copyOf(path.getGraph().getMapId().getBytes("UTF-8"),
BinaryGraphReader.MAP_ID_FIELD_LENGTH); BinaryGraphReader.MAP_ID_FIELD_LENGTH);
dos.write(bytes); dos.write(bytes);
@ -48,7 +48,6 @@ public class BinaryPathWriter extends BinaryWriter implements PathWriter {
} }
dos.flush(); dos.flush();
dos.close();
} }
} }

View File

@ -23,8 +23,8 @@ public abstract class BinaryReader implements AutoCloseable, Closeable {
* the given magic number and at least the given minimum version. * the given magic number and at least the given minimum version.
* *
* @param magicNumber Magic number of files to be read. * @param magicNumber Magic number of files to be read.
* @param minVersion Minimum version of files to be read. * @param minVersion Minimum version of files to be read.
* @param dis Input stream from which to read. * @param dis Input stream from which to read.
*/ */
protected BinaryReader(int magicNumber, int minVersion, DataInputStream dis) { protected BinaryReader(int magicNumber, int minVersion, DataInputStream dis) {
this.magicNumber = magicNumber; this.magicNumber = magicNumber;
@ -44,7 +44,7 @@ public abstract class BinaryReader implements AutoCloseable, Closeable {
* @param version Version to check. * @param version Version to check.
* *
* @throws BadVersionException if the given version is not greater than the * @throws BadVersionException if the given version is not greater than the
* minimum version. * minimum version.
*/ */
protected void checkVersionOrThrow(int version) throws BadVersionException { protected void checkVersionOrThrow(int version) throws BadVersionException {
if (version < this.minVersion) { if (version < this.minVersion) {
@ -80,7 +80,7 @@ public abstract class BinaryReader implements AutoCloseable, Closeable {
* *
* @param b Byte to check. * @param b Byte to check.
* *
* @throws IOException if an error occurs while reading the byte. * @throws IOException if an error occurs while reading the byte.
* @throws BadFormatException if the byte read is not the expected one. * @throws BadFormatException if the byte read is not the expected one.
*/ */
protected void checkByteOrThrow(int b) throws IOException { protected void checkByteOrThrow(int b) throws IOException {
@ -93,7 +93,7 @@ public abstract class BinaryReader implements AutoCloseable, Closeable {
* Read a byte array of fixed length from the input and convert it to a string * Read a byte array of fixed length from the input and convert it to a string
* using the given charset, removing any trailing '\0'. * using the given charset, removing any trailing '\0'.
* *
* @param length Number of bytes to read. * @param length Number of bytes to read.
* @param charset Charset to use to convert the bytes into a string. * @param charset Charset to use to convert the bytes into a string.
* *
* @return The convert string. * @return The convert string.

View File

@ -15,12 +15,12 @@ public class MapMismatchException extends IOException {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
// Actual and expected map ID. // Actual and expected map ID.
private String actualMapId, expectedMapId; private final String actualMapId, expectedMapId;
/** /**
* Create a new MapMismatchException with the given IDs. * Create a new MapMismatchException with the given IDs.
* *
* @param actualMapId Actual map ID found when reading the path. * @param actualMapId Actual map ID found when reading the path.
* @param expectedMapId Expected map ID from the graph. * @param expectedMapId Expected map ID from the graph.
*/ */
public MapMismatchException(String actualMapId, String expectedMapId) { public MapMismatchException(String actualMapId, String expectedMapId) {

24
pom.xml
View File

@ -18,18 +18,18 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> </properties>
<build> <build>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version> <version>3.1</version>
<configuration> <configuration>
<source>${jdk.version}</source> <source>${jdk.version}</source>
<target>${jdk.version}</target> <target>${jdk.version}</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
<dependency> <dependency>