panda.io.Settings Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of panda-core Show documentation
Show all versions of panda-core Show documentation
Panda Core is the core module of Panda Framework, it contains commonly used utility classes similar to apache-commons.
package panda.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import panda.bind.json.Jsons;
import panda.cast.Castors;
import panda.lang.Charsets;
import panda.lang.Strings;
import panda.lang.Texts;
public class Settings implements Map {
private Map props = new ConcurrentHashMap();
private Map froms = new ConcurrentHashMap();
public Settings() {
}
public Settings(String... files) throws IOException {
load(files);
}
/**
* load multiple properties files.
*
* @param paths properties files
* @throws IOException if an IO error occurred
*/
public void load(String... paths) throws IOException {
for (String path : paths) {
load(path);
}
}
/**
* @param is input stream
* @param from the name of the input stream
* @throws IOException if an IO error occurs
*/
public void load(InputStream is, String from) throws IOException {
Properties ps = new Properties();
ps.load(new InputStreamReader(is, Charsets.UTF_8));
putAll(ps, from);
}
/**
* @param file file
* @throws IOException if an IO error occurs
*/
public void load(File file) throws IOException {
InputStream is = null;
try {
is = new FileInputStream(file);
load(is, file.getAbsolutePath());
}
finally {
Streams.safeClose(is);
}
}
/**
* @param file file
* @throws IOException if an IO error occurs
*/
public void load(String file) throws IOException {
InputStream is = null;
try {
is = Streams.openInputStream(file);
load(is, file);
}
finally {
Streams.safeClose(is);
}
}
//------------------------------------------------------------------------
@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);
}
public String getFrom(Object key) {
return froms.get(key);
}
@Override
public String put(String key, String value) {
return put(key, value, Strings.EMPTY);
}
public String put(String key, String value, String from) {
if (value == null) {
return remove(key);
}
froms.put(key, Strings.defaultString(from));
String val = Texts.translate(value, this);
return props.put(key, val);
}
@Override
public String remove(Object key) {
froms.remove(key);
return props.remove(key);
}
public void putAll(Properties ps, String from) {
for (Iterator> i = ps.entrySet().iterator(); i.hasNext(); ) {
Entry