blob: acb9be8294fff2368c8d925620841b001175cf59 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.debug;
import java.io.Serializable;
/**
* A StringBuilder that also handles indentation for append operations.
* @author tonytv
*/
@SuppressWarnings("serial")
final class IndentStringBuilder implements Serializable, Appendable, CharSequence {
private final StringBuilder builder = new StringBuilder();
private final String singleIndentation;
private int level = 0;
private boolean newline = true;
private void appendIndentation() {
if (newline) {
for (int i=0; i<level; i++) {
builder.append(singleIndentation);
}
}
newline = false;
}
public IndentStringBuilder(String singleIndentation) {
this.singleIndentation = singleIndentation;
}
public IndentStringBuilder() {
this(" ");
}
public void resetIndentLevel(int level) {
this.level = level;
}
//returns the indent level before indenting.
public int newlineAndIndent() {
newline();
return indent();
}
//returns the indent level before indenting.
public int indent() {
return level++;
}
public IndentStringBuilder newline() {
newline = true;
builder.append('\n');
return this;
}
public IndentStringBuilder append(Object o) {
appendIndentation();
builder.append(o);
return this;
}
public IndentStringBuilder append(String s) {
appendIndentation();
builder.append(s);
return this;
}
public IndentStringBuilder append(CharSequence charSequence) {
appendIndentation();
builder.append(charSequence);
return this;
}
public IndentStringBuilder append(CharSequence charSequence, int i, int i1) {
appendIndentation();
builder.append(charSequence, i, i1);
return this;
}
public IndentStringBuilder append(char c) {
appendIndentation();
builder.append(c);
return this;
}
public String toString() {
return builder.toString();
}
public int length() {
return builder.length();
}
public char charAt(int i) {
return builder.charAt(i);
}
public CharSequence subSequence(int i, int i1) {
return builder.subSequence(i, i1);
}
}
|