[util] Convert Printer from Java to Kotlin

This commit is contained in:
Sergej Jaskiewicz
2023-10-24 13:15:45 +02:00
committed by Space Team
parent c442d0e159
commit 895d107ac5
@@ -13,176 +13,140 @@
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
package org.jetbrains.kotlin.utils
package org.jetbrains.kotlin.utils; import java.io.IOException
import org.jetbrains.annotations.NotNull; /**
* A wrapper around an arbitrary [Appendable] making it convenient to print text with indentation.
*/
open class Printer private constructor(
private val out: Appendable,
private val maxBlankLines: Int,
private val indentUnit: String,
private var indent: String
) {
private var blankLineCountIncludingCurrent = 0
private var withholdIndentOnce = false
private var length = 0
import java.io.IOException; constructor(out: Appendable, indentUnit: String) : this(out, Int.MAX_VALUE, indentUnit)
import java.util.Collection;
import java.util.Iterator;
public class Printer { @JvmOverloads
private static final String DEFAULT_INDENTATION_UNIT = " "; constructor(out: Appendable, maxBlankLines: Int = Int.MAX_VALUE, indentUnit: String = DEFAULT_INDENTATION_UNIT) : this(
public static final String LINE_SEPARATOR = System.getProperty("line.separator"); out,
maxBlankLines,
indentUnit,
indent = "",
)
private final Appendable out; constructor(out: Appendable, parent: Printer) : this(out, parent.maxBlankLines, parent.indentUnit, parent.indent)
private final int maxBlankLines;
private String indent; private fun append(o: Any?) {
private final String indentUnit;
private int blankLineCountIncludingCurrent = 0;
private boolean withholdIndentOnce = false;
private int length = 0;
public Printer(@NotNull Appendable out) {
this(out, Integer.MAX_VALUE);
}
public Printer(@NotNull Appendable out, @NotNull String indentUnit) {
this(out, Integer.MAX_VALUE, indentUnit);
}
public Printer(@NotNull Appendable out, int maxBlankLines) {
this(out, maxBlankLines, DEFAULT_INDENTATION_UNIT);
}
public Printer(@NotNull Appendable out, int maxBlankLines, @NotNull String indentUnit) {
this(out, maxBlankLines, indentUnit, "");
}
private Printer(@NotNull Appendable out, int maxBlankLines, @NotNull String indentUnit, @NotNull String indent) {
this.out = out;
this.maxBlankLines = maxBlankLines;
this.indentUnit = indentUnit;
this.indent = indent;
}
public Printer(@NotNull Appendable out, @NotNull Printer parent) {
this(out, parent.maxBlankLines, parent.indentUnit, parent.indent);
}
private void append(Object o) {
try { try {
String string = o.toString(); val string = o.toString()
out.append(string); out.append(string)
length += string.length(); length += string.length
} } catch (e: IOException) {
catch (IOException e) {
// Do nothing // Do nothing
} }
} }
@NotNull fun println(vararg objects: Any?): Printer {
public Printer println(Object... objects) { print(*objects)
print(objects); printLineSeparator()
printLineSeparator(); return this
return this;
} }
private void printLineSeparator() { private fun printLineSeparator() {
if (blankLineCountIncludingCurrent <= maxBlankLines) { if (blankLineCountIncludingCurrent <= maxBlankLines) {
blankLineCountIncludingCurrent++; blankLineCountIncludingCurrent++
append(LINE_SEPARATOR); append(LINE_SEPARATOR)
} }
} }
@NotNull fun print(vararg objects: Any?): Printer {
public Printer print(Object... objects) {
if (withholdIndentOnce) { if (withholdIndentOnce) {
withholdIndentOnce = false; withholdIndentOnce = false
} else if (objects.isNotEmpty()) {
printIndent()
} }
else if (objects.length > 0) { printWithNoIndent(*objects)
printIndent(); return this
}
fun printIndent() {
append(indent)
}
fun printWithNoIndent(vararg objects: Any?): Printer {
for (`object` in objects) {
blankLineCountIncludingCurrent = 0
append(`object`)
} }
printWithNoIndent(objects); return this
return this;
} }
public void printIndent() { fun withholdIndentOnce(): Printer {
append(indent); withholdIndentOnce = true
return this
} }
@NotNull fun printlnWithNoIndent(vararg objects: Any?): Printer {
public Printer printWithNoIndent(Object... objects) { printWithNoIndent(*objects)
for (Object object : objects) { printLineSeparator()
blankLineCountIncludingCurrent = 0; return this
append(object);
}
return this;
} }
@NotNull fun pushIndent(): Printer {
public Printer withholdIndentOnce() { indent += indentUnit
withholdIndentOnce = true; return this
return this;
} }
@NotNull fun popIndent(): Printer {
public Printer printlnWithNoIndent(Object... objects) { check(indent.length >= indentUnit.length) { "No indentation to pop" }
printWithNoIndent(objects); indent = indent.substring(indentUnit.length)
printLineSeparator(); return this
return this;
} }
@NotNull fun separated(separator: Any, vararg items: Any?): Printer {
public Printer pushIndent() { for (i in items.indices) {
indent += indentUnit;
return this;
}
@NotNull
public Printer popIndent() {
if (indent.length() < indentUnit.length()) {
throw new IllegalStateException("No indentation to pop");
}
indent = indent.substring(indentUnit.length());
return this;
}
@NotNull
public Printer separated(Object separator, Object... items) {
for (int i = 0; i < items.length; i++) {
if (i > 0) { if (i > 0) {
printlnWithNoIndent(separator); printlnWithNoIndent(separator)
} }
printlnWithNoIndent(items[i]); printlnWithNoIndent(items[i])
} }
return this; return this
} }
@NotNull fun separated(separator: Any, items: Collection<*>): Printer {
public Printer separated(Object separator, Collection<?> items) { val iterator = items.iterator()
for (Iterator<?> iterator = items.iterator(); iterator.hasNext(); ) { while (iterator.hasNext()) {
printlnWithNoIndent(iterator.next()); printlnWithNoIndent(iterator.next())
if (iterator.hasNext()) { if (iterator.hasNext()) {
printlnWithNoIndent(separator); printlnWithNoIndent(separator)
} }
} }
return this; return this
} }
public boolean isEmpty() { val isEmpty: Boolean
return length == 0; get() = length == 0
override fun toString(): String {
return out.toString()
} }
@Override val currentIndentLengthInUnits: Int
public String toString() { get() = indent.length / indentUnit.length
return out.toString();
}
public int getCurrentIndentLengthInUnits() { val indentUnitLength: Int
return indent.length() / indentUnit.length(); get() = indentUnit.length
}
public int getIndentUnitLength() { companion object {
return indentUnit.length(); private const val DEFAULT_INDENTATION_UNIT = " "
@JvmField
val LINE_SEPARATOR: String = System.getProperty("line.separator")
} }
} }