Skip to content

Commit

Permalink
Parameterizes IonTextWriterBuilder with the concrete builder type.
Browse files Browse the repository at this point in the history
  • Loading branch information
tgregg committed May 13, 2024
1 parent 1174837 commit 1afd1c0
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* NOT FOR APPLICATION USE!
*/
public abstract class _Private_IonTextWriterBuilder<T extends _Private_IonTextWriterBuilder<T>>
extends IonTextWriterBuilder
extends IonTextWriterBuilder<T>
{
private final static CharSequence SPACE_CHARACTER = " ";

Expand Down Expand Up @@ -79,15 +79,15 @@ public T mutable()
//=========================================================================

@Override
public IonTextWriterBuilder withPrettyPrinting()
public T withPrettyPrinting()
{
T b = mutable();
b._pretty_print = true;
return b;
}

@Override
public IonTextWriterBuilder withJsonDowngrade()
public T withJsonDowngrade()
{
T b = mutable();

Expand Down Expand Up @@ -149,7 +149,7 @@ final CharSequence topLevelSeparator()
T fillDefaults()
{
// Ensure that we don't modify the user's builder.
IonTextWriterBuilder b = copy();
_Private_IonTextWriterBuilder b = copy();

if (b.getCatalog() == null)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
package com.amazon.ion.impl;

import com.amazon.ion.IonCatalog;
import com.amazon.ion.IonWriter;
import com.amazon.ion.SymbolTable;
import com.amazon.ion.impl.bin.DelimitedContainerStrategy;
import com.amazon.ion.impl.bin.IonManagedWriter_1_1;
import com.amazon.ion.impl.bin.ManagedWriterOptions_1_1;
import com.amazon.ion.impl.bin.SymbolInliningStrategy;
import com.amazon.ion.system.IonTextWriterBuilder_1_1;

import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Objects;

/**
Expand Down Expand Up @@ -55,9 +52,6 @@ public IonTextWriterBuilder_1_1 withSymbolInliningStrategy(SymbolInliningStrateg
return b;
}

// The following methods are overridden in order to resolve a clashing return type, as they are defined by
// multiple ancestors (version-agnostic abstract classes and the Ion 1.1 interface).

@Override
public _Private_IonTextWriterBuilder_1_1 copy()
{
Expand All @@ -76,71 +70,6 @@ public _Private_IonTextWriterBuilder_1_1 mutable()
return copy();
}

@Override
public _Private_IonTextWriterBuilder_1_1 withCatalog(IonCatalog catalog) {
return (_Private_IonTextWriterBuilder_1_1) super.getCatalog();
}

@Override
public _Private_IonTextWriterBuilder_1_1 withImports(SymbolTable[] imports) {
return (_Private_IonTextWriterBuilder_1_1) super.getCatalog();
}

@Override
public _Private_IonTextWriterBuilder_1_1 withPrettyPrinting() {
return (_Private_IonTextWriterBuilder_1_1) super.withPrettyPrinting();
}

@Override
public _Private_IonTextWriterBuilder_1_1 withJsonDowngrade() {
return (_Private_IonTextWriterBuilder_1_1) super.withJsonDowngrade();
}

@Override
public _Private_IonTextWriterBuilder_1_1 withCharset(Charset charset) {
return (_Private_IonTextWriterBuilder_1_1) super.withCharset(charset);
}

@Override
public _Private_IonTextWriterBuilder_1_1 withCharsetAscii() {
return (_Private_IonTextWriterBuilder_1_1) super.withCharsetAscii();
}

@Override
public _Private_IonTextWriterBuilder_1_1 withMinimalSystemData() {
return (_Private_IonTextWriterBuilder_1_1) super.withMinimalSystemData();
}

@Override
public _Private_IonTextWriterBuilder_1_1 withLstMinimizing(LstMinimizing minimizing) {
return (_Private_IonTextWriterBuilder_1_1) super.withLstMinimizing(minimizing);
}

@Override
public _Private_IonTextWriterBuilder_1_1 withLongStringThreshold(int threshold) {
return (_Private_IonTextWriterBuilder_1_1) super.withLongStringThreshold(threshold);
}

@Override
public _Private_IonTextWriterBuilder_1_1 withNewLineType(NewLineType newLineType) {
return (_Private_IonTextWriterBuilder_1_1) super.withNewLineType(newLineType);
}

@Override
public _Private_IonTextWriterBuilder_1_1 withWriteTopLevelValuesOnNewLines(boolean writeTopLevelValuesOnNewLines) {
return (_Private_IonTextWriterBuilder_1_1) super.withWriteTopLevelValuesOnNewLines(writeTopLevelValuesOnNewLines);
}

@Override
public _Private_IonTextWriterBuilder_1_1 withMaximumTimestampPrecisionDigits(int maximumTimestampPrecisionDigits) {
return (_Private_IonTextWriterBuilder_1_1) super.withMaximumTimestampPrecisionDigits(maximumTimestampPrecisionDigits);
}

@Override
public _Private_IonTextWriterBuilder_1_1 withIvmMinimizing(IvmMinimizing minimizing) {
return (_Private_IonTextWriterBuilder_1_1) super.withIvmMinimizing(minimizing);
}

@Override
public IonWriter build(Appendable out) {
if (out == null) {
Expand Down
87 changes: 43 additions & 44 deletions src/main/java/com/amazon/ion/system/IonTextWriterBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,11 @@
* <a href="https://github.com/amazon-ion/ion-java/issues/32">issue amazon-ion/ion-java/issues/32</a>
* if you require it.
*
* @param <T> the concrete type of the builder implementation. Users should either assign instances to
* {@code IonTextWriterBuilder<?>}, or to a more specific type, like {@link IonTextWriterBuilder_1_1}.
*/
public abstract class IonTextWriterBuilder
extends IonWriterBuilderBase<IonTextWriterBuilder>
public abstract class IonTextWriterBuilder<T extends IonTextWriterBuilder<T>>
extends IonWriterBuilderBase<T>
{
/**
* A strategy for minimizing the output of local symbol tables.
Expand Down Expand Up @@ -148,7 +150,7 @@ public CharSequence getCharSequence() {
* @see #pretty()
* @see #json()
*/
public static IonTextWriterBuilder standard()
public static IonTextWriterBuilder<?> standard()
{
return _Private_IonTextWriterBuilder_1_0.standard();
}
Expand All @@ -163,7 +165,7 @@ public static IonTextWriterBuilder standard()
*
*/
public static IonTextWriterBuilder minimal()
public static IonTextWriterBuilder<?> minimal()
{
return standard().withMinimalSystemData();
}
Expand All @@ -180,7 +182,7 @@ public static IonTextWriterBuilder minimal()
*
* @see #withPrettyPrinting()
*/
public static IonTextWriterBuilder pretty()
public static IonTextWriterBuilder<?> pretty()
{
return standard().withPrettyPrinting();
}
Expand All @@ -192,7 +194,7 @@ public static IonTextWriterBuilder pretty()
*
* @see #withJsonDowngrade()
*/
public static IonTextWriterBuilder json()
public static IonTextWriterBuilder<?> json()
{
return standard().withJsonDowngrade();
}
Expand All @@ -204,14 +206,14 @@ public static IonTextWriterBuilder json()
// * Re-use same imports after a finish
// * Indentation CharSeq

private Charset myCharset;
private InitialIvmHandling myInitialIvmHandling;
private IvmMinimizing myIvmMinimizing;
private LstMinimizing myLstMinimizing;
private int myLongStringThreshold;
private NewLineType myNewLineType;
private boolean myTopLevelValuesOnNewLines;
private int myMaximumTimestampPrecisionDigits = Timestamp.DEFAULT_MAXIMUM_DIGITS_TEXT;
Charset myCharset;
InitialIvmHandling myInitialIvmHandling;
IvmMinimizing myIvmMinimizing;
LstMinimizing myLstMinimizing;
int myLongStringThreshold;
NewLineType myNewLineType;
boolean myTopLevelValuesOnNewLines;
int myMaximumTimestampPrecisionDigits = Timestamp.DEFAULT_MAXIMUM_DIGITS_TEXT;


/** NOT FOR APPLICATION USE! */
Expand All @@ -220,7 +222,7 @@ protected IonTextWriterBuilder()
}

/** NOT FOR APPLICATION USE! */
protected IonTextWriterBuilder(IonTextWriterBuilder that)
protected IonTextWriterBuilder(T that)
{
super(that);

Expand All @@ -240,23 +242,23 @@ protected IonTextWriterBuilder(IonTextWriterBuilder that)


@Override
public abstract IonTextWriterBuilder copy();
public abstract T copy();

@Override
public abstract IonTextWriterBuilder immutable();
public abstract T immutable();

@Override
public abstract IonTextWriterBuilder mutable();
public abstract T mutable();


@Override
public IonTextWriterBuilder withCatalog(IonCatalog catalog)
public T withCatalog(IonCatalog catalog)
{
return super.withCatalog(catalog);
}

@Override
public IonTextWriterBuilder withImports(SymbolTable... imports)
public T withImports(SymbolTable... imports)
{
return super.withImports(imports);
}
Expand Down Expand Up @@ -320,9 +322,9 @@ public void setCharset(Charset charset)
* @see #getCharset()
* @see #setCharset(Charset)
*/
public IonTextWriterBuilder withCharset(Charset charset)
public T withCharset(Charset charset)
{
IonTextWriterBuilder b = mutable();
T b = mutable();
b.setCharset(charset);
return b;
}
Expand All @@ -333,7 +335,7 @@ public IonTextWriterBuilder withCharset(Charset charset)
* @return this instance, if mutable;
* otherwise a mutable copy of this instance.
*/
public IonTextWriterBuilder withCharsetAscii()
public T withCharsetAscii()
{
return withCharset(ASCII);
}
Expand All @@ -359,9 +361,9 @@ public IonTextWriterBuilder withCharsetAscii()
*
*/
public IonTextWriterBuilder withMinimalSystemData()
public T withMinimalSystemData()
{
IonTextWriterBuilder b = mutable();
T b = mutable();
b.setInitialIvmHandling(SUPPRESS);
b.setIvmMinimizing(IvmMinimizing.DISTANT);
b.setLstMinimizing(LstMinimizing.EVERYTHING);
Expand All @@ -382,7 +384,7 @@ public IonTextWriterBuilder withMinimalSystemData()
* @return this instance, if mutable;
* otherwise a mutable copy of this instance.
*/
public abstract IonTextWriterBuilder withPrettyPrinting();
public abstract T withPrettyPrinting();


/**
Expand All @@ -406,7 +408,7 @@ public IonTextWriterBuilder withMinimalSystemData()
* @return this instance, if mutable;
* otherwise a mutable copy of this instance.
*/
public abstract IonTextWriterBuilder withJsonDowngrade();
public abstract T withJsonDowngrade();


//-------------------------------------------------------------------------
Expand Down Expand Up @@ -461,10 +463,9 @@ public void setInitialIvmHandling(InitialIvmHandling handling)
* @see #setInitialIvmHandling(IonWriterBuilder.InitialIvmHandling)
* @see #withInitialIvmHandling(IonWriterBuilder.InitialIvmHandling)
*/
public final IonTextWriterBuilder
withInitialIvmHandling(InitialIvmHandling handling)
public final T withInitialIvmHandling(InitialIvmHandling handling)
{
IonTextWriterBuilder b = mutable();
T b = mutable();
b.setInitialIvmHandling(handling);
return b;
}
Expand Down Expand Up @@ -525,10 +526,9 @@ public void setIvmMinimizing(IvmMinimizing minimizing)
*
*/
public IonTextWriterBuilder
withIvmMinimizing(IvmMinimizing minimizing)
public T withIvmMinimizing(IvmMinimizing minimizing)
{
IonTextWriterBuilder b = mutable();
T b = mutable();
b.setIvmMinimizing(minimizing);
return b;
}
Expand Down Expand Up @@ -587,10 +587,9 @@ public void setLstMinimizing(LstMinimizing minimizing)
*
*/
public IonTextWriterBuilder
withLstMinimizing(LstMinimizing minimizing)
public T withLstMinimizing(LstMinimizing minimizing)
{
IonTextWriterBuilder b = mutable();
T b = mutable();
b.setLstMinimizing(minimizing);
return b;
}
Expand Down Expand Up @@ -644,9 +643,9 @@ public void setLongStringThreshold(int threshold)
* @return this instance, if mutable;
* otherwise a mutable copy of this instance.
*/
public IonTextWriterBuilder withLongStringThreshold(int threshold)
public T withLongStringThreshold(int threshold)
{
IonTextWriterBuilder b = mutable();
T b = mutable();
b.setLongStringThreshold(threshold);
return b;
}
Expand Down Expand Up @@ -697,9 +696,9 @@ public void setNewLineType(NewLineType newLineType)
* @return this instance, if mutable;
* otherwise a mutable copy of this instance.
*/
public IonTextWriterBuilder withNewLineType(NewLineType newLineType)
public T withNewLineType(NewLineType newLineType)
{
IonTextWriterBuilder b = mutable();
T b = mutable();
b.setNewLineType(newLineType);
return b;
}
Expand Down Expand Up @@ -747,9 +746,9 @@ public void setWriteTopLevelValuesOnNewLines(boolean writeTopLevelValuesOnNewLin
* @see #getWriteTopLevelValuesOnNewLines()
* @see #setWriteTopLevelValuesOnNewLines(boolean)
*/
public IonTextWriterBuilder withWriteTopLevelValuesOnNewLines(boolean writeTopLevelValuesOnNewLines)
public T withWriteTopLevelValuesOnNewLines(boolean writeTopLevelValuesOnNewLines)
{
IonTextWriterBuilder b = mutable();
T b = mutable();
b.setWriteTopLevelValuesOnNewLines(writeTopLevelValuesOnNewLines);
return b;
}
Expand Down Expand Up @@ -789,8 +788,8 @@ public void setMaximumTimestampPrecisionDigits(int maximumTimestampPrecisionDigi
* @see #getMaximumTimestampPrecisionDigits()
* @see #setMaximumTimestampPrecisionDigits(int)
*/
public IonTextWriterBuilder withMaximumTimestampPrecisionDigits(int maximumTimestampPrecisionDigits) {
IonTextWriterBuilder b = mutable();
public T withMaximumTimestampPrecisionDigits(int maximumTimestampPrecisionDigits) {
T b = mutable();
b.setMaximumTimestampPrecisionDigits(maximumTimestampPrecisionDigits);
return b;
}
Expand Down

0 comments on commit 1afd1c0

Please sign in to comment.