PageRenderTime 344ms CodeModel.GetById 141ms app.highlight 5ms RepoModel.GetById 196ms app.codeStats 1ms

/src/org/ooc/backend/TabbedWriter.java

http://github.com/nddrylliog/ooc
Java | 74 lines | 55 code | 16 blank | 3 comment | 2 complexity | abaa308b71dac0016c30f74d9094cca3 MD5 | raw file
 1package org.ooc.backend;
 2
 3import java.io.IOException;
 4import java.io.Writer;
 5
 6public class TabbedWriter implements Appendable {
 7
 8	protected Appendable appendable;
 9	protected int tabLevel;
10	protected String tab = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
11
12	public TabbedWriter(Appendable appendable) {
13		this.appendable = appendable;
14	}
15	
16	public void close() throws IOException {
17		if(appendable instanceof Writer) {
18			((Writer) appendable).close();
19		} else {
20			// well, do nothing, probably trying
21			// to close a StringBuilder, which is
22			// nonsense.
23		}
24	}
25	
26	public TabbedWriter append(char c) throws IOException {
27		appendable.append(c);
28		return this;
29	}
30	
31	public TabbedWriter append(String s) throws IOException {
32		appendable.append(s);
33		return this;
34	}
35	
36	public TabbedWriter writeTabs() throws IOException {
37		appendable.append(tab, 0, tabLevel);
38		return this;
39	}
40	
41	public TabbedWriter newUntabbedLine() throws IOException {
42		appendable.append('\n');
43		return this;
44	}
45	
46	public TabbedWriter nl() throws IOException {
47		return newUntabbedLine().writeTabs();
48	}
49	
50	public TabbedWriter newLine() throws IOException {
51		return newUntabbedLine().writeTabs();
52	}
53	
54	public TabbedWriter tab() {
55		tabLevel++;
56		return this;
57	}
58	
59	public TabbedWriter untab() {
60		tabLevel--;
61		return this;
62	}
63
64	public TabbedWriter append(CharSequence csq) throws IOException {
65		appendable.append(csq);
66		return this;
67	}
68
69	public TabbedWriter append(CharSequence csq, int start, int end) throws IOException {
70		appendable.append(csq, start, end);
71		return this;
72	}
73	
74}