[model] Clean Java classes.
This commit is contained in:
parent
c9ff67b10e
commit
2f936d44ec
@ -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;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
@ -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));
|
||||||
|
@ -43,13 +43,13 @@ public final class Node implements Comparable<Node> {
|
|||||||
*/
|
*/
|
||||||
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);
|
||||||
|
@ -12,7 +12,7 @@ public class BadFormatException extends IOException {
|
|||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
private static final long serialVersionUID = 1L;
|
private static final long serialVersionUID = 1270945933549613579L;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -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),
|
||||||
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ 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.
|
||||||
|
Loading…
Reference in New Issue
Block a user