From 2f936d44ecb0d455f4a92fe7e96719c8c9e7cb9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mika=C3=ABl=20Capelle?= Date: Fri, 27 Mar 2020 13:10:36 +0100 Subject: [PATCH] [model] Clean Java classes. --- .../insa/graphs/model/AccessRestrictions.java | 14 ++---- .../org/insa/graphs/model/ArcBackward.java | 3 +- .../java/org/insa/graphs/model/Graph.java | 14 +++--- .../main/java/org/insa/graphs/model/Node.java | 14 +++--- .../graphs/model/io/BadFormatException.java | 2 +- .../graphs/model/io/BinaryGraphReader.java | 47 +++++++++---------- .../graphs/model/io/BinaryPathReader.java | 10 ++-- .../graphs/model/io/BinaryPathWriter.java | 3 +- .../insa/graphs/model/io/BinaryReader.java | 10 ++-- .../graphs/model/io/MapMismatchException.java | 4 +- pom.xml | 28 +++++------ 11 files changed, 71 insertions(+), 78 deletions(-) diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/AccessRestrictions.java b/be-graphes-model/src/main/java/org/insa/graphs/model/AccessRestrictions.java index 78d8689..eac0077 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/AccessRestrictions.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/AccessRestrictions.java @@ -170,7 +170,7 @@ public class AccessRestrictions { * Create a new AccessRestrictions instances with the given restrictions. * * @param restrictions Map of restrictions for this instance of - * AccessRestrictions. + * AccessRestrictions. */ public AccessRestrictions(EnumMap 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 * 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. * * @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 * 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. * * @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 * 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. * * @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 modes, EnumSet restrictions) { - boolean allowed = true; - for (AccessMode mode: modes) { - allowed = allowed && isAllowedForAny(mode, restrictions); - } - return allowed; + return modes.stream().allMatch(mode -> isAllowedForAny(mode, restrictions)); } } diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/ArcBackward.java b/be-graphes-model/src/main/java/org/insa/graphs/model/ArcBackward.java index 051fc34..4203fb5 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/ArcBackward.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/ArcBackward.java @@ -47,7 +47,8 @@ class ArcBackward extends Arc { @Override public List getPoints() { - List pts = new ArrayList<>(this.originalArc.getPoints()); + // There does not seem to be a cleaner way to do this using Java 8 streams. + final List pts = new ArrayList<>(this.originalArc.getPoints()); Collections.reverse(pts); return pts; } diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/Graph.java b/be-graphes-model/src/main/java/org/insa/graphs/model/Graph.java index 42e3ecc..5a0cb68 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/Graph.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/Graph.java @@ -32,9 +32,9 @@ public final class Graph { /** * Create a new graph with the given ID, name, nodes and information. * - * @param mapId ID 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 mapId ID 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 graphStatistics Information for this graph. */ public Graph(String mapId, String mapName, List nodes, GraphStatistics graphStatistics) { @@ -98,20 +98,20 @@ public final class Graph { * @return Transpose graph of this graph. */ public Graph transpose() { - ArrayList trNodes = new ArrayList<>(nodes.size()); + final ArrayList trNodes = new ArrayList<>(nodes.size()); for (Node node: nodes) { trNodes.add(new Node(node.getId(), node.getPoint())); } for (Node node: nodes) { - Node orig = trNodes.get(node.getId()); + final Node orig = trNodes.get(node.getId()); for (Arc arc: node.getSuccessors()) { 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(), arc.getRoadInformation(), arc.getPoints()))); } 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.getRoadInformation(), arc.getPoints()); dest.addSuccessor(new ArcBackward(newArc)); diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/Node.java b/be-graphes-model/src/main/java/org/insa/graphs/model/Node.java index 37f20b0..778b48a 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/Node.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/Node.java @@ -33,23 +33,23 @@ public final class Node implements Comparable { * corresponding backward arc is created and add to destination. *

* - * @param origin Origin of the arc. - * @param destination Destination of the arc. - * @param length Length of the arc. + * @param origin Origin of the arc. + * @param destination Destination of the arc. + * @param length Length of 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). */ public static Arc linkNodes(Node origin, Node destination, float length, RoadInformation roadInformation, ArrayList points) { - Arc arc = null; + final Arc arc; if (roadInformation.isOneWay()) { arc = new ArcForward(origin, destination, length, roadInformation, points); origin.addSuccessor(arc); } else { - Arc d2o; + final Arc d2o; if (origin.getId() < destination.getId()) { arc = new ArcForward(origin, destination, length, roadInformation, points); d2o = new ArcBackward(arc); @@ -78,7 +78,7 @@ public final class Node implements Comparable { * Create a new Node with the given ID corresponding to the given Point with an * empty list of successors. * - * @param id ID of the node. + * @param id ID of the node. * @param point Position of the node. */ public Node(int id, Point point) { diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/io/BadFormatException.java b/be-graphes-model/src/main/java/org/insa/graphs/model/io/BadFormatException.java index 184debe..78439f0 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/io/BadFormatException.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/io/BadFormatException.java @@ -12,7 +12,7 @@ public class BadFormatException extends IOException { /** * */ - private static final long serialVersionUID = 1L; + private static final long serialVersionUID = 1270945933549613579L; /** * diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryGraphReader.java b/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryGraphReader.java index 720f54f..84833e8 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryGraphReader.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryGraphReader.java @@ -7,15 +7,15 @@ import java.util.EnumMap; import java.util.List; 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.Graph; import org.insa.graphs.model.GraphStatistics; +import org.insa.graphs.model.GraphStatistics.BoundingBox; import org.insa.graphs.model.Node; import org.insa.graphs.model.Point; 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; /** @@ -32,7 +32,7 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader { protected static final int MAP_ID_FIELD_LENGTH = 32; // List of observers - protected List observers = new ArrayList<>(); + protected final List observers = new ArrayList<>(); /** * 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 }; // fill maps... - EnumMap restrictions = new EnumMap<>(AccessMode.class); + final EnumMap restrictions = new EnumMap<>(AccessMode.class); long copyAccess = access; for (AccessMode mode: allModes) { if (mode == null) { @@ -155,11 +155,12 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader { checkVersionOrThrow(dis.readInt()); // Read map id. - String mapId; - String mapName = ""; + final String mapId; + final String mapName; if (getCurrentVersion() < 6) { mapId = "0x" + Integer.toHexString(dis.readInt()); + mapName = ""; // No map name for older versions. } else { 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)); // Number of descriptors and nodes. - int nbDesc = dis.readInt(); - int nbNodes = dis.readInt(); + final int nbDesc = dis.readInt(); + final int nbNodes = dis.readInt(); // Number of successors for each nodes. - int[] nbSuccessors = new int[nbNodes]; + final int[] nbSuccessors = new int[nbNodes]; int nbTotalSuccessors = 0; // Construct an array list with initial capacity of nbNodes. - ArrayList nodes = new ArrayList(nbNodes); + final ArrayList nodes = new ArrayList(nbNodes); // Read nodes. 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)); for (int node = 0; node < nbNodes; ++node) { // Read longitude / latitude. - float longitude = ((float) dis.readInt()) / 1E6f; - float latitude = ((float) dis.readInt()) / 1E6f; + final float longitude = ((float) dis.readInt()) / 1E6f; + final float latitude = ((float) dis.readInt()) / 1E6f; // Update minimum / maximum. minLongitude = Math.min(longitude, minLongitude); @@ -208,7 +209,7 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader { checkByteOrThrow(255); // Read descriptors. - RoadInformation[] descs = new RoadInformation[nbDesc]; + final RoadInformation[] descs = new RoadInformation[nbDesc]; // Read 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) { // Read target node number. - int destNode = this.read24bits(); + final int destNode = this.read24bits(); // Read information number. - int descrNum = this.read24bits(); + final int descrNum = this.read24bits(); // Length of the arc. float length; @@ -253,10 +254,10 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader { nodes.get(destNode).getPoint())); // Number of segments. - int nbSegments = dis.readUnsignedShort(); + final int nbSegments = dis.readUnsignedShort(); // Chain of points corresponding to the segments. - ArrayList points = new ArrayList(nbSegments + 2); + final ArrayList points = new ArrayList(nbSegments + 2); points.add(nodes.get(node).getPoint()); for (int seg = 0; seg < nbSegments; ++seg) { @@ -271,12 +272,12 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader { points.add(nodes.get(destNode).getPoint()); - RoadInformation info = descs[descrNum]; - Node orig = nodes.get(node); - Node dest = nodes.get(destNode); + final RoadInformation info = descs[descrNum]; + final Node orig = nodes.get(node); + final Node dest = nodes.get(destNode); // 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()) { nbOneWayRoad++; } @@ -289,8 +290,6 @@ public class BinaryGraphReader extends BinaryReader implements GraphReader { observers.forEach((observer) -> observer.notifyEndReading()); - this.dis.close(); - return new Graph(mapId, mapName, nodes, new GraphStatistics( new BoundingBox(new Point(minLongitude, maxLatitude), diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryPathReader.java b/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryPathReader.java index f3351bd..4aca938 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryPathReader.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryPathReader.java @@ -35,27 +35,25 @@ public class BinaryPathReader extends BinaryReader implements PathReader { checkVersionOrThrow(dis.readInt()); // 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())) { throw new MapMismatchException(mapId, graph.getMapId()); } // 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 readNode(graph); readNode(graph); // Read intermediate nodes: - ArrayList nodes = new ArrayList(); + final ArrayList nodes = new ArrayList(); for (int i = 0; i < nbNodes; ++i) { nodes.add(readNode(graph)); } - this.dis.close(); - return Path.createFastestPathFromNodes(graph, nodes); } @@ -66,7 +64,7 @@ public class BinaryPathReader extends BinaryReader implements PathReader { * * @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. */ protected Node readNode(Graph graph) throws IOException { diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryPathWriter.java b/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryPathWriter.java index 1eb778c..52b00df 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryPathWriter.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryPathWriter.java @@ -30,7 +30,7 @@ public class BinaryPathWriter extends BinaryWriter implements PathWriter { dos.writeInt(BinaryPathReader.VERSION); // 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); dos.write(bytes); @@ -48,7 +48,6 @@ public class BinaryPathWriter extends BinaryWriter implements PathWriter { } dos.flush(); - dos.close(); } } diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryReader.java b/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryReader.java index b654bad..159a59b 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryReader.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/io/BinaryReader.java @@ -23,8 +23,8 @@ public abstract class BinaryReader implements AutoCloseable, Closeable { * the given magic number and at least the given minimum version. * * @param magicNumber Magic number of files to be read. - * @param minVersion Minimum version of files to be read. - * @param dis Input stream from which to read. + * @param minVersion Minimum version of files to be read. + * @param dis Input stream from which to read. */ protected BinaryReader(int magicNumber, int minVersion, DataInputStream dis) { this.magicNumber = magicNumber; @@ -44,7 +44,7 @@ public abstract class BinaryReader implements AutoCloseable, Closeable { * @param version Version to check. * * @throws BadVersionException if the given version is not greater than the - * minimum version. + * minimum version. */ protected void checkVersionOrThrow(int version) throws BadVersionException { if (version < this.minVersion) { @@ -80,7 +80,7 @@ public abstract class BinaryReader implements AutoCloseable, Closeable { * * @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. */ 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 * 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. * * @return The convert string. diff --git a/be-graphes-model/src/main/java/org/insa/graphs/model/io/MapMismatchException.java b/be-graphes-model/src/main/java/org/insa/graphs/model/io/MapMismatchException.java index fb2de3b..ea80056 100644 --- a/be-graphes-model/src/main/java/org/insa/graphs/model/io/MapMismatchException.java +++ b/be-graphes-model/src/main/java/org/insa/graphs/model/io/MapMismatchException.java @@ -15,12 +15,12 @@ public class MapMismatchException extends IOException { private static final long serialVersionUID = 1L; // Actual and expected map ID. - private String actualMapId, expectedMapId; + private final String actualMapId, expectedMapId; /** * 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. */ public MapMismatchException(String actualMapId, String expectedMapId) { diff --git a/pom.xml b/pom.xml index c275f19..260a625 100644 --- a/pom.xml +++ b/pom.xml @@ -17,19 +17,19 @@ ${jdk.version} UTF-8 - - - - - maven-compiler-plugin - 3.1 - - ${jdk.version} - ${jdk.version} - - - - + + + + + maven-compiler-plugin + 3.1 + + ${jdk.version} + ${jdk.version} + + + + @@ -39,7 +39,7 @@ test - + be-graphes-model be-graphes-algos