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