Skip to content

Commit

Permalink
Simplify ion-java-cli OutputFormat (#1014)
Browse files Browse the repository at this point in the history
* Simplify ion-java-cli OutputFormat

* Clarify unreachable statement message

* Private static factories instead of functions
  • Loading branch information
jobarr-amzn authored Dec 12, 2024
1 parent 8f98013 commit fbdc0b1
Showing 1 changed file with 35 additions and 74 deletions.
109 changes: 35 additions & 74 deletions ion-java-cli/src/main/java/com/amazon/tools/cli/OutputFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}

0 comments on commit fbdc0b1

Please sign in to comment.