Created
February 16, 2018 15:20
-
-
Save MorbosVermin/db68106c868af329201afa63eda57112 to your computer and use it in GitHub Desktop.
Validation using an Enum type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.text.ParseException; | |
public class TestClass { | |
static class Parameter { | |
private String name; | |
private Object value; | |
public Parameter(String name, Object value) { | |
this.name = name; | |
this.value = value; | |
} | |
public String getName() { | |
return name; | |
} | |
public Object getValue() { | |
return value; | |
} | |
} | |
enum Validation { | |
STRING, | |
INTEGER, | |
BOOLEAN, | |
DOUBLE, | |
DATETIME, | |
DATE, | |
TIME; | |
public void validate(Object data, Parameter ...p) throws ParseException { | |
switch(this) { | |
case STRING: | |
for(Parameter param : p) { | |
if(param.getName().equals("regex")) { | |
if(!data.toString().matches(param.getValue().toString())) | |
throw new ParseException("Validation failed: invalid format given as a string.", 1); | |
}else if(param.getName().equals("min")) { | |
if(data.toString().length() < Integer.parseInt(param.getValue() +"")) | |
throw new ParseException("Validation failed: string was less than minimal length required.", 1); | |
}else if(param.getName().equals("max")) { | |
if(data.toString().length() > Integer.parseInt(param.getValue() +"")) | |
throw new ParseException("Validation failed: string was less than minimal length required.", 1); | |
}else if(param.getName().equals("possibleValues")) { | |
String[] pValues = (String[])param.getValue(); | |
boolean found = false; | |
for(String pValue : pValues) { | |
if(pValue.equals(data.toString())) { | |
found = true; | |
break; | |
} | |
} | |
if(!found) | |
throw new ParseException("Validation failed: string given was not in the list of good values.", 1); | |
} | |
} | |
break; | |
case INTEGER: | |
int v = -1; | |
try { | |
v = Integer.parseInt(data.toString()); | |
}catch(NumberFormatException e) { | |
throw new ParseException("Validation failed: invalid numeric format.", 1); | |
} | |
for(Parameter param : p) { | |
if(param.getName().equals("min") && (v < Integer.parseInt(param.getValue() +""))) | |
throw new ParseException("Validation failed: number is less than the minimum allowed.", 1); | |
else if(param.getName().equals("max") && (v > Integer.parseInt(param.getValue() +""))) | |
throw new ParseException("Validation failed: number is greater the maximum allowed.", 1); | |
} | |
break; | |
case BOOLEAN: | |
if(!data.toString().equals("0") && | |
!data.toString().equals("1") && | |
!data.toString().equalsIgnoreCase("true") && | |
!data.toString().equalsIgnoreCase("false")) { | |
throw new ParseException("Validation failed: invalid boolean format.", 1); | |
} | |
break; | |
//TODO More data-type validations here. | |
} | |
} | |
} | |
public static void main(String[] args) { | |
try { | |
Validation.STRING.validate(args[0], new Parameter[] { new Parameter("min", 1), new Parameter("max", 100) }); | |
} catch (ParseException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment