I am trying to design validator which will validate the rules configured in config file and if any of the rules satisfied, we need to block that data.
public class AllMatchDemo {
public static void main(String[] args) {
Map<String,Object> dataMap = new HashMap<>();
dataMap.put("String","Ask");
dataMap.put("Date1", new Date());
dataMap.put("Long", new Long(100));
dataMap.put("Integer", new Integer(10));
String message = new String();
List<Validator> validators = Arrays.asList(new RegexValidator(),new DateIsBeforeTodayValidator ());
boolean result = validators.stream().anyMatch(validator -> validator.validate(dataMap,message));
System.out.println("Result :"+result);
if(result)
{
block;
}
}
}
public interface Validator {
boolean validate(Map<String,String> dataMap,String message);
}
Following are implementors :
public class DateIsBeforeTodayValidator implements Validator {
String dateFieldToBeCompared;
@Override
public boolean validate(Map<String,String> dataMap, String message) {
}
public void setDateFieldToBeCompared(String dateFieldToBeCompared) {
this.dateFieldToBeCompared = dateFieldToBeCompared;
}
}
public class RegexValidator implements Validator {
@Override
public boolean validate(IPublishable publishable) {
//validation Logic here based on match
}
}
Initially I started with RegexValidator without any Validator and later on requirement kept on increasing and I have to compare dates too so I created Validator interface and provided above 2 implementations. In future many other implementations would be needed like date1 compare to date2 , date less than, number greater than etc.
Is there any other better way to handle design so that it can be scalable in future ?
Aucun commentaire:
Enregistrer un commentaire