Fix exceptions for readers, add path writer.
This commit is contained in:
parent
164a9d4494
commit
6c3bc23984
@ -1,16 +1,18 @@
|
|||||||
package org.insa.graph.io;
|
package org.insa.graph.io;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.insa.graph.Graph;
|
import org.insa.graph.Graph;
|
||||||
|
|
||||||
public interface AbstractGraphReader {
|
public interface AbstractGraphReader {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a graph an returns it.
|
* Read a graph an returns it.
|
||||||
*
|
*
|
||||||
* @return Graph.
|
* @return Graph.
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public Graph read() throws Exception;
|
public Graph read() throws IOException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,20 @@
|
|||||||
package org.insa.graph.io;
|
package org.insa.graph.io;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
import org.insa.graph.Graph;
|
import org.insa.graph.Graph;
|
||||||
import org.insa.graph.Path;
|
import org.insa.graph.Path;
|
||||||
|
|
||||||
public interface AbstractPathReader {
|
public interface AbstractPathReader {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Read a path of the given graph and returns it.
|
* Read a path of the given graph and returns it.
|
||||||
*
|
*
|
||||||
* @param graph Graph of the path.
|
* @param graph Graph of the path.
|
||||||
*
|
*
|
||||||
* @return A new path.
|
* @return A new path.
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public Path readPath(Graph graph) throws Exception;
|
public Path readPath(Graph graph) throws IOException;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
18
src/main/org/insa/graph/io/AbstractPathWriter.java
Normal file
18
src/main/org/insa/graph/io/AbstractPathWriter.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package org.insa.graph.io;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.insa.graph.Path;
|
||||||
|
|
||||||
|
public interface AbstractPathWriter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a path.
|
||||||
|
*
|
||||||
|
* @param path Path to write.
|
||||||
|
*
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public void writePath(Path path) throws IOException;
|
||||||
|
|
||||||
|
}
|
@ -11,55 +11,55 @@ import org.insa.graph.Path.CreationMode;
|
|||||||
|
|
||||||
public class BinaryPathReader extends BinaryReader implements AbstractPathReader {
|
public class BinaryPathReader extends BinaryReader implements AbstractPathReader {
|
||||||
|
|
||||||
// Map version and magic number targeted for this reader.
|
// Map version and magic number targeted for this reader.
|
||||||
private static final int VERSION = 1;
|
protected static final int VERSION = 1;
|
||||||
private static final int MAGIC_NUMBER = 0xdecafe;
|
protected static final int MAGIC_NUMBER = 0xdecafe;
|
||||||
|
|
||||||
public BinaryPathReader(DataInputStream dis) {
|
public BinaryPathReader(DataInputStream dis) {
|
||||||
super(MAGIC_NUMBER, VERSION, dis);
|
super(MAGIC_NUMBER, VERSION, dis);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Path readPath(Graph graph) throws Exception {
|
public Path readPath(Graph graph) throws IOException {
|
||||||
|
|
||||||
// Read and check magic number and version.
|
|
||||||
checkMagicNumberOrThrow(dis.readInt());
|
|
||||||
checkVersionOrThrow(dis.readInt());
|
|
||||||
|
|
||||||
// Read map ID and check against graph.
|
|
||||||
int mapId = dis.readInt();
|
|
||||||
|
|
||||||
if (mapId != graph.getMapId()) {
|
|
||||||
throw new MapMismatchException(mapId, graph.getMapId());
|
|
||||||
}
|
|
||||||
|
|
||||||
// Number of nodes in the path (without first and last).
|
|
||||||
int nbNodes = dis.readInt();
|
|
||||||
|
|
||||||
// Skip (duplicate) first and last node
|
|
||||||
readNode(graph);
|
|
||||||
readNode(graph);
|
|
||||||
|
|
||||||
// Read intermediate nodes:
|
|
||||||
ArrayList<Node> nodes = new ArrayList<Node>();
|
|
||||||
for (int i = 0; i < nbNodes; ++i) {
|
|
||||||
nodes.add(readNode(graph));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new Path(graph, nodes, CreationMode.SHORTEST_TIME);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
// Read and check magic number and version.
|
||||||
* Read a node from the stream and returns id.
|
checkMagicNumberOrThrow(dis.readInt());
|
||||||
*
|
checkVersionOrThrow(dis.readInt());
|
||||||
* @return
|
|
||||||
* @throws IOException
|
// Read map ID and check against graph.
|
||||||
*/
|
int mapId = dis.readInt();
|
||||||
protected Node readNode(Graph graph) throws IOException {
|
|
||||||
// Discard zone.
|
if (mapId != graph.getMapId()) {
|
||||||
dis.readUnsignedByte();
|
throw new MapMismatchException(mapId, graph.getMapId());
|
||||||
|
}
|
||||||
return graph.getNodes().get(read24bits());
|
|
||||||
}
|
// Number of nodes in the path (without first and last).
|
||||||
|
int nbNodes = dis.readInt();
|
||||||
|
|
||||||
|
// Skip (duplicate) first and last node
|
||||||
|
readNode(graph);
|
||||||
|
readNode(graph);
|
||||||
|
|
||||||
|
// Read intermediate nodes:
|
||||||
|
ArrayList<Node> nodes = new ArrayList<Node>();
|
||||||
|
for (int i = 0; i < nbNodes; ++i) {
|
||||||
|
nodes.add(readNode(graph));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new Path(graph, nodes, CreationMode.SHORTEST_TIME);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read a node from the stream and returns id.
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
protected Node readNode(Graph graph) throws IOException {
|
||||||
|
// Discard zone.
|
||||||
|
dis.readUnsignedByte();
|
||||||
|
|
||||||
|
return graph.getNodes().get(read24bits());
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
44
src/main/org/insa/graph/io/BinaryPathWriter.java
Normal file
44
src/main/org/insa/graph/io/BinaryPathWriter.java
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
package org.insa.graph.io;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.insa.graph.Arc;
|
||||||
|
import org.insa.graph.Path;
|
||||||
|
|
||||||
|
public class BinaryPathWriter extends BinaryWriter implements AbstractPathWriter {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param dos
|
||||||
|
*/
|
||||||
|
protected BinaryPathWriter(DataOutputStream dos) {
|
||||||
|
super(dos);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void writePath(Path path) throws IOException {
|
||||||
|
|
||||||
|
// Write magic number and version.
|
||||||
|
dos.writeInt(BinaryPathReader.MAGIC_NUMBER);
|
||||||
|
dos.writeInt(BinaryPathReader.VERSION);
|
||||||
|
|
||||||
|
// Write map id.
|
||||||
|
dos.writeInt(path.getGraph().getMapId());
|
||||||
|
|
||||||
|
// Write number of racs
|
||||||
|
dos.writeInt(path.getArcs().size() + 1);
|
||||||
|
|
||||||
|
// Write origin / destination.
|
||||||
|
dos.writeInt(path.getOrigin().getId());
|
||||||
|
dos.writeInt(path.getDestination().getId());
|
||||||
|
|
||||||
|
// Write nodes.
|
||||||
|
dos.writeInt(path.getOrigin().getId());
|
||||||
|
for (Arc arc: path.getArcs()) {
|
||||||
|
dos.writeInt(arc.getDestination().getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
dos.flush();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
30
src/main/org/insa/graph/io/BinaryWriter.java
Normal file
30
src/main/org/insa/graph/io/BinaryWriter.java
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
package org.insa.graph.io;
|
||||||
|
|
||||||
|
import java.io.DataOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
public abstract class BinaryWriter {
|
||||||
|
|
||||||
|
// Output stream.
|
||||||
|
protected DataOutputStream dos;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param dos
|
||||||
|
*/
|
||||||
|
protected BinaryWriter(DataOutputStream dos) {
|
||||||
|
this.dos = dos;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Write a 24-bits integer in BigEndian to the output stream.
|
||||||
|
*
|
||||||
|
* @param value
|
||||||
|
*
|
||||||
|
* @throws IOException
|
||||||
|
*/
|
||||||
|
protected void write24bits(int value) throws IOException {
|
||||||
|
dos.writeShort(value >> 8);
|
||||||
|
dos.writeByte(value & 0xff);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,36 +1,38 @@
|
|||||||
package org.insa.graph.io;
|
package org.insa.graph.io;
|
||||||
|
|
||||||
public class MapMismatchException extends Exception {
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
public class MapMismatchException extends IOException {
|
||||||
*
|
|
||||||
*/
|
|
||||||
private static final long serialVersionUID = 3076730078387819138L;
|
|
||||||
// Actual and expected magic numbers.
|
|
||||||
private int actualMapId, expectedMapId;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @param actualVersion
|
|
||||||
* @param expectedVersion
|
|
||||||
*/
|
|
||||||
public MapMismatchException(int actualMapId, int expectedMapId) {
|
|
||||||
super();
|
|
||||||
this.actualMapId = actualMapId;
|
|
||||||
this.expectedMapId = expectedMapId;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return
|
*
|
||||||
*/
|
*/
|
||||||
public int getActualMapId() {
|
private static final long serialVersionUID = 3076730078387819138L;
|
||||||
return actualMapId;
|
// Actual and expected magic numbers.
|
||||||
}
|
private int actualMapId, expectedMapId;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return
|
*
|
||||||
*/
|
* @param actualVersion
|
||||||
public int getExpectedMapId() {
|
* @param expectedVersion
|
||||||
return expectedMapId;
|
*/
|
||||||
}
|
public MapMismatchException(int actualMapId, int expectedMapId) {
|
||||||
|
super();
|
||||||
|
this.actualMapId = actualMapId;
|
||||||
|
this.expectedMapId = expectedMapId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int getActualMapId() {
|
||||||
|
return actualMapId;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public int getExpectedMapId() {
|
||||||
|
return expectedMapId;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user