Skip to content

Commit 23a36c9

Browse files
committed
feat: Loading from file and map values implemented
1 parent 33b68c6 commit 23a36c9

File tree

3 files changed

+102
-30
lines changed

3 files changed

+102
-30
lines changed

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

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,42 +7,61 @@
77
import java.nio.file.Files;
88
import java.nio.file.Path;
99
import java.nio.file.StandardOpenOption;
10-
import java.util.Map;
10+
import java.util.AbstractMap;
11+
import java.util.AbstractSet;
12+
import java.util.ArrayList;
13+
import java.util.Iterator;
14+
import java.util.LinkedHashMap;
15+
import java.util.List;
1116
import java.util.Set;
12-
import java.util.TreeMap;
13-
14-
public class Properties {
15-
private final Map<String, String> values = new TreeMap<>();
16-
17-
public String getProperty(String key) {
18-
return getProperty(key, null);
19-
}
20-
21-
public String getProperty(String key, String defaultValue) {
22-
return values.getOrDefault(key, defaultValue);
17+
import java.util.stream.Collectors;
18+
19+
public class Properties extends AbstractMap<String, String> {
20+
private final LinkedHashMap<String, String> values = new LinkedHashMap<>();
21+
private final List<PropertiesParser.Token> tokens = new ArrayList<>();
22+
23+
@Override
24+
public Set<Entry<String, String>> entrySet() {
25+
return new AbstractSet<Entry<String, String>>() {
26+
@Override
27+
public Iterator<Entry<String, String>> iterator() {
28+
return new Iterator<Entry<String, String>>() {
29+
Iterator<Entry<String, String>> iter = values.entrySet().iterator();
30+
31+
@Override
32+
public boolean hasNext() {
33+
return iter.hasNext();
34+
}
35+
36+
@Override
37+
public Entry<String, String> next() {
38+
return iter.next();
39+
}
40+
41+
@Override
42+
public void remove() {
43+
// TODO handle remove
44+
iter.remove();
45+
}
46+
};
47+
}
48+
49+
@Override
50+
public int size() {
51+
return values.entrySet().size();
52+
}
53+
};
2354
}
2455

25-
public String setProperty(String key, String value) {
56+
@Override
57+
public String put(String key, String value) {
58+
// TODO handle adds and replaces
2659
return values.put(key, value);
2760
}
2861

29-
public void setProperties(Properties props) {
30-
setProperties(props.values);
31-
}
32-
33-
public void setProperties(Map<String, String> props) {
34-
values.putAll(props);
35-
}
36-
37-
public boolean containsKey(String key) {
38-
return values.containsKey(key);
39-
}
40-
41-
public Set<String> keySet() {
42-
return values.keySet();
43-
}
44-
45-
public String remove(String key) {
62+
@Override
63+
public String remove(Object key) {
64+
// TODO handle remove
4665
return values.remove(key);
4766
}
4867

@@ -51,10 +70,22 @@ public void load(Path file) throws IOException {
5170
}
5271

5372
public void load(Reader reader) throws IOException {
73+
tokens.clear();
5474
BufferedReader br =
5575
reader instanceof BufferedReader
5676
? (BufferedReader) reader
5777
: new BufferedReader(reader);
78+
List<PropertiesParser.Token> ts = PropertiesParser.tokens(br)
79+
.collect(Collectors.toList());
80+
tokens.addAll(ts);
81+
String key = null;
82+
for (PropertiesParser.Token token : tokens) {
83+
if (token.type == PropertiesParser.Type.KEY) {
84+
key = token.getText();
85+
} else if (token.type == PropertiesParser.Type.VALUE) {
86+
values.put(key, token.getText());
87+
}
88+
}
5889
}
5990

6091
public static Properties loadProperties(Path file) throws IOException {
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package org.codejive.properties;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
import java.io.IOException;
6+
import java.net.URISyntaxException;
7+
import java.nio.file.Path;
8+
import java.nio.file.Paths;
9+
10+
import static org.hamcrest.MatcherAssert.assertThat;
11+
import static org.hamcrest.Matchers.aMapWithSize;
12+
import static org.hamcrest.Matchers.contains;
13+
import static org.hamcrest.Matchers.equalTo;
14+
15+
public class TestProperties {
16+
17+
@Test
18+
void testLoad() throws IOException, URISyntaxException {
19+
Path f = Paths.get(getClass().getResource("/test.properties").toURI());
20+
Properties p = Properties.loadProperties(f);
21+
assertThat(p, aMapWithSize(7));
22+
assertThat(p.keySet(), contains("one", "two", "three", " with spaces", "altsep", "multiline", "key.4"));
23+
assertThat(p.values(), contains("simple", "value containing spaces", "and escapes\n\t\r\f", "everywhere",
24+
"value", "one \n two \n\tthree", "\u1234"));
25+
}
26+
}

src/test/resources/test.properties

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#comment1
2+
# comment2
3+
4+
! comment3
5+
one=simple
6+
two=value containing spaces
7+
# another comment
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

0 commit comments

Comments
 (0)