Skip to content

Commit

Permalink
Add utility classes to format tables (CSV, ASCII or AMPL)
Browse files Browse the repository at this point in the history
  • Loading branch information
geofjamg committed Oct 17, 2016
1 parent 186c697 commit 71e065f
Show file tree
Hide file tree
Showing 17 changed files with 715 additions and 427 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.iidm.export.ampl.util;

import eu.itesla_project.commons.io.table.Column;
import eu.itesla_project.commons.io.table.CsvTableFormatter;
import eu.itesla_project.commons.io.table.TableFormatter;

import java.io.IOException;
import java.io.Writer;
import java.util.Locale;

/**
* Specialization of CSV table formatter for AMPL .dat file generation.
* 3 differences:
* - separator is white space
* - comments are allowed and start with # (header is also a comment)
* - strings are quoted because of the white space separator
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class AmplDatTableFormatter extends CsvTableFormatter {

public AmplDatTableFormatter(Writer writer, String title, float invalidFloatValue, boolean header, Locale locale, Column... columns) {
super(writer, title, ' ', Float.toString(invalidFloatValue), header, locale, columns);
}

protected void writeHeader() throws IOException {
writer.append("#").append(title).append(System.lineSeparator())
.append("#");
for (int i = 0; i < columns.length; i++) {
writer.append("\"").append(columns[i].getName()).append("\"");
if (i < columns.length-1) {
writer.append(separator);
}
}
writer.append(System.lineSeparator());
}

@Override
public TableFormatter writeCell(String s) throws IOException {
return write("\"" + s + "\"");
}

@Override
public AmplDatTableFormatter writeComment(String comment) throws IOException {
if (column != 0) {
throw new IllegalStateException("Row has to be completed to start a comment");
}
writer.write("#");
writer.write(comment);
writer.write(System.lineSeparator());
return this;
}

}

This file was deleted.

9 changes: 9 additions & 0 deletions commons/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,15 @@
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service</artifactId>
</dependency>
<dependency>
<groupId>org.ow2.sirocco</groupId>
<artifactId>sirocco-text-table-formatter</artifactId>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>log4j-over-slf4j</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.commons.io.table;

import java.io.IOException;
import java.util.Locale;
import java.util.Objects;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public abstract class AbstractTableFormatter implements TableFormatter {

protected final Locale locale;

protected final String invalidString;

protected AbstractTableFormatter(Locale locale, String invalidString) {
this.locale = Objects.requireNonNull(locale);
this.invalidString = Objects.requireNonNull(invalidString);
}

protected abstract TableFormatter write(String value) throws IOException;

@Override
public TableFormatter writeCell(String s) throws IOException {
return write(s);
}

@Override
public TableFormatter writeCell(char c) throws IOException {
return write(Character.toString(c));
}

@Override
public TableFormatter writeCell(int i) throws IOException {
return write(Integer.toString(i));
}

@Override
public TableFormatter writeCell(float f) throws IOException {
return write(Float.isNaN(f) ? invalidString : String.format(locale, "%g", f));
}

@Override
public TableFormatter writeCell(double d) throws IOException {
return write(Double.isNaN(d) ? invalidString : String.format(locale, "%g", d));
}

@Override
public TableFormatter writeCell(boolean b) throws IOException {
return write(Boolean.toString(b));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.commons.io.table;

import org.nocrala.tools.texttablefmt.BorderStyle;
import org.nocrala.tools.texttablefmt.Table;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.Locale;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class AsciiTableFormatter extends AbstractTableFormatter {

private final String title;

private final Writer writer;

private final Table table ;

public AsciiTableFormatter(String title, Locale locale, Column... columns) {
this(new OutputStreamWriter(System.out), title, locale, columns);
}

public AsciiTableFormatter(Writer writer, String title, Locale locale, Column... columns) {
super(locale, "inv");
this.writer = writer;
this.title = title;
this.table = new Table(columns.length, BorderStyle.CLASSIC_WIDE);
for (Column column : columns) {
table.addCell(column.getName());
}
}

@Override
public AsciiTableFormatter writeComment(String comment) throws IOException {
// not supported
return this;
}

@Override
protected TableFormatter write(String value) throws IOException {
table.addCell(value);
return this;
}

@Override
public void close() throws IOException {
writer.write(title + ":" + System.lineSeparator());
writer.write(table.render() + System.lineSeparator());
writer.close();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) 2016, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.commons.io.table;

import java.io.Writer;
import java.util.Locale;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
public class AsciiTableFormatterFactory implements TableFormatterFactory {

@Override
public TableFormatter create(Writer writer, String title, Locale locale, Column... columns) {
return new AsciiTableFormatter(writer, title, locale, columns);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package eu.itesla_project.iidm.export.ampl.util;
package eu.itesla_project.commons.io.table;

/**
*
Expand Down

0 comments on commit 71e065f

Please sign in to comment.