From 0ff9ed78aadb32f85ee3e37f5574d7ca2e12cdf5 Mon Sep 17 00:00:00 2001 From: bjculkin Date: Thu, 6 Apr 2017 16:02:03 -0400 Subject: Add simple property setup --- .../java/bjc/utils/ioutils/SimpleProperties.java | 141 +++++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java') diff --git a/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java b/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java new file mode 100644 index 0000000..e3b0122 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/ioutils/SimpleProperties.java @@ -0,0 +1,141 @@ +package bjc.utils.ioutils; + +import java.io.InputStream; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Scanner; +import java.util.Set; + +/** + * Simple file based properties. + * + * @author EVE + * + */ +public class SimpleProperties implements Map { + private static Map props; + + /** + * Create a new set of simple properties. + */ + public SimpleProperties() { + props = new HashMap<>(); + } + + /** + * Load properties from the provided input stream. + * + * The format is the name, a space, then the body. + * + * All leading/trailing spaces from the name & body are removed. + * + * @param is + * The stream to read from. + * + * @param allowDuplicates + * Whether or not duplicate keys should be allowed. + */ + public void loadFrom(InputStream is, boolean allowDuplicates) { + try(Scanner scn = new Scanner(is)) { + while(scn.hasNextLine()) { + String ln = scn.nextLine().trim(); + + /* + * Skip blank lines/comments + */ + if(ln.equals("")) continue; + if(ln.equals("#")) continue; + + int sepIdx = ln.indexOf(' '); + + if(sepIdx == -1) { + throw new NoSuchElementException( + "Properties must be a name, a space, then the body"); + } + + String name = ln.substring(0, sepIdx).trim(); + String body = ln.substring(sepIdx).trim(); + + if(!allowDuplicates && containsKey(name)) { + String msg = String.format("Duplicate key '%s'", name); + + throw new IllegalStateException(msg); + } + + put(name, body); + } + } + } + + @Override + public int size() { + return props.size(); + } + + @Override + public boolean isEmpty() { + return props.isEmpty(); + } + + @Override + public boolean containsKey(Object key) { + return props.containsKey(key); + } + + @Override + public boolean containsValue(Object value) { + return props.containsValue(value); + } + + @Override + public String get(Object key) { + return props.get(key); + } + + @Override + public String put(String key, String value) { + return props.put(key, value); + } + + @Override + public String remove(Object key) { + return props.remove(key); + } + + @Override + public void putAll(Map m) { + props.putAll(m); + } + + @Override + public void clear() { + props.clear(); + } + + @Override + public Set keySet() { + return props.keySet(); + } + + @Override + public Collection values() { + return props.values(); + } + + @Override + public Set> entrySet() { + return props.entrySet(); + } + + @Override + public boolean equals(Object o) { + return props.equals(o); + } + + @Override + public int hashCode() { + return props.hashCode(); + } +} -- cgit v1.2.3