Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.oic.utils; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.json.simple.JSONObject; /** * * @author b2020 */ public class Validators { private final JSONObject json; private final JSONObject errorJson; private final Map<String, validationType> validationRule; private int maxLength; private int minLength; public enum validationType { REQUIRED, STUDENTID, MAXLENGTH, MINLENGTH, INTEGERTYPE, BIRTHDAY, LONGTYPE }; public Validators(JSONObject json) { validationRule = new HashMap<>(); errorJson = new JSONObject(); this.json = json; } /** * Validation * * @param key * @param types */ public void add(String key, validationType... types) { for (validationType type : types) { validationRule.put(key, type); } } public boolean validate() { boolean error = false; for (Map.Entry<String, validationType> e : validationRule.entrySet()) { errorJson.put(e.getKey(), "1"); try { String data = json.get(e.getKey()).toString(); Pattern pattern; Matcher matcher; switch (e.getValue()) { //? case REQUIRED: if (data.length() < 1) { throw new NullPointerException(); } break; case STUDENTID: pattern = Pattern.compile("^[a-zA-Z][0-9]{4}$"); matcher = pattern.matcher(data); if (!matcher.find()) { throw new NullPointerException(); } break; case MAXLENGTH: if (data.getBytes().length > maxLength) { throw new NullPointerException(); } break; case MINLENGTH: if (data.getBytes().length < minLength) { throw new NullPointerException(); } break; case INTEGERTYPE: int i = Integer.parseInt(data); i++; break; case LONGTYPE: long l = Long.parseLong(data); l++; break; case BIRTHDAY: pattern = Pattern.compile( "^([1-9][0-9]{3})-(([1-9]{1}|1[0-2]{1})|0[1-9])-([1-9]{1}|[1-2]{1}[0-9]{1}|3[0-1]{1}|0[1-9])$"); matcher = pattern.matcher(data); if (!matcher.find()) { throw new NullPointerException(); } break; } errorJson.remove(e.getKey()); } catch (Exception ex) { error = true; } } if (error == true) { return false; } else { return true; } } public JSONObject getError() { return errorJson; } /* validation type */ /** * ? * * @return */ public validationType required() { return validationType.REQUIRED; } /** * ?? * * @return */ public validationType studentId() { return validationType.STUDENTID; } /** * ? ?2????? * * @param maxLength * @return */ public validationType maxLength(int maxLength) { this.maxLength = maxLength; return validationType.MAXLENGTH; } /** * ? ?2????? * * @param minLength * @return */ public validationType minLength(int minLength) { this.minLength = minLength; return validationType.MINLENGTH; } public validationType integerType() { return validationType.INTEGERTYPE; } public validationType longType() { return validationType.LONGTYPE; } public validationType birthday() { return validationType.BIRTHDAY; } }