Skip to content

Commit 75f39ab

Browse files
committed
Add unwrite() and getLastChar() to SimpleWriter which now extends Appendable
1 parent a84066c commit 75f39ab

File tree

5 files changed

+211
-2
lines changed

5 files changed

+211
-2
lines changed

junit/io/sf/carte/util/BufferSimpleWriterTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ public void testBufferSimpleWriter() throws IOException {
3333
assertEquals("abcdef1", wri.toString());
3434
wri.unwrite(2);
3535
assertEquals("abcde", wri.toString());
36+
wri.append('g');
37+
assertEquals("abcdeg", wri.toString());
38+
wri.append("hijk", 1, 4);
39+
assertEquals("abcdegijk", wri.toString());
3640
wri.unwrite(20);
3741
assertEquals(0, wri.length());
3842
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
3+
Copyright (c) 2005-2025, Carlos Amengual.
4+
5+
SPDX-License-Identifier: BSD-3-Clause
6+
7+
Licensed under a BSD-style License. You can find the license here:
8+
https://css4j.github.io/LICENSE.txt
9+
10+
*/
11+
12+
package io.sf.carte.util;
13+
14+
import static org.junit.jupiter.api.Assertions.assertEquals;
15+
import static org.junit.jupiter.api.Assertions.assertThrows;
16+
17+
import java.io.IOException;
18+
import java.io.StringWriter;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
public class DefaultSimpleWriterTest {
23+
24+
@Test
25+
public void testDefaultSimpleWriter() throws IOException {
26+
StringWriter writer = new StringWriter();
27+
DefaultSimpleWriter wri = new DefaultSimpleWriter(writer);
28+
wri.write('a');
29+
wri.write("bc");
30+
char[] cbuf = {'d', 'e', 'f'};
31+
wri.write(cbuf , 0, cbuf.length);
32+
wri.write(1);
33+
wri.write(0);
34+
assertEquals("abcdef10", writer.toString());
35+
wri.append("ghijk", 1, 4);
36+
assertEquals("abcdef10hij", writer.toString());
37+
assertThrows(UnsupportedOperationException.class, () -> wri.unwrite());
38+
assertEquals("abcdef10hij", writer.toString());
39+
assertThrows(UnsupportedOperationException.class, () -> wri.unwrite(3));
40+
wri.append('_');
41+
assertEquals("abcdef10hij_", writer.toString());
42+
}
43+
44+
}

src/io/sf/carte/util/BufferSimpleWriter.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
package io.sf.carte.util;
1313

14+
import java.io.IOException;
15+
1416
/**
1517
* An implementation of {@link SimpleWriter} backed by a {@link StringBuilder}.
1618
*/
@@ -84,13 +86,29 @@ public String toString() {
8486
/**
8587
* Remove the last character found in this buffer, if any.
8688
*/
89+
@Override
8790
public void unwrite() {
8891
int len = buffer.length();
8992
if (len != 0) {
9093
buffer.setLength(len - 1);
9194
}
9295
}
9396

97+
/**
98+
* Get the last character processed.
99+
*
100+
* @return the last character processed.
101+
* @throws IllegalStateException if no character was processed yet.
102+
*/
103+
@Override
104+
public char getLastChar() throws IllegalStateException {
105+
int len = buffer.length();
106+
if (len == 0) {
107+
throw new IllegalStateException();
108+
}
109+
return buffer.charAt(len - 1);
110+
}
111+
94112
/**
95113
* Remove the last <code>numChars</code> characters found in this buffer.
96114
* <p>
@@ -99,6 +117,7 @@ public void unwrite() {
99117
*
100118
* @param numChars the number of characters to remove.
101119
*/
120+
@Override
102121
public void unwrite(int numChars) {
103122
int len = buffer.length() - numChars;
104123
if (len < 0) {
@@ -112,6 +131,12 @@ public void write(CharSequence s) {
112131
buffer.append(s);
113132
}
114133

134+
@Override
135+
public BufferSimpleWriter append(CharSequence csq, int start, int end) throws IOException {
136+
buffer.append(csq, start, end);
137+
return this;
138+
}
139+
115140
@Override
116141
public void write(char[] cbuf, int offset, int len) {
117142
buffer.append(cbuf, offset, len);
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
3+
Copyright (c) 2005-2025, Carlos Amengual.
4+
5+
SPDX-License-Identifier: BSD-3-Clause
6+
7+
Licensed under a BSD-style License. You can find the license here:
8+
https://css4j.github.io/LICENSE.txt
9+
10+
*/
11+
12+
package io.sf.carte.util;
13+
14+
import java.io.IOException;
15+
import java.io.Writer;
16+
17+
/**
18+
* An implementation of {@link SimpleWriter} backed by a {@link Writer}.
19+
*/
20+
public class DefaultSimpleWriter implements SimpleWriter {
21+
22+
private final Writer writer;
23+
24+
/**
25+
* Initializes a <code>BufferSimpleWriter</code> with a writer.
26+
*
27+
* @param writer the writer.
28+
*/
29+
public DefaultSimpleWriter(Writer writer) {
30+
super();
31+
this.writer = writer;
32+
}
33+
34+
/**
35+
* Get the {@code Writer}.
36+
*
37+
* @return the writer.
38+
*/
39+
public Writer getWriter() {
40+
return writer;
41+
}
42+
43+
@Override
44+
public void newLine() throws IOException {
45+
writer.append('\n');
46+
}
47+
48+
@Override
49+
public void write(CharSequence s) throws IOException {
50+
writer.append(s);
51+
}
52+
53+
@Override
54+
public void write(char[] cbuf, int offset, int len) throws IOException {
55+
writer.write(cbuf, offset, len);
56+
}
57+
58+
@Override
59+
public DefaultSimpleWriter append(CharSequence csq, int start, int end) throws IOException {
60+
writer.append(csq, start, end);
61+
return this;
62+
}
63+
64+
@Override
65+
public void write(char c) throws IOException {
66+
writer.append(c);
67+
}
68+
69+
@Override
70+
public void write(int num) throws IOException {
71+
writer.write(Integer.toString(num));
72+
}
73+
74+
}

src/io/sf/carte/util/SimpleWriter.java

Lines changed: 64 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
* instead may target another system.
2323
*
2424
*/
25-
public interface SimpleWriter {
25+
public interface SimpleWriter extends Appendable {
2626

2727
/**
2828
* Write a newline according to the characteristics of the target platform.
@@ -40,6 +40,21 @@ public interface SimpleWriter {
4040
*/
4141
void write(CharSequence seq) throws IOException;
4242

43+
@Override
44+
default SimpleWriter append(CharSequence csq) throws IOException {
45+
write(csq);
46+
return this;
47+
}
48+
49+
@Override
50+
default SimpleWriter append(CharSequence csq, int start, int end) throws IOException {
51+
if (csq == null) {
52+
csq = "null";
53+
}
54+
write(csq.subSequence(start, end));
55+
return this;
56+
}
57+
4358
/**
4459
* Write a sequence of characters from an array.
4560
*
@@ -64,6 +79,12 @@ public interface SimpleWriter {
6479
*/
6580
void write(char c) throws IOException;
6681

82+
@Override
83+
default SimpleWriter append(char c) throws IOException {
84+
write(c);
85+
return this;
86+
}
87+
6788
/**
6889
* Write an integer.
6990
*
@@ -72,6 +93,47 @@ public interface SimpleWriter {
7293
* @throws IOException
7394
* if an error happens when writing.
7495
*/
75-
void write(int num) throws IOException;
96+
default void write(int num) throws IOException {
97+
write(Integer.toString(num));
98+
}
99+
100+
/**
101+
* Remove the last character found in this buffer, if any.
102+
* <p>
103+
* Optional operation.
104+
* </p>
105+
*
106+
* @throws UnsupportedOperationException if the operation is not supported.
107+
*/
108+
default void unwrite() throws UnsupportedOperationException {
109+
throw new UnsupportedOperationException("Cannot unwrite.");
110+
}
111+
112+
/**
113+
* Remove the last <code>numChars</code> characters found in this buffer.
114+
* <p>
115+
* If <code>numChars</code> is larger than the length of this buffer, removes
116+
* all its contents.
117+
*
118+
* @param numChars the number of characters to remove.
119+
* @throws UnsupportedOperationException if the operation is not supported.
120+
*/
121+
default void unwrite(int numChars) throws UnsupportedOperationException {
122+
throw new UnsupportedOperationException("Cannot unwrite.");
123+
}
124+
125+
/**
126+
* Get the last character processed.
127+
* <p>
128+
* Optional operation.
129+
* </p>
130+
*
131+
* @return the last character processed.
132+
* @throws UnsupportedOperationException if the operation is not supported.
133+
* @throws IllegalStateException if no character was processed yet.
134+
*/
135+
default char getLastChar() throws UnsupportedOperationException, IllegalStateException {
136+
throw new UnsupportedOperationException("Cannot retrieve the last character.");
137+
}
76138

77139
}

0 commit comments

Comments
 (0)