package com.ws.core.pagination;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
public class PropertiesHelper {
private static final int SYSTEM_PROPERTIES_MODE_NEVER = 0;
private static final int SYSTEM_PROPERTIES_MODE_FALLBACK = 1;
private static final int SYSTEM_PROPERTIES_MODE_OVERRIDE = 2;
private int systemPropertiesMode = 0;
private Properties p;
public PropertiesHelper(Properties p) {
this.setProperties(p);
}
public PropertiesHelper(Properties p, int systemPropertiesMode) {
this.setProperties(p);
if (systemPropertiesMode != SYSTEM_PROPERTIES_MODE_NEVER && systemPropertiesMode != SYSTEM_PROPERTIES_MODE_FALLBACK && systemPropertiesMode != SYSTEM_PROPERTIES_MODE_OVERRIDE) {
throw new IllegalArgumentException("error systemPropertiesMode mode:" + systemPropertiesMode);
} else {
this.systemPropertiesMode = systemPropertiesMode;
}
}
public Properties getProperties() {
return this.p;
}
public void setProperties(Properties props) {
if (props == null) {
throw new IllegalArgumentException("properties must be not null");
} else {
this.p = props;
}
}
public String getRequiredString(String key) {
String value = this.getProperty(key);
if (isBlankString(value)) {
throw new IllegalStateException("required property is blank by key=" + key);
} else {
return value;
}
}
public String getNullIfBlank(String key) {
String value = this.getProperty(key);
return isBlankString(value) ? null : value;
}
public String getNullIfEmpty(String key) {
String value = this.getProperty(key);
return value != null && !"".equals(value) ? value : null;
}
public String getAndTryFromSystem(String key) {
String value = this.getProperty(key);
if (isBlankString(value)) {
value = this.getSystemProperty(key);
}
return value;
}
private String getSystemProperty(String key) {
String value = System.getProperty(key);
if (isBlankString(value)) {
value = System.getenv(key);
}
return value;
}
public Integer getInteger(String key) {
String v = this.getProperty(key);
return v == null ? null : Integer.parseInt(v);
}
public int getInt(String key, int defaultValue) {
return this.getProperty(key) == null ? defaultValue : Integer.parseInt(this.getRequiredString(key));
}
public int getRequiredInt(String key) {
return Integer.parseInt(this.getRequiredString(key));
}
public Long getLong(String key) {
return this.getProperty(key) == null ? null : Long.parseLong(this.getRequiredString(key));
}
public long getLong(String key, long defaultValue) {
return this.getProperty(key) == null ? defaultValue : Long.parseLong(this.getRequiredString(key));
}
public Long getRequiredLong(String key) {
return Long.parseLong(this.getRequiredString(key));
}
public Boolean getBoolean(String key) {
return this.getProperty(key) == null ? null : Boolean.parseBoolean(this.getRequiredString(key));
}
public boolean getBoolean(String key, boolean defaultValue) {
return this.getProperty(key) == null ? defaultValue : Boolean.parseBoolean(this.getRequiredString(key));
}
public boolean getRequiredBoolean(String key) {
return Boolean.parseBoolean(this.getRequiredString(key));
}
public Float getFloat(String key) {
return this.getProperty(key) == null ? null : Float.parseFloat(this.getRequiredString(key));
}
public float getFloat(String key, float defaultValue) {
return this.getProperty(key) == null ? defaultValue : Float.parseFloat(this.getRequiredString(key));
}
public Float getRequiredFloat(String key) {
return Float.parseFloat(this.getRequiredString(key));
}
public Double getDouble(String key) {
return this.getProperty(key) == null ? null : Double.parseDouble(this.getRequiredString(key));
}
public double getDouble(String key, double defaultValue) {
return this.getProperty(key) == null ? defaultValue : Double.parseDouble(this.getRequiredString(key));
}
public Double getRequiredDouble(String key) {
return Double.parseDouble(this.getRequiredString(key));
}
public Object setProperty(String key, int value) {
return this.setProperty(key, String.valueOf(value));
}
public Object setProperty(String key, long value) {
return this.setProperty(key, String.valueOf(value));
}
public Object setProperty(String key, float value) {
return this.setProperty(key, String.valueOf(value));
}
public Object setProperty(String key, double value) {
return this.setProperty(key, String.valueOf(value));
}
public Object setProperty(String key, boolean value) {
return this.setProperty(key, String.valueOf(value));
}
public String[] getStringArray(String key) {
String v = this.getProperty(key);
return v == null ? new String[0] : tokenizeToStringArray(v, ", \t\n\r\f");
}
public int[] getIntArray(String key) {
return toIntArray(this.getStringArray(key));
}
public Properties getStartsWithProperties(String prefix) {
if (prefix == null) {
throw new IllegalArgumentException("'prefix' must be not null");
} else {
Properties props = this.getProperties();
Properties result = new Properties();
for (Entry<Object, Object> object : props.entrySet()) {
String key = (String) object.getKey();
if (key != null && key.startsWith(prefix)) {
result.put(key.substring(prefix.length()), object.getValue());
}
}
return result;
}
}
public String getProperty(String key, String defaultValue) {
String value = this.getProperty(key);
return isBlankString(value) ? defaultValue : value;
}
public String getProperty(String key) {
String propVal = null;
if (this.systemPropertiesMode == SYSTEM_PROPERTIES_MODE_OVERRIDE) {
propVal = this.getSystemProperty(key);
}
if (propVal == null) {
propVal = this.p.getProperty(key);
}
if (propVal == null && this.systemPropertiesMode == SYSTEM_PROPERTIES_MODE_FALLBACK) {
propVal = this.getSystemProperty(key);
}
return propVal;
}
public Object setProperty(String key, String value) {
return this.p.setProperty(key, value);
}
public void clear() {
this.p.clear();
}
public Set<Entry<Object, Object>> entrySet() {
return this.p.entrySet();
}
public Enumeration<?> propertyNames() {
return this.p.propertyNames();
}
public boolean contains(Object value) {
return this.p.contains(value);
}
public boolean containsKey(Object key) {
return this.p.containsKey(key);
}
public boolean containsValue(Object value) {
return this.p.containsValue(value);
}
public Enumeration<Object> elements() {
return this.p.elements();
}
public Object get(Object key) {
return this.p.get(key);
}
public boolean isEmpty() {
return this.p.isEmpty();
}
public Enumeration<Object> keys() {
return this.p.keys();
}
public Set<Object> keySet() {
return this.p.keySet();
}
public
评论2
最新资源