
jp.go.nict.langrid.commons.parameter.ParameterContext Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jp.go.nict.langrid.commons Show documentation
Show all versions of jp.go.nict.langrid.commons Show documentation
Common and utility library for the Service Grid Server Software and java web services.
The newest version!
/*
* $Id: ParameterContext.java 918 2013-08-15 02:20:44Z t-nakaguchi $
*
* This is a program for Language Grid Core Node. This combines multiple language resources and provides composite language services.
* Copyright (C) 2009 NICT Language Grid Project.
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 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 Lesser
* General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package jp.go.nict.langrid.commons.parameter;
import java.util.logging.Level;
import java.util.logging.Logger;
import jp.go.nict.langrid.commons.transformer.PassthroughTransformer;
import jp.go.nict.langrid.commons.transformer.StringSplittingTransformer;
import jp.go.nict.langrid.commons.transformer.StringToBooleanTransformer;
import jp.go.nict.langrid.commons.transformer.StringToDoubleTransformer;
import jp.go.nict.langrid.commons.transformer.StringToEnumTransformer;
import jp.go.nict.langrid.commons.transformer.StringToFloatTransformer;
import jp.go.nict.langrid.commons.transformer.StringToIntegerTransformer;
import jp.go.nict.langrid.commons.transformer.StringToLongTransformer;
import jp.go.nict.langrid.commons.transformer.TransformationException;
import jp.go.nict.langrid.commons.transformer.Transformer;
import jp.go.nict.langrid.commons.util.function.Supplier;
/**
*
*
* @author Takao Nakaguchi
* @author $Author: t-nakaguchi $
* @version $Revision: 918 $
*/
public abstract class ParameterContext {
/**
*
*
*/
public abstract String getValue(String name);
/**
*
*
*/
public boolean getBoolean(String parameterName, boolean defaultValue){
return getWithTransformer(
parameterName, defaultValue, s2b
);
}
/**
*
*
*/
public Boolean getBoolean(String parameterName){
return getWithTransformer(parameterName, s2b);
}
/**
*
*
*/
public int getInteger(String parameterName, int defaultValue){
return getWithTransformer(
parameterName, defaultValue, s2i
);
}
/**
*
*
*/
public Integer getInteger(String parameterName){
return getWithTransformer(parameterName, s2i);
}
/**
*
*
*/
public long getLong(String parameterName, long defaultValue){
return getWithTransformer(
parameterName, defaultValue, s2l
);
}
/**
*
*
*/
public Long getLong(String parameterName){
return getWithTransformer(parameterName, s2l);
}
/**
*
*
*/
public float getFloat(String parameterName, float defaultValue){
return getWithTransformer(
parameterName, defaultValue, s2f
);
}
/**
*
*
*/
public Float getFloat(String parameterName){
return getWithTransformer(parameterName, s2f);
}
/**
*
*
*/
public double getDouble(String parameterName, double defaultValue){
return getWithTransformer(
parameterName, defaultValue, s2d
);
}
/**
*
*
*/
public Double getDouble(String parameterName){
return getWithTransformer(parameterName, s2d);
}
/**
*
*
*/
public String getString(String parameterName, String defaultValue){
return getWithTransformer(
parameterName, defaultValue, s2s
);
}
public String getString(String parameterName, Supplier supplier){
return getWithTransformer(
parameterName, supplier, s2s
);
}
/**
*
*
*/
public String[] getStrings(String parameterName, String[] defaultValue){
return getWithTransformer(
parameterName, defaultValue, s2sa
);
}
/**
*
*
*/
public > T getEnum(String parameterName, T defaultValue, Class clazz){
return getWithTransformer(
parameterName, defaultValue, new StringToEnumTransformer(clazz)
);
}
/**
*
*
*/
public > T getEnum(String parameterName, Class clazz){
return getWithTransformer(
parameterName, new StringToEnumTransformer(clazz)
);
}
/**
*
*
*/
public T getWithTransformer(
String parameterName, T defaultValue
, Transformer transformer)
{
String v = getValue(parameterName);
if(v == null){
return defaultValue;
}
try{
return transformer.transform(v);
} catch(TransformationException e){
logger.log(
Level.WARNING, "failed to convert parameter. name:\""
+ parameterName + "\" value:\"" + v + "\"", e
);
return defaultValue;
}
}
/**
*
*
*/
public T getWithTransformer(
String parameterName, Supplier supplier
, Transformer transformer)
{
String v = getValue(parameterName);
if(v == null){
return supplier.get();
}
try{
return transformer.transform(v);
} catch(TransformationException e){
logger.log(
Level.WARNING, "failed to convert parameter. name:\""
+ parameterName + "\" value:\"" + v + "\"", e
);
return supplier.get();
}
}
/**
*
*
*/
public T getWithTransformer(
String parameterName
, Transformer transformer)
{
String v = getValue(parameterName);
if(v == null) return null;
try{
return transformer.transform(v);
} catch(TransformationException e){
logger.log(
Level.WARNING, "failed to convert parameter. name:\""
+ parameterName + "\" value:\"" + v + "\"", e
);
return null;
}
}
/**
*
*
*/
public void load(Object bean)
throws ParameterRequiredException{
loader.load(bean, this);
}
private ParameterLoader loader = new ParameterLoader();
private static Transformer s2b = new StringToBooleanTransformer();
private static Transformer s2i = new StringToIntegerTransformer();
private static Transformer s2l = new StringToLongTransformer();
private static Transformer s2f = new StringToFloatTransformer();
private static Transformer s2d = new StringToDoubleTransformer();
private static Transformer s2s = new PassthroughTransformer();
private static Transformer s2sa = new StringSplittingTransformer(",");
private static Logger logger = Logger.getLogger(ParameterContext.class.getName());
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy