Skip to content

Commit aeabff9

Browse files
committed
feat: Implemented remove()
1 parent af656e3 commit aeabff9

File tree

7 files changed

+145
-5
lines changed

7 files changed

+145
-5
lines changed

src/main/java/org/codejive/properties/Properties.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,9 @@ public Set<Entry<String, String>> entrySet() {
210210
@Override
211211
public Iterator<Entry<String, String>> iterator() {
212212
return new Iterator<Entry<String, String>>() {
213-
final Iterator<Entry<String, String>> iter = values.entrySet().iterator();
213+
private final Iterator<Entry<String, String>> iter =
214+
values.entrySet().iterator();
215+
private Entry<String, String> currentEntry;
214216

215217
@Override
216218
public boolean hasNext() {
@@ -219,12 +221,14 @@ public boolean hasNext() {
219221

220222
@Override
221223
public Entry<String, String> next() {
222-
return iter.next();
224+
return (currentEntry = iter.next());
223225
}
224226

225227
@Override
226228
public void remove() {
227-
// TODO handle remove
229+
if (currentEntry != null) {
230+
removeItem(currentEntry.getKey());
231+
}
228232
iter.remove();
229233
}
230234
};
@@ -382,8 +386,29 @@ private Cursor addNewKeyValue(String rawKey, String key, String rawValue, String
382386

383387
@Override
384388
public String remove(Object key) {
385-
// TODO handle remove
386-
return values.remove(key);
389+
String skey = key.toString();
390+
removeItem(skey);
391+
return values.remove(skey);
392+
}
393+
394+
private void removeItem(String skey) {
395+
setComment(skey, Collections.emptyList());
396+
Cursor pos = indexOf(skey);
397+
assert pos.isType(PropertiesParser.Type.KEY);
398+
pos.remove();
399+
assert pos.isType(PropertiesParser.Type.SEPARATOR);
400+
pos.remove();
401+
assert pos.isType(PropertiesParser.Type.VALUE);
402+
pos.remove();
403+
if (pos.isEol()) {
404+
pos.remove();
405+
}
406+
}
407+
408+
@Override
409+
public void clear() {
410+
tokens.clear();
411+
values.clear();
387412
}
388413

389414
/**

src/test/java/org/codejive/properties/TestProperties.java

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.nio.file.Path;
1010
import java.nio.file.Paths;
1111
import java.util.Collections;
12+
import java.util.Iterator;
1213
import org.junit.jupiter.api.Test;
1314

1415
public class TestProperties {
@@ -314,6 +315,72 @@ void testPutFirstWithHeader() throws IOException, URISyntaxException {
314315
}
315316
}
316317

318+
@Test
319+
void testRemoveFirst() throws IOException, URISyntaxException {
320+
Properties p = Properties.loadProperties(getResource("/test.properties"));
321+
p.remove("one");
322+
StringWriter sw = new StringWriter();
323+
p.store(sw);
324+
assertThat(sw.toString(), equalTo(readAll(getResource("/test-removefirst.properties"))));
325+
}
326+
327+
@Test
328+
void testRemoveMiddle() throws IOException, URISyntaxException {
329+
Properties p = Properties.loadProperties(getResource("/test.properties"));
330+
p.remove("three");
331+
StringWriter sw = new StringWriter();
332+
p.store(sw);
333+
assertThat(sw.toString(), equalTo(readAll(getResource("/test-removemiddle.properties"))));
334+
}
335+
336+
@Test
337+
void testRemoveLast() throws IOException, URISyntaxException {
338+
Properties p = Properties.loadProperties(getResource("/test.properties"));
339+
p.remove("key.4");
340+
StringWriter sw = new StringWriter();
341+
p.store(sw);
342+
assertThat(sw.toString(), equalTo(readAll(getResource("/test-removelast.properties"))));
343+
}
344+
345+
@Test
346+
void testRemoveAll() throws IOException, URISyntaxException {
347+
Properties p = Properties.loadProperties(getResource("/test.properties"));
348+
p.remove("one");
349+
p.remove("two");
350+
p.remove("three");
351+
p.remove(" with spaces");
352+
p.remove("altsep");
353+
p.remove("multiline");
354+
p.remove("key.4");
355+
StringWriter sw = new StringWriter();
356+
p.store(sw);
357+
assertThat(sw.toString(), equalTo(readAll(getResource("/test-removeall.properties"))));
358+
}
359+
360+
@Test
361+
void testRemoveMiddleIterator() throws IOException, URISyntaxException {
362+
Properties p = Properties.loadProperties(getResource("/test.properties"));
363+
Iterator iter = p.keySet().iterator();
364+
while (iter.hasNext()) {
365+
if (iter.next().equals("three")) {
366+
iter.remove();
367+
break;
368+
}
369+
}
370+
StringWriter sw = new StringWriter();
371+
p.store(sw);
372+
assertThat(sw.toString(), equalTo(readAll(getResource("/test-removemiddle.properties"))));
373+
}
374+
375+
@Test
376+
void testClear() throws IOException, URISyntaxException {
377+
Properties p = Properties.loadProperties(getResource("/test.properties"));
378+
p.clear();
379+
StringWriter sw = new StringWriter();
380+
p.store(sw);
381+
assertThat(sw.toString(), equalTo(readAll(getResource("/test-clear.properties"))));
382+
}
383+
317384
@Test
318385
void testRemoveComment() throws IOException, URISyntaxException {
319386
Properties p = Properties.loadProperties(getResource("/test.properties"));

src/test/resources/test-clear.properties

Whitespace-only changes.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#comment1
2+
# comment2
3+
4+
# final comment
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#comment1
2+
# comment2
3+
4+
two=value containing spaces
5+
# another comment
6+
! and a comment
7+
! block
8+
three=and escapes\n\t\r\f
9+
\ with\ spaces = everywhere
10+
altsep:value
11+
multiline = one \
12+
two \
13+
three
14+
key.4 = \u1234
15+
# final comment
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#comment1
2+
# comment2
3+
4+
! comment3
5+
one=simple
6+
two=value containing spaces
7+
# another comment
8+
! and a comment
9+
! block
10+
three=and escapes\n\t\r\f
11+
\ with\ spaces = everywhere
12+
altsep:value
13+
multiline = one \
14+
two \
15+
three
16+
# final comment
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#comment1
2+
# comment2
3+
4+
! comment3
5+
one=simple
6+
two=value containing spaces
7+
\ with\ spaces = everywhere
8+
altsep:value
9+
multiline = one \
10+
two \
11+
three
12+
key.4 = \u1234
13+
# final comment

0 commit comments

Comments
 (0)