27e4f63b08b0992327e55b148f09c1b74a5fe2a9
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / resolver / validator / SimpleResult.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 package org.opendaylight.groupbasedpolicy.resolver.validator;
9
10 /**
11  * Class that represents simple result (e.g. SUCCESS/FAILURE and description of
12  * that result). Provide option to save custom result (state/code), which can be
13  * evaluated by other processes.
14  *
15  * @see SimpleResult#code
16  *
17  */
18 public class SimpleResult {
19
20     /**
21      * Code of result.
22      */
23     private final int code;
24     /**
25      * Description of result.
26      */
27     private final String description;
28
29     /**
30      * Construct new {@link SimpleResult}.
31      *
32      * @param result State of result ({@code true} on success and {@code false}
33      * on failure).
34      * @param description Description of result.
35      */
36     public SimpleResult(boolean result, String description) {
37         this.code = result ? 0 : 1;
38         this.description = description;
39     }
40
41     /**
42      * Construct new {@link SimpleResult} with empty description.
43      *
44      * @param result State of result ({@code true} on SUCCESS and {@code false}
45      * on FAILURE).
46      */
47     public SimpleResult(boolean result) {
48         this(result, null);
49     }
50
51     /**
52      * Construct new {@link SimpleResult}.
53      *
54      * @param code Code of result, where 0 is expected as SUCCESS and everything
55      * else as FAILURE.
56      * @param description Description of result.
57      */
58     public SimpleResult(int code, String description) {
59         this.code = code;
60         this.description = description;
61     }
62
63     /**
64      * Construct new {@link SimpleResult} with empty description.
65      *
66      * @param code Code of result, where 0 is expected as SUCCESS and everything
67      * else as FAILURE.
68      */
69     public SimpleResult(int code) {
70         this(code, null);
71     }
72
73     /**
74      * Returns code of result.
75      *
76      * @return Code of result
77      */
78     public int getCode() {
79         return code;
80     }
81
82     /**
83      * Returns {@code true} if code of result IS {@code 0}, otherwise returns
84      * {@code false}.
85      *
86      * @return {@code true} or {@code false} based on value of {@code code}
87      */
88     public boolean isSuccess() {
89         return getCode() == 0;
90     }
91
92     /**
93      * Returns {@code true} if code of result IS NOT {@code 0}, otherwise
94      * returns {@code false}.
95      *
96      * @return {@code true} or {@code false} based on value of {@code code}
97      */
98     public boolean isFailure() {
99         return !isSuccess();
100     }
101
102     /**
103      * Returns saved description of result or empty string.
104      *
105      * @return Description of result.
106      */
107     public String getDescription() {
108         if (description == null) {
109             return "";
110         } else {
111             return description;
112         }
113     }
114
115 }