l2j_mobius/L2J_Mobius_Test/java/com/l2jmobius/util/PropertiesParser.java
MobiusDev 93c43d7067
2016-10-20 23:40:28 +00:00

275 lines
8.1 KiB
Java

/*
* This file is part of the L2J Mobius project.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.l2jmobius.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.time.Duration;
import java.util.Properties;
import java.util.logging.Logger;
/**
* Simplifies loading of property files and adds logging if a non existing property is requested.
* @author NosBit
*/
public final class PropertiesParser
{
private static final Logger _log = Logger.getLogger(PropertiesParser.class.getName());
private final Properties _properties = new Properties();
private final File _file;
public PropertiesParser(String name)
{
this(new File(name));
}
public PropertiesParser(File file)
{
_file = file;
try (FileInputStream fileInputStream = new FileInputStream(file))
{
try (InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, Charset.defaultCharset()))
{
_properties.load(inputStreamReader);
}
}
catch (Exception e)
{
_log.warning("[" + _file.getName() + "] There was an error loading config reason: " + e.getMessage());
}
}
public boolean containskey(String key)
{
return _properties.containsKey(key);
}
private String getValue(String key)
{
final String value = _properties.getProperty(key);
return value != null ? value.trim() : null;
}
public boolean getBoolean(String key, boolean defaultValue)
{
final String value = getValue(key);
if (value == null)
{
_log.warning("[" + _file.getName() + "] missing property for key: " + key + " using default value: " + defaultValue);
return defaultValue;
}
if (value.equalsIgnoreCase("true"))
{
return true;
}
else if (value.equalsIgnoreCase("false"))
{
return false;
}
else
{
_log.warning("[" + _file.getName() + "] Invalid value specified for key: " + key + " specified value: " + value + " should be \"boolean\" using default value: " + defaultValue);
return defaultValue;
}
}
public byte getByte(String key, byte defaultValue)
{
final String value = getValue(key);
if (value == null)
{
_log.warning("[" + _file.getName() + "] missing property for key: " + key + " using default value: " + defaultValue);
return defaultValue;
}
try
{
return Byte.parseByte(value);
}
catch (NumberFormatException e)
{
_log.warning("[" + _file.getName() + "] Invalid value specified for key: " + key + " specified value: " + value + " should be \"byte\" using default value: " + defaultValue);
return defaultValue;
}
}
public short getShort(String key, short defaultValue)
{
final String value = getValue(key);
if (value == null)
{
_log.warning("[" + _file.getName() + "] missing property for key: " + key + " using default value: " + defaultValue);
return defaultValue;
}
try
{
return Short.parseShort(value);
}
catch (NumberFormatException e)
{
_log.warning("[" + _file.getName() + "] Invalid value specified for key: " + key + " specified value: " + value + " should be \"short\" using default value: " + defaultValue);
return defaultValue;
}
}
public int getInt(String key, int defaultValue)
{
final String value = getValue(key);
if (value == null)
{
_log.warning("[" + _file.getName() + "] missing property for key: " + key + " using default value: " + defaultValue);
return defaultValue;
}
try
{
return Integer.parseInt(value);
}
catch (NumberFormatException e)
{
_log.warning("[" + _file.getName() + "] Invalid value specified for key: " + key + " specified value: " + value + " should be \"int\" using default value: " + defaultValue);
return defaultValue;
}
}
public long getLong(String key, long defaultValue)
{
final String value = getValue(key);
if (value == null)
{
_log.warning("[" + _file.getName() + "] missing property for key: " + key + " using default value: " + defaultValue);
return defaultValue;
}
try
{
return Long.parseLong(value);
}
catch (NumberFormatException e)
{
_log.warning("[" + _file.getName() + "] Invalid value specified for key: " + key + " specified value: " + value + " should be \"long\" using default value: " + defaultValue);
return defaultValue;
}
}
public float getFloat(String key, float defaultValue)
{
final String value = getValue(key);
if (value == null)
{
_log.warning("[" + _file.getName() + "] missing property for key: " + key + " using default value: " + defaultValue);
return defaultValue;
}
try
{
return Float.parseFloat(value);
}
catch (NumberFormatException e)
{
_log.warning("[" + _file.getName() + "] Invalid value specified for key: " + key + " specified value: " + value + " should be \"float\" using default value: " + defaultValue);
return defaultValue;
}
}
public double getDouble(String key, double defaultValue)
{
final String value = getValue(key);
if (value == null)
{
_log.warning("[" + _file.getName() + "] missing property for key: " + key + " using default value: " + defaultValue);
return defaultValue;
}
try
{
return Double.parseDouble(value);
}
catch (NumberFormatException e)
{
_log.warning("[" + _file.getName() + "] Invalid value specified for key: " + key + " specified value: " + value + " should be \"double\" using default value: " + defaultValue);
return defaultValue;
}
}
public String getString(String key, String defaultValue)
{
final String value = getValue(key);
if (value != null)
{
return value;
}
_log.warning("[" + _file.getName() + "] missing property for key: " + key + " using default value: " + defaultValue);
return defaultValue;
}
public <T extends Enum<T>> T getEnum(String key, Class<T> clazz, T defaultValue)
{
final String value = getValue(key);
if (value == null)
{
_log.warning("[" + _file.getName() + "] missing property for key: " + key + " using default value: " + defaultValue);
return defaultValue;
}
try
{
return Enum.valueOf(clazz, value);
}
catch (IllegalArgumentException e)
{
_log.warning("[" + _file.getName() + "] Invalid value specified for key: " + key + " specified value: " + value + " should be enum value of \"" + clazz.getSimpleName() + "\" using default value: " + defaultValue);
return defaultValue;
}
}
/**
* @param durationPattern
* @param defaultValue
* @return {@link Duration} object by the durationPattern specified, {@code null} in case of malformed pattern.
*/
public Duration getDuration(String durationPattern, String defaultValue)
{
return getDuration(durationPattern, defaultValue, null);
}
/**
* @param durationPattern
* @param defaultValue
* @param defaultDuration
* @return {@link Duration} object by the durationPattern specified, the defaultDuration in case of malformed pattern.
*/
public Duration getDuration(String durationPattern, String defaultValue, Duration defaultDuration)
{
final String value = getString(durationPattern, defaultValue);
try
{
return TimeUtil.parseDuration(value);
}
catch (IllegalStateException e)
{
_log.warning("[" + _file.getName() + "] Invalid value specified for key: " + durationPattern + " specified value: " + value + " should be time patttern using default value: " + defaultValue);
}
return defaultDuration;
}
}