rinde.sim.serializers.AbstractGraphSerializer Maven / Gradle / Ivy
/**
*
*/
package rinde.sim.serializers;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.Writer;
import rinde.sim.core.graph.ConnectionData;
import rinde.sim.core.graph.Graph;
/**
* Common interface for graph serialization deserialization
* @author Bartosz Michalik
*
*/
public abstract class AbstractGraphSerializer {
abstract public Graph read(Reader reader) throws IOException;
abstract public void write(Graph extends E> graph, Writer writer)
throws IOException;
public Graph read(File file) throws FileNotFoundException, IOException {
return readReader(new FileReader(file));
}
public Graph read(InputStream stream) throws IOException {
return readReader(new InputStreamReader(stream));
}
Graph readReader(Reader r) throws IOException {
final Graph graph = read(r);
r.close();
return graph;
}
public Graph read(String filePath) throws FileNotFoundException,
IOException {
return read(new File(filePath));
}
public void write(Graph extends E> graph, File file) throws IOException {
final FileWriter writer = new FileWriter(file);
write(graph, writer);
writer.close();
}
public void write(Graph extends E> graph, String filePath)
throws IOException {
write(graph, new File(filePath));
}
}