/** * Transform the given field into its full path, * regarding the nested path of this instance. */ protected String fixedField(@Nullable String field) { if (StringUtils.hasLength(field)) { return getNestedPath() + canonicalFieldName(field); } else { String path = getNestedPath(); return (path.endsWith(Errors.NESTED_PATH_SEPARATOR) ? path.substring(0, path.length() - NESTED_PATH_SEPARATOR.length()) : path); } }
@Override public void pushNestedPath(String subPath) { this.nestedPathStack.push(getNestedPath()); doSetNestedPath(getNestedPath() + subPath); }
@Override public List<FieldError> getFieldErrors(String field) { List<FieldError> fieldErrors = getFieldErrors(); List<FieldError> result = new LinkedList<>(); String fixedField = fixedField(field); for (FieldError error : fieldErrors) { if (isMatchingFieldError(fixedField, error)) { result.add(error); } } return Collections.unmodifiableList(result); }
@Override public List<ObjectError> getAllErrors() { List<ObjectError> result = new LinkedList<>(); result.addAll(getGlobalErrors()); result.addAll(getFieldErrors()); return Collections.unmodifiableList(result); }
@Override public int getFieldErrorCount(String field) { return getFieldErrors(field).size(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(getClass().getName()); sb.append(": ").append(getErrorCount()).append(" errors"); for (ObjectError error : getAllErrors()) { sb.append('\n').append(error); } return sb.toString(); }
@Override public void setNestedPath(@Nullable String nestedPath) { doSetNestedPath(nestedPath); this.nestedPathStack.clear(); }
@Override public boolean hasErrors() { return !getAllErrors().isEmpty(); }
@Override public boolean hasFieldErrors() { return (getFieldErrorCount() > 0); }
@Override public int getGlobalErrorCount() { return getGlobalErrors().size(); }
/** * Actually set the nested path. * Delegated to by setNestedPath and pushNestedPath. */ protected void doSetNestedPath(@Nullable String nestedPath) { if (nestedPath == null) { nestedPath = ""; } nestedPath = canonicalFieldName(nestedPath); if (nestedPath.length() > 0 && !nestedPath.endsWith(Errors.NESTED_PATH_SEPARATOR)) { nestedPath += Errors.NESTED_PATH_SEPARATOR; } this.nestedPath = nestedPath; }
@Override public boolean hasGlobalErrors() { return (getGlobalErrorCount() > 0); }
@Override @Nullable public Class<?> getFieldType(String field) { Object value = getFieldValue(field); return (value != null ? value.getClass() : null); }
@Override public int getFieldErrorCount() { return getFieldErrors().size(); }
@Override public String toString() { StringBuilder sb = new StringBuilder(getClass().getName()); sb.append(": ").append(getErrorCount()).append(" errors"); for (ObjectError error : getAllErrors()) { sb.append('\n').append(error); } return sb.toString(); }
@Override public List<ObjectError> getAllErrors() { List<ObjectError> result = new LinkedList<>(); result.addAll(getGlobalErrors()); result.addAll(getFieldErrors()); return Collections.unmodifiableList(result); }
@Override public void popNestedPath() throws IllegalStateException { try { String formerNestedPath = this.nestedPathStack.pop(); doSetNestedPath(formerNestedPath); } catch (NoSuchElementException ex) { throw new IllegalStateException("Cannot pop nested path: no nested path on stack"); } }
@Override public int getErrorCount() { return getAllErrors().size(); }
@Override public boolean hasFieldErrors(String field) { return (getFieldErrorCount(field) > 0); }
@Override @Nullable public ObjectError getGlobalError() { List<ObjectError> globalErrors = getGlobalErrors(); return (!globalErrors.isEmpty() ? globalErrors.get(0) : null); }