com.alogic.validator.Validator Maven / Gradle / Ivy
package com.alogic.validator;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.w3c.dom.Element;
import com.alogic.load.Loadable;
import com.anysoft.util.Configurable;
import com.anysoft.util.JsonTools;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;
import com.anysoft.util.XMLConfigurable;
import com.anysoft.util.XmlElementProperties;
import com.anysoft.util.XmlTools;
/**
* 验证器接口
* @author yyduan
* @since 1.6.12.11 [20181206 duanyy]
*/
public interface Validator extends Loadable{
/**
* 对指定的值进行验证检查
* @param value 指定的值
* @param throwException 是否抛出异常
* @return 是否通过检查
*/
public boolean check(String value,boolean throwException);
/**
* 获取异常代码
*/
public String getCode();
/**
* 获取异常消息
* @return 异常消息
*/
public String getMessage();
/**
* 虚基类
* @author yyduan
*
*/
public abstract static class Abstract implements Validator,XMLConfigurable,Configurable{
/**
* a logger of slf4j
*/
protected static final Logger LOG = LoggerFactory.getLogger(Validator.class);
/**
* id
*/
protected String id;
/**
* 数据加载时间戳
*/
protected long timestamp = System.currentTimeMillis();
/**
* 数据的生存周期:30分钟
*/
public static final long TTL = 30 * 60 * 1000L;
@Override
public String getId() {
return id;
}
@Override
public long getTimestamp() {
return timestamp;
}
@Override
public boolean isExpired() {
return System.currentTimeMillis() - timestamp > TTL;
}
@Override
public void expire(){
timestamp = timestamp - TTL;
}
@Override
public void report(Element xml) {
if (xml != null){
XmlTools.setString(xml, "module", getClass().getName());
XmlTools.setString(xml,"id",getId());
}
}
@Override
public void report(Map json) {
if (json != null){
JsonTools.setString(json,"module",getClass().getName());
JsonTools.setString(json,"id",getId());
}
}
@Override
public void configure(Properties p) {
id = PropertiesConstants.getString(p,"id",id);
}
@Override
public void configure(Element e, Properties p) {
XmlElementProperties props = new XmlElementProperties(e,p);
configure(props);
}
}
}