Last active
February 26, 2018 18:58
-
-
Save ntakouris/cfe7e119a2672f36b298b93563f9cfe9 to your computer and use it in GitHub Desktop.
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
public class ComponentValidator { | |
private static final ComponentValidator _instance; | |
static { | |
_instance = new ComponentValidator(); | |
} | |
private ComponentValidator() { | |
} | |
public static ComponentValidator getInstance() { | |
return _instance; | |
} | |
public Response validate(Object instance) { | |
List<AutoValidatedUserInputComponent> components = new ArrayList<>(); | |
for (Field field : instance.getClass().getDeclaredFields()) { | |
if (!field.isAnnotationPresent(ValidComponent.class)) { | |
continue; | |
} | |
Object possibleComponent = getPossibleComponent(instance, field); | |
if (possibleComponent == null) { | |
continue; | |
} | |
if (possibleComponent instanceof AutoValidatedUserInputComponent) { | |
AutoValidatedUserInputComponent component = (AutoValidatedUserInputComponent) possibleComponent; | |
if (component.hasValidInput()) { | |
continue; | |
} | |
components.add(component); | |
} | |
} | |
return new Response(components); | |
} | |
private Object getPossibleComponent(Object instance, Field field) { | |
Object possibleComponent = null; | |
try { | |
possibleComponent = field.get(instance); | |
} catch (IllegalAccessException e) { | |
e.printStackTrace(); | |
} | |
return possibleComponent; | |
} | |
public class Response{ | |
List<AutoValidatedUserInputComponent> components; | |
public Response(List<AutoValidatedUserInputComponent> components) { | |
this.components = components; | |
} | |
public boolean isValid() { | |
return components != null && components.isEmpty(); | |
} | |
public List<AutoValidatedUserInputComponent> getComponents() { | |
return components; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment