From fbdc0b1244a897a11582866ab32d1bdff9b86d56 Mon Sep 17 00:00:00 2001 From: Joshua Barr <70981087+jobarr-amzn@users.noreply.github.com> Date: Thu, 12 Dec 2024 11:31:29 -0800 Subject: [PATCH] Simplify ion-java-cli OutputFormat (#1014) * Simplify ion-java-cli OutputFormat * Clarify unreachable statement message * Private static factories instead of functions --- .../com/amazon/tools/cli/OutputFormat.java | 109 ++++++------------ 1 file changed, 35 insertions(+), 74 deletions(-) diff --git a/ion-java-cli/src/main/java/com/amazon/tools/cli/OutputFormat.java b/ion-java-cli/src/main/java/com/amazon/tools/cli/OutputFormat.java index 15a658fcd3..ab1882934c 100644 --- a/ion-java-cli/src/main/java/com/amazon/tools/cli/OutputFormat.java +++ b/ion-java-cli/src/main/java/com/amazon/tools/cli/OutputFormat.java @@ -11,78 +11,39 @@ * Represents the different Ion output formats supported by the command line com.amazon.tools in this package. */ public enum OutputFormat { - /** - * Nicely spaced, 'prettified' text Ion. - */ - PRETTY { - @Override - public IonWriter createIonWriter(OutputStream outputStream) { - return IonTextWriterBuilder.pretty().build(outputStream); - } - - @Override - public IonWriter createIonWriterWithImports(OutputStream outputStream, SymbolTable[] imports) { - return IonTextWriterBuilder.pretty().withImports(imports).build(outputStream); - } - }, - /** - * Minimally spaced text Ion. - */ - TEXT { - @Override - public IonWriter createIonWriter(OutputStream outputStream) { - return IonTextWriterBuilder.standard().build(outputStream); - } - - @Override - public IonWriter createIonWriterWithImports(OutputStream outputStream, SymbolTable[] imports) { - return IonTextWriterBuilder.standard().withImports(imports).build(outputStream); - } - }, - /** - * Compact, read-optimized binary Ion. - */ - BINARY { - @Override - public IonWriter createIonWriter(OutputStream outputStream) { - return IonBinaryWriterBuilder.standard().build(outputStream); - } - - @Override - public IonWriter createIonWriterWithImports(OutputStream outputStream, SymbolTable[] imports) { - return IonBinaryWriterBuilder.standard().withImports(imports).build(outputStream); - } - }, - /** - * Event Stream - */ - EVENTS { - @Override - public IonWriter createIonWriter(OutputStream outputStream) { - return IonTextWriterBuilder.pretty().build(outputStream); - } - - @Override - public IonWriter createIonWriterWithImports(OutputStream outputStream, SymbolTable[] imports) { - return IonTextWriterBuilder.pretty().withImports(imports).build(outputStream); - } - }, - /** - * None - */ - NONE { - @Override - public IonWriter createIonWriter(OutputStream outputStream) { - NoOpOutputStream out = new NoOpOutputStream(); - return IonTextWriterBuilder.pretty().build(out); - } - - @Override - public IonWriter createIonWriterWithImports(OutputStream outputStream, SymbolTable[] imports) { - return IonTextWriterBuilder.pretty().withImports(imports).build(outputStream); - } - }; - - abstract IonWriter createIonWriter(OutputStream outputStream); - abstract IonWriter createIonWriterWithImports(OutputStream outputStream, SymbolTable[] symbolTable); + /** Nicely spaced, 'prettified' text Ion */ PRETTY, + /** Minimally spaced text Ion */ TEXT, + /** Compact, read-optimized binary Ion */ BINARY, + /** Event Stream */ EVENTS, + /** No output, /dev/null */ NONE; + + IonWriter createIonWriter(OutputStream outputStream) { + return createIonWriter(this, outputStream); + } + + IonWriter createIonWriterWithImports(OutputStream outputStream, SymbolTable[] symbolTable) { + return createIonWriter(this, outputStream, symbolTable); + } + + private static IonWriter createIonWriter(OutputFormat format, OutputStream outputStream) { + switch (format) { + case TEXT: return IonTextWriterBuilder.standard().build(outputStream); + case PRETTY: return IonTextWriterBuilder.pretty().build(outputStream); + case EVENTS: return IonTextWriterBuilder.pretty().build(outputStream); + case BINARY: return IonBinaryWriterBuilder.standard().build(outputStream); + case NONE: return IonTextWriterBuilder.standard().build(new NoOpOutputStream()); + default: throw new IllegalStateException("Unsupported output format: " + format); + } + } + + private static IonWriter createIonWriter(OutputFormat format, OutputStream out, SymbolTable... symbols) { + switch (format) { + case TEXT: return IonTextWriterBuilder.standard().withImports(symbols).build(out); + case PRETTY: return IonTextWriterBuilder.pretty().withImports(symbols).build(out); + case EVENTS: return IonTextWriterBuilder.pretty().withImports(symbols).build(out); + case BINARY: return IonBinaryWriterBuilder.standard().withImports(symbols).build(out); + case NONE: return IonTextWriterBuilder.standard().withImports(symbols).build(new NoOpOutputStream()); + default: throw new IllegalStateException("Unsupported output format: " + format); + } + } }