9bd98ac1aba6acd38f162b1766e94787f4f3fe74
[controller.git] / opendaylight / sal / api / src / main / java / org / opendaylight / controller / sal / utils / Status.java
1 package org.opendaylight.controller.sal.utils;
2
3 /**
4  * Represents the return object of the osgi service interfaces function calls.
5  * It contains a code {@code StatusCode} representing the result of the call
6  * and a string which describes a failure reason (if any) in human readable form.
7  */
8 public class Status {
9         StatusCode code;
10         String description;
11         
12         /**
13          * Generates an instance of the Status class.
14          * 
15          * @param errorCode The status code. If passed as null, code will be 
16          * stored as {@code StatusCode.UNDEFINED}
17          * @param description The human readable description of the status. If passed
18          * as null, description will be inferred by the code
19          */
20         public Status(StatusCode errorCode, String description) {
21                 this.code = (errorCode != null)? errorCode : StatusCode.UNDEFINED;
22                 this.description = (description != null)? description : this.code.toString();
23         }
24         
25         /**
26          * Returns the status code
27          * @return the {@code StatusCode} representing the status code 
28          */
29         public StatusCode getCode() {
30                 return code;
31         }
32         
33         /**
34          * Returns a human readable description of the failure if any
35          * @return a string representing the reason of failure
36          */
37         public String getDescription() {
38                 return description;
39         }
40         
41         /**
42          * Tells whether the status is successful
43          * @return true if the Status code is {@code StatusCode.SUCCESS}
44          */
45         public boolean isSuccess() {
46                 return code == StatusCode.SUCCESS;
47         }
48         
49         @Override
50         public String toString() {
51                 return code + ": " + description;
52         }
53
54         @Override
55         public int hashCode() {
56                 final int prime = 31;
57                 int result = 1;
58                 result = prime * result + ((code == null) ? 0 : code.hashCode());
59                 return result;
60         }
61
62         @Override
63         public boolean equals(Object obj) {
64                 if (this == obj)
65                         return true;
66                 if (obj == null)
67                         return false;
68                 if (getClass() != obj.getClass())
69                         return false;
70                 Status other = (Status) obj;
71                 if (code != other.code)
72                         return false;
73                 return true;
74         }
75 }