-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add utility classes to format tables (CSV, ASCII or AMPL)
- Loading branch information
Showing
17 changed files
with
715 additions
and
427 deletions.
There are no files selected for viewing
451 changes: 207 additions & 244 deletions
451
ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/AmplNetworkWriter.java
Large diffs are not rendered by default.
Oops, something went wrong.
59 changes: 59 additions & 0 deletions
59
ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/util/AmplDatTableFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
121 changes: 0 additions & 121 deletions
121
ampl-export/src/main/java/eu/itesla_project/iidm/export/ampl/util/TableFormatter.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
commons/src/main/java/eu/itesla_project/commons/io/table/AbstractTableFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
commons/src/main/java/eu/itesla_project/commons/io/table/AsciiTableFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
commons/src/main/java/eu/itesla_project/commons/io/table/AsciiTableFormatterFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.