Add yang generated packages in .gitignore
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / resolver / validator / ValidationResult.java
1 package org.opendaylight.groupbasedpolicy.resolver.validator;
2
3 /*
4  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
5  *
6  * This program and the accompanying materials are made available under the
7  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
8  * and is available at http://www.eclipse.org/legal/epl-v10.html
9  */
10 import java.util.ArrayList;
11 import java.util.List;
12
13 /**
14  * Class to hold validation results of object and his children.
15  *
16  */
17 public class ValidationResult {
18
19     /**
20      * Enumeration of possible results.
21      */
22     public enum Result {
23
24         /**
25          * Everything is OK
26          */
27         SUCCESS(true),
28         /**
29          * Base validation failed
30          */
31         FAIL_BASE(false),
32         /**
33          * Validation of (any) child failed
34          */
35         FAIL_CHILD(false),
36         /**
37          * Base validation and validation of (any) child failed
38          */
39         FAIL_BASE_AND_CHILD(false);
40
41         private boolean value;
42
43         Result(boolean value) {
44             this.value = value;
45         }
46
47         public boolean getValue() {
48             return this.value;
49         }
50     }
51
52     /**
53      * Variable to store result of validation. The final result is based on base
54      * status and status of all children.
55      */
56     private Result result = Result.SUCCESS;
57
58     /**
59      * Class of {@link Validator}, which returns result.
60      */
61     private final Class<? extends Validator> validatorClass;
62
63     /**
64      * List of all children validations.
65      */
66     private List<ValidationResult> childResults = new ArrayList<>();
67
68     /**
69      * Human-readable description of current status.
70      */
71     private String description;
72
73     /**
74      * Create new {@link ValidationResult} to store result of validation.
75      *
76      * @param validatorClass Creator of {@link ValidationResult}
77      */
78     public ValidationResult(Class<? extends Validator> validatorClass) {
79         this.validatorClass = validatorClass;
80     }
81
82     /**
83      *
84      * @return Current result
85      */
86     public Result getResult() {
87         return result;
88     }
89
90     /**
91      *
92      * @param result Result to set
93      */
94     public void setResult(Result result) {
95         this.result = result;
96     }
97
98     /**
99      *
100      * @return List of result child objects
101      */
102     public List<ValidationResult> getChildResults() {
103         return childResults;
104     }
105
106     /**
107      * Add new child result. Result of his parent is based on status of base and
108      * all children.
109      *
110      * @param childResult
111      */
112     public void addChildResult(ValidationResult childResult) {
113         if (!childResult.getResult().getValue()) {
114
115             //if validation already failed for base or child
116             if (this.getResult().equals(ValidationResult.Result.FAIL_BASE)
117                     || this.getResult().equals(ValidationResult.Result.FAIL_BASE_AND_CHILD)) {
118                 this.setResult(ValidationResult.Result.FAIL_BASE_AND_CHILD);
119
120                 //if validation failed only for child
121             } else {
122                 this.setResult(ValidationResult.Result.FAIL_CHILD);
123             }
124         }
125
126         this.childResults.add(childResult);
127     }
128
129     /**
130      * Returns {@link Validator} class in which the result arises.
131      *
132      * @return {@link Validator} class
133      */
134     public Class<? extends Validator> getValidatorClass() {
135         return validatorClass;
136     }
137
138     /**
139      *
140      * @return Current result description
141      */
142     public String getDescription() {
143         return description;
144     }
145
146     /**
147      * Set human-readable description of result.
148      *
149      * @param description Result description to set
150      */
151     public void setDescription(String description) {
152         this.description = description;
153     }
154
155 }