All Downloads are FREE. Search and download functionalities are using the official Maven repository.

rinde.sim.serializers.AbstractGraphSerializer Maven / Gradle / Ivy

There is a newer version: 4.4.6
Show newest version
/**
 * 
 */
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 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 graph, File file) throws IOException {
    final FileWriter writer = new FileWriter(file);
    write(graph, writer);
    writer.close();
  }

  public void write(Graph graph, String filePath)
      throws IOException {
    write(graph, new File(filePath));
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy