GBP core tests improvements
[groupbasedpolicy.git] / groupbasedpolicy / src / main / java / org / opendaylight / groupbasedpolicy / dto / ValidationResultBuilder.java
1 /*\r
2  * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.\r
3  *\r
4  * This program and the accompanying materials are made available under the\r
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
6  * and is available at http://www.eclipse.org/legal/epl-v10.html\r
7  */\r
8 package org.opendaylight.groupbasedpolicy.dto;\r
9 \r
10 \r
11 import javax.annotation.Nonnull;\r
12 \r
13 import org.opendaylight.groupbasedpolicy.api.ValidationResult;\r
14 import org.opendaylight.yangtools.concepts.Builder;\r
15 \r
16 public final class ValidationResultBuilder implements Builder<ValidationResult> {\r
17 \r
18     private static final class ValidationResultImpl implements ValidationResult {\r
19 \r
20         private final boolean success;\r
21         private final String message;\r
22 \r
23         private ValidationResultImpl(final boolean success, final String message) {\r
24             this.success = success;\r
25             this.message = message;\r
26         }\r
27 \r
28         @Override\r
29         public boolean isValid() {\r
30             return success;\r
31         }\r
32 \r
33         @Override\r
34         public String getMessage() {\r
35             return message;\r
36         }\r
37 \r
38         @Override\r
39         public boolean equals(Object o) {\r
40             if (this == o)\r
41                 return true;\r
42             if (o == null || getClass() != o.getClass())\r
43                 return false;\r
44 \r
45             ValidationResultImpl that = (ValidationResultImpl) o;\r
46 \r
47             if (success != that.success)\r
48                 return false;\r
49             return message.equals(that.message);\r
50         }\r
51 \r
52         @Override\r
53         public int hashCode() {\r
54             int result = (success ? 1 : 0);\r
55             result = 31 * result + message.hashCode();\r
56             return result;\r
57         }\r
58     }\r
59 \r
60     private boolean success;\r
61     private String message = "";\r
62 \r
63     public ValidationResultBuilder success() {\r
64         this.success = true;\r
65         return this;\r
66     }\r
67 \r
68     /**\r
69      * Returns a builder for a failed result.\r
70      */\r
71     public ValidationResultBuilder failed() {\r
72         this.success = false;\r
73         return this;\r
74     }\r
75 \r
76     /**\r
77      * Returns a builder for a failed result.\r
78      *\r
79      * @param message brief explanation\r
80      * @throws IllegalArgumentException when message is null\r
81      */\r
82     public ValidationResultBuilder setMessage(@Nonnull String message) {\r
83         if (message == null) {\r
84             throw new IllegalArgumentException("Result message cannot be set to NULL!");\r
85         }\r
86         this.message = message;\r
87         return this;\r
88     }\r
89 \r
90     @Override\r
91     public ValidationResult build() {\r
92         return new ValidationResultImpl(success, message);\r
93     }\r
94 \r
95 }\r