All Downloads are FREE. Search and download functionalities are using the official Maven repository.

colesico.framework.validation.ValidationIssue Maven / Gradle / Ivy

There is a newer version: 5.3.0
Show newest version
/*
 * Copyright © 2014-2020 Vladlen V. Larionov and others as noted.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package colesico.framework.validation;

import java.io.Serializable;
import java.util.*;

/**
 * Describes a validation issue
 */
public class ValidationIssue implements Serializable {

    /**
     * Validation subject name
     */
    private String subject;

    /**
     * Validation errors list
     */
    private List errors = new ArrayList<>();

    /**
     * Nested issues if the subject is complex object.
     * Map Subject -> Issue
     */
    private Map subissues = new HashMap<>();

    public ValidationIssue() {
    }

    public ValidationIssue(String subject) {
        this.subject = subject;
    }

    public boolean hasErrors() {
        return !errors.isEmpty();
    }

    public boolean hasSubissues() {
        return !subissues.isEmpty();
    }

    public ValidationIssue getSubissue(String subject) {
        return subissues.get(subject);
    }

    public String getSubject() {
        return subject;
    }

    public void addError(ValidationError errorMessage) {
        errors.add(errorMessage);
    }

    public void addSubissue(ValidationIssue subissue) {
        subissues.put(subissue.getSubject(), subissue);
    }

    public List getErrors() {
        return Collections.unmodifiableList(errors);
    }

    public Map getSubissues() {
        return Collections.unmodifiableMap(subissues);
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public void setErrors(List errors) {
        this.errors = errors;
    }

    public void setSubissues(Map subissues) {
        this.subissues = subissues;
    }

    @Override
    public String toString() {
        StringBuilder eb = new StringBuilder();
        for (ValidationError e : errors) {
            eb.append(e.toString()).append(",\n");
        }

        StringBuilder ib = new StringBuilder();
        for (ValidationIssue vi : subissues.values()) {
            ib.append(vi.toString()).append(",\n");
        }

        return "ValidationIssue : {" +
                "  subject: '" + subject + "',\n" +
                "  errors: {\n" +
                eb.toString() +
                "  \n},\n" +
                "  subissues: {\n" +
                ib.toString() +
                "  \n}\n" +
                '}';
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy