blob: addc8e8f7c0bdde52eb3f8d77882d77336ab6756 [file] [log] [blame]
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.textclassifier.utils;
import androidx.core.util.Preconditions;
import java.io.PrintWriter;
/**
* A print writer that supports indentation.
*
* @see PrintWriter
*/
public final class IndentingPrintWriter {
private static final String SINGLE_INDENT = " ";
private final PrintWriter mWriter;
private StringBuilder mIndentBuilder = new StringBuilder();
private String mCurrentIndent = "";
public IndentingPrintWriter(PrintWriter writer) {
mWriter = Preconditions.checkNotNull(writer);
}
/** Prints a string. */
public IndentingPrintWriter println(String string) {
mWriter.print(mCurrentIndent);
mWriter.print(string);
mWriter.println();
return this;
}
/** Prints a empty line */
public IndentingPrintWriter println() {
mWriter.println();
return this;
}
/** Increases indents for subsequent texts. */
public IndentingPrintWriter increaseIndent() {
mIndentBuilder.append(SINGLE_INDENT);
mCurrentIndent = mIndentBuilder.toString();
return this;
}
/** Decreases indents for subsequent texts. */
public IndentingPrintWriter decreaseIndent() {
mIndentBuilder.delete(0, SINGLE_INDENT.length());
mCurrentIndent = mIndentBuilder.toString();
return this;
}
/** Prints a key-valued pair. */
public IndentingPrintWriter printPair(String key, Object value) {
println(String.format("%s=%s", key, String.valueOf(value)));
return this;
}
/** Flushes the stream. */
public void flush() {
mWriter.flush();
}
}