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