Fix exceptions for readers, add path writer.

This commit is contained in:
Holt59 2018-02-24 15:11:15 +01:00
parent 164a9d4494
commit 6c3bc23984
7 changed files with 194 additions and 96 deletions

View File

@ -1,16 +1,18 @@
package org.insa.graph.io;
import java.io.IOException;
import org.insa.graph.Graph;
public interface AbstractGraphReader {
/**
* Read a graph an returns it.
*
* @return Graph.
* @throws Exception
*
*/
public Graph read() throws Exception;
/**
* Read a graph an returns it.
*
* @return Graph.
* @throws Exception
*
*/
public Graph read() throws IOException;
}

View File

@ -1,18 +1,20 @@
package org.insa.graph.io;
import java.io.IOException;
import org.insa.graph.Graph;
import org.insa.graph.Path;
public interface AbstractPathReader {
/**
* Read a path of the given graph and returns it.
*
* @param graph Graph of the path.
*
* @return A new path.
* @throws Exception
*/
public Path readPath(Graph graph) throws Exception;
/**
* Read a path of the given graph and returns it.
*
* @param graph Graph of the path.
*
* @return A new path.
* @throws Exception
*/
public Path readPath(Graph graph) throws IOException;
}

View 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;
}

View File

@ -11,55 +11,55 @@ import org.insa.graph.Path.CreationMode;
public class BinaryPathReader extends BinaryReader implements AbstractPathReader {
// Map version and magic number targeted for this reader.
private static final int VERSION = 1;
private static final int MAGIC_NUMBER = 0xdecafe;
// Map version and magic number targeted for this reader.
protected static final int VERSION = 1;
protected static final int MAGIC_NUMBER = 0xdecafe;
public BinaryPathReader(DataInputStream dis) {
super(MAGIC_NUMBER, VERSION, dis);
}
public BinaryPathReader(DataInputStream dis) {
super(MAGIC_NUMBER, VERSION, dis);
}
@Override
public Path readPath(Graph graph) throws Exception {
@Override
public Path readPath(Graph graph) throws IOException {
// Read and check magic number and version.
checkMagicNumberOrThrow(dis.readInt());
checkVersionOrThrow(dis.readInt());
// Read and check magic number and version.
checkMagicNumberOrThrow(dis.readInt());
checkVersionOrThrow(dis.readInt());
// Read map ID and check against graph.
int mapId = dis.readInt();
// Read map ID and check against graph.
int mapId = dis.readInt();
if (mapId != graph.getMapId()) {
throw new MapMismatchException(mapId, graph.getMapId());
}
if (mapId != graph.getMapId()) {
throw new MapMismatchException(mapId, graph.getMapId());
}
// Number of nodes in the path (without first and last).
int nbNodes = dis.readInt();
// Number of nodes in the path (without first and last).
int nbNodes = dis.readInt();
// Skip (duplicate) first and last node
readNode(graph);
readNode(graph);
// 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));
}
// 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);
}
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();
/**
* 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());
}
return graph.getNodes().get(read24bits());
}
}

View 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();
}
}

View 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);
}
}

View File

@ -1,36 +1,38 @@
package org.insa.graph.io;
public class MapMismatchException extends Exception {
import java.io.IOException;
/**
*
*/
private static final long serialVersionUID = 3076730078387819138L;
// Actual and expected magic numbers.
private int actualMapId, expectedMapId;
public class MapMismatchException extends IOException {
/**
*
* @param actualVersion
* @param expectedVersion
*/
public MapMismatchException(int actualMapId, int expectedMapId) {
super();
this.actualMapId = actualMapId;
this.expectedMapId = expectedMapId;
}
/**
*
*/
private static final long serialVersionUID = 3076730078387819138L;
// Actual and expected magic numbers.
private int actualMapId, expectedMapId;
/**
* @return
*/
public int getActualMapId() {
return actualMapId;
}
/**
*
* @param actualVersion
* @param expectedVersion
*/
public MapMismatchException(int actualMapId, int expectedMapId) {
super();
this.actualMapId = actualMapId;
this.expectedMapId = expectedMapId;
}
/**
* @return
*/
public int getExpectedMapId() {
return expectedMapId;
}
/**
* @return
*/
public int getActualMapId() {
return actualMapId;
}
/**
* @return
*/
public int getExpectedMapId() {
return expectedMapId;
}
}