
org.nuiton.validator.NuitonValidatorResult Maven / Gradle / Ivy
The newest version!
/*
* #%L
* Validation :: API
* %%
* Copyright (C) 2021 - 2024 Ultreia.io
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* .
* #L%
*/
package org.nuiton.validator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
/**
* Contains validation messages coming from the method
* {@link NuitonValidator#validate(Object, NuitonValidationContext)}.
*
* @author Tony Chemit - [email protected]
* @since 2.0
*/
public class NuitonValidatorResult {
protected EnumMap>> messages;
protected Map> tagValues;
public static class FieldMap extends TreeMap {
private static final long serialVersionUID = 1L;
}
public boolean isValid() {
return !hasFatalMessages() && !hasErrorMessagess();
}
public void clear() {
messages.clear();
}
public boolean isEmpty() {
return messages.isEmpty();
}
public boolean hasMessagesForScope(NuitonValidatorScope scope) {
boolean result = false;
if (messages != null) {
FieldMap> map = messages.get(scope);
result = map != null && !map.isEmpty();
}
return result;
}
public boolean hasMessagesForScope(String field,
NuitonValidatorScope scope) {
boolean result = false;
if (messages != null) {
result = messages.containsKey(scope);
if (result) {
FieldMap> fieldMap = messages.get(scope);
result = fieldMap != null && fieldMap.containsKey(field);
}
}
return result;
}
public boolean hasFatalMessages() {
boolean result = hasMessagesForScope(NuitonValidatorScope.FATAL);
return result;
}
public boolean hasErrorMessagess() {
boolean result = hasMessagesForScope(NuitonValidatorScope.ERROR);
return result;
}
public boolean hasInfoMessages() {
boolean result = hasMessagesForScope(NuitonValidatorScope.INFO);
return result;
}
public boolean hasWarningMessages() {
boolean result = hasMessagesForScope(NuitonValidatorScope.WARNING);
return result;
}
public void addMessagesForScope(NuitonValidatorScope scope,
Map> newMessages) {
if (messages == null) {
messages = new EnumMap<>(NuitonValidatorScope.class);
}
FieldMap> fieldMap = messages.get(scope);
if (fieldMap == null) {
fieldMap = new FieldMap<>();
messages.put(scope, fieldMap);
}
for (Map.Entry> entry : newMessages.entrySet()) {
String fieldName = entry.getKey();
List messages = entry.getValue();
List oldMessages = fieldMap.get(fieldName);
if (oldMessages == null) {
oldMessages = messages;
fieldMap.put(fieldName, oldMessages);
} else {
oldMessages.addAll(messages);
}
}
}
public void setMessagesForScope(NuitonValidatorScope scope,
String field,
List messages) {
if (this.messages == null) {
this.messages = new EnumMap<>(NuitonValidatorScope.class);
}
FieldMap> fieldMap = this.messages.get(scope);
if (fieldMap == null) {
fieldMap = new FieldMap<>();
this.messages.put(scope, fieldMap);
}
fieldMap.put(field, messages);
}
public List getMessagesForScope(NuitonValidatorScope scope) {
List result = new ArrayList<>();
if (messages != null) {
FieldMap> fieldMap = messages.get(scope);
for (List messages : fieldMap.values()) {
result.addAll(messages);
}
}
return result;
}
public List getMessagesForScope(String field,
NuitonValidatorScope scope) {
List result = null;
if (messages != null) {
FieldMap> fieldMap = messages.get(scope);
result = fieldMap.get(field);
}
if (result == null) {
result = Collections.emptyList();
}
return result;
}
public List getFatalMessages(String field) {
List result =
getMessagesForScope(field, NuitonValidatorScope.FATAL);
return result;
}
public List getErrorMessages(String field) {
List result =
getMessagesForScope(field, NuitonValidatorScope.ERROR);
return result;
}
public List getInfoMessages(String field) {
List result =
getMessagesForScope(field, NuitonValidatorScope.INFO);
return result;
}
public List getWarningMessages(String field) {
List result =
getMessagesForScope(field, NuitonValidatorScope.WARNING);
return result;
}
public Map getTagValues(String field) {
Map result = null;
if (tagValues != null) {
result = tagValues.get(field);
}
if (result == null) {
result = Collections.emptyMap();
}
return result;
}
public List getFieldsForScope(NuitonValidatorScope scope) {
List result = null;
if (messages != null) {
FieldMap> fieldMap = messages.get(scope);
if (fieldMap != null) {
result = new ArrayList<>(fieldMap.keySet());
}
}
if (result == null) {
result = Collections.emptyList();
}
return result;
}
public List getFieldsForFatal() {
List result = getFieldsForScope(NuitonValidatorScope.FATAL);
return result;
}
public List getFieldsForError() {
List result = getFieldsForScope(NuitonValidatorScope.ERROR);
return result;
}
public List getFieldsForInfo() {
List result = getFieldsForScope(NuitonValidatorScope.INFO);
return result;
}
public List getFieldsForWarning() {
List result = getFieldsForScope(NuitonValidatorScope.WARNING);
return result;
}
public void clearMessagesForScope(NuitonValidatorScope scope) {
if (messages != null) {
messages.remove(scope);
}
}
public NuitonValidatorScope getFieldHighestScope(String field) {
if (messages == null) {
return null;
}
if (containsField(field, NuitonValidatorScope.FATAL)) {
return NuitonValidatorScope.FATAL;
}
if (containsField(field, NuitonValidatorScope.ERROR)) {
return NuitonValidatorScope.ERROR;
}
if (containsField(field, NuitonValidatorScope.WARNING)) {
return NuitonValidatorScope.WARNING;
}
if (containsField(field, NuitonValidatorScope.INFO)) {
return NuitonValidatorScope.INFO;
}
// no scope for the field
return null;
}
public NuitonValidatorScope[] getFieldScopes(String field) {
Set result = new HashSet<>();
if (messages != null) {
if (containsField(field, NuitonValidatorScope.FATAL)) {
result.add(NuitonValidatorScope.FATAL);
}
if (containsField(field, NuitonValidatorScope.ERROR)) {
result.add(NuitonValidatorScope.ERROR);
}
if (containsField(field, NuitonValidatorScope.WARNING)) {
result.add(NuitonValidatorScope.WARNING);
}
if (containsField(field, NuitonValidatorScope.INFO)) {
result.add(NuitonValidatorScope.INFO);
}
}
return result.toArray(new NuitonValidatorScope[result.size()]);
}
protected boolean containsField(String field, NuitonValidatorScope scope) {
FieldMap> fieldMap = messages.get(scope);
return fieldMap != null && fieldMap.containsKey(field);
}
protected EnumMap>> getMessages() {
return messages;
}
protected Map> getTagValues() {
return tagValues;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy