org.cache2k.xmlConfiguration.StandardPropertyParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of cache2k-xml-configuration Show documentation
Show all versions of cache2k-xml-configuration Show documentation
A light weight and high performance Java caching library. XML configuration file support.
This artifact is included in cache2k-all.
package org.cache2k.xmlConfiguration;
/*
* #%L
* cache2k XML configuration
* %%
* Copyright (C) 2000 - 2016 headissue GmbH, Munich
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import java.util.HashMap;
import java.util.Map;
/**
* @author Jens Wilke
*/
public class StandardPropertyParser implements PropertyParser {
private Map, ValueConverter> type2parser = new HashMap, ValueConverter>();
private static final Map UNIT2LONG = new HashMap() {{
put("KiB", 1024L);
put("MiB", 1024L * 1024);
put("GiB", 1024L * 1024 * 1024);
put("TiB", 1024L * 1024 * 1024 * 1024);
put("k", 1000L);
put("M", 1000L * 1000);
put("G", 1000L * 1000 * 1000);
put("T", 1000L * 1000 * 1000 * 1000);
put("s", 1000L);
put("m", 1000L * 60);
put("h", 1000L * 60 * 60);
put("d", 1000L * 60 * 60 * 24);
}};
public Object parse(Class> _targetType, String _value) throws Exception {
ValueConverter p = type2parser.get(_targetType);
if (p == null) {
throw new IllegalArgumentException("Unknown target type: " + _targetType);
}
return p.parse(_value);
}
private void addParser(Class> _type, ValueConverter> p) {
type2parser.put(_type, p);
}
private void addParser(Class> _primitiveType, Class> _type, ValueConverter> p) {
type2parser.put(_primitiveType, p);
type2parser.put(_type, p);
}
{
addParser(Integer.TYPE, Integer.class, new ValueConverter() {
@Override
public Integer parse(final String v) {
return Integer.valueOf(v);
}
});
addParser(Boolean.TYPE, Boolean.class, new ValueConverter() {
@Override
public Boolean parse(String v) {
v = v.toLowerCase();
if ("true".equals(v)) {
return true;
}
if ("false".equals(v)) {
return false;
}
throw new IllegalArgumentException("no boolean, true/false expected");
}
});
addParser(Long.TYPE, Long.class, new ValueConverter() {
@Override
public Long parse(String v) {
return parseLongWithUnitSuffix(v);
}
});
addParser(String.class, new ValueConverter() {
@Override
public String parse(final String v) {
return v;
}
});
}
static long parseLongWithUnitSuffix(String v) {
v = v.replace("_", "");
long _multiplier = 1;
int pos = v.length();
while(--pos >= 0 && !Character.isDigit(v.charAt(pos)));
if (pos < v.length() - 1) {
String _unitSuffix = v.substring(pos + 1);
Long _newMultiplier = UNIT2LONG.get(_unitSuffix);
if (_newMultiplier == null) {
throw new NumberFormatException("Unknown unit suffix in: \"" + v + "\"");
}
v = v.substring(0, pos + 1);
_multiplier = _newMultiplier;
}
return Long.valueOf(v) * _multiplier;
}
}