Skip to content

Commit

Permalink
cr: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
radeusgd committed Jun 23, 2022
1 parent 04196ea commit 2d4537f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
12 changes: 5 additions & 7 deletions distribution/lib/Standard/Table/0.0.0-dev/src/Data/Table.enso
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Standard.Table.Io.Spreadsheet_Write_Mode
import Standard.Table.Io.File_Format
import Standard.Base.System.File
import Standard.Base.System.File.Existing_File_Behavior
import Standard.Base.Error.Common as Errors
import Standard.Table.Internal.Table_Helpers
import Standard.Table.Internal.Aggregate_Column_Helper
import Standard.Table.Internal.Parse_Values_Helper
Expand All @@ -34,8 +35,6 @@ polyglot java import org.enso.table.data.table.Table as Java_Table
polyglot java import org.enso.table.data.table.Column as Java_Column
polyglot java import org.enso.table.format.xlsx.Writer as Spreadsheet_Writer
polyglot java import org.enso.table.operations.OrderBuilder
polyglot java import java.io.StringReader
polyglot java import java.io.StringWriter

## Creates a new table from a vector of `[name, items]` pairs.

Expand Down Expand Up @@ -1284,10 +1283,9 @@ print_table header rows indices_count format_term =
([" " + header_line, divider] + row_lines).join '\n'

Table.from (that : Text) (format:File_Format.Delimited|File_Format.Fixed_Width = File_Format.Delimited '\t') (on_problems:Problem_Behavior=Report_Warning) =
java_reader = StringReader.new that
Delimited_Reader.read_from_reader format java_reader on_problems
if format.is_a File_Format.Delimited then Delimited_Reader.read_text that format on_problems else
Errors.unimplemented "Table.from for fixed-width files is not yet implemented."

Text.from (that : Table) (format:File_Format.Delimited|File_Format.Fixed_Width = File_Format.Delimited '\t') =
java_writer = StringWriter.new
Delimited_Writer.write_to_writer that format java_writer
java_writer.toString
if format.is_a File_Format.Delimited then Delimited_Writer.write_text that format else
Errors.unimplemented "Text.from for fixed-width files is not yet implemented."
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ polyglot java import com.univocity.parsers.common.TextParsingException
polyglot java import org.enso.base.Encoding_Utils
polyglot java import java.io.InputStream
polyglot java import java.io.Reader
polyglot java import java.io.StringReader

polyglot java import org.enso.table.parsing.IdentityParser
polyglot java import org.enso.table.parsing.TypeInferringParser
Expand All @@ -46,6 +47,11 @@ read_file format file on_problems =
stream.with_java_stream java_stream->
here.read_stream format java_stream on_problems related_file=file

read_text : Text -> Delimited -> Problem_Behavior -> Table
read_text text format on_problems =
java_reader = StringReader.new text
Delimited_Reader.read_from_reader format java_reader on_problems

## PRIVATE
Reads an input stream according to the provided format.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ polyglot java import org.enso.table.write.WriteQuoteBehavior
polyglot java import java.io.PrintWriter
polyglot java import java.io.IOException
polyglot java import org.enso.table.formatting.TextFormatter
polyglot java import java.io.StringWriter

## Writes a delimited file according to the provided format.

Expand All @@ -28,16 +29,24 @@ polyglot java import org.enso.table.formatting.TextFormatter
operation. By default, a warning is issued, but the operation proceeds.
If set to `Report_Error`, the operation fails with a dataflow error.
If set to `Ignore`, the operation proceeds without errors or warnings.
write_file : Table -> Delimited -> File -> Existing_File_Behavior -> Problem_Behavior -> Any
write_file : Table -> File_Format.Delimited -> File -> Existing_File_Behavior -> Problem_Behavior -> Any
write_file table format file on_existing_file on_problems =
case on_existing_file of
Existing_File_Behavior.Append ->
Errors.unimplemented "Appending to an existing Delimited file is not implemented yet."
Errors.unimplemented "Appending to an existing File_Format.Delimited file is not implemented yet."
_ ->
on_existing_file.write file stream->
stream.with_java_stream java_stream->
here.write_to_stream table format java_stream on_problems related_file=file

## PRIVATE
Returns a Text value representing the table in the delimited format.
write_text : Table -> File_Format.Delimited -> Text
write_text table format =
java_writer = StringWriter.new
here.write_to_writer table format java_writer
java_writer.toString

## PRIVATE
Writes to an output stream according to the provided format.

Expand All @@ -51,7 +60,7 @@ write_file table format file on_existing_file on_problems =
If set to `Ignore`, the operation proceeds without errors or warnings.
- related_file: The file related to the provided `java_stream`, if available,
or `Nothing`. It is used for more detailed error reporting.
write_to_stream : Table -> Delimited -> OutputStream -> Problem_Behavior -> File | Nothing -> Any
write_to_stream : Table -> File_Format.Delimited -> OutputStream -> Problem_Behavior -> File | Nothing -> Any
write_to_stream table format java_stream on_problems related_file=Nothing =
handle_io_exception ~action = Panic.catch IOException action caught_panic->
Error.throw (File.wrap_io_exception related_file caught_panic.payload.cause)
Expand All @@ -74,7 +83,7 @@ write_to_stream table format java_stream on_problems related_file=Nothing =
- table: The table to serialize.
- format: The specification of the delimited file format.
- java_writer: A Java `Writer` to which characters will be written.
write_to_writer : Table -> Delimited -> Writer -> Any
write_to_writer : Table -> File_Format.Delimited -> Writer -> Any
write_to_writer table format java_writer =
column_formatters = Panic.recover Illegal_Argument_Error <| case format.value_formatter of
Nothing -> table.columns.map column-> case column.storage_type of
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,11 @@ public String format(Object value) {
return format(decimal.doubleValue());
}

if (value instanceof Long integer) {
return format(integer.doubleValue());
}

throw new IllegalArgumentException("Unsupported type for DecimalFormatter.");
}

@Override
public boolean canFormat(Object value) {
return value instanceof Double || value instanceof Long;
return value instanceof Double;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.enso.table.formatting;

import java.math.BigInteger;
import java.text.DecimalFormat;

public class IntegerFormatter implements DataFormatter {
Expand Down Expand Up @@ -42,7 +41,7 @@ public String format(Object value) {
return format(integer.longValue());
}

throw new IllegalArgumentException("Unsupported type for DecimalFormatter.");
throw new IllegalArgumentException("Unsupported type for IntegerFormatter.");
}

@Override
Expand Down

0 comments on commit 2d4537f

Please sign in to comment.