Merge "Refactor frontend JS"
[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 and
14  * 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. This is used as return code
22      * for internal API2 function calls. This constructor allows to specify,
23      * beside the Status Code, a custom human readable description to add more
24      * information about the status.
25      * 
26      * @param errorCode
27      *            The status code. If passed as null, code will be stored as
28      *            {@code StatusCode.UNDEFINED}
29      * @param description
30      *            The human readable description of the status. If passed as
31      *            null, description will be inferred by the code
32      */
33     public Status(StatusCode errorCode, String description) {
34         this.code = (errorCode != null) ? errorCode : StatusCode.UNDEFINED;
35         this.description = (description != null) ? description : this.code
36                 .toString();
37     }
38
39     /**
40      * Generates an instance of the Status class based on the passed StatusCode
41      * only. The description field of the Status object will be inferred by the
42      * status code.
43      * 
44      * @param errorCode
45      *            The status code. If passed as null, code will be stored as
46      *            {@code StatusCode.UNDEFINED}
47      */
48     public Status(StatusCode errorCode) {
49         this.code = (errorCode != null) ? errorCode : StatusCode.UNDEFINED;
50         this.description = (description != null) ? description : this.code
51                 .toString();
52     }
53
54     /**
55      * Returns the status code
56      * 
57      * @return the {@code StatusCode} representing the status code
58      */
59     public StatusCode getCode() {
60         return code;
61     }
62
63     /**
64      * Returns a human readable description of the failure if any
65      * 
66      * @return a string representing the reason of failure
67      */
68     public String getDescription() {
69         return description;
70     }
71
72     /**
73      * Tells whether the status is successful
74      * 
75      * @return true if the Status code is {@code StatusCode.SUCCESS}
76      */
77     public boolean isSuccess() {
78         return code == StatusCode.SUCCESS;
79     }
80
81     @Override
82     public String toString() {
83         return code + ": " + description;
84     }
85
86     @Override
87     public int hashCode() {
88         final int prime = 31;
89         int result = 1;
90         result = prime * result + ((code == null) ? 0 : code.hashCode());
91         return result;
92     }
93
94     @Override
95     public boolean equals(Object obj) {
96         if (this == obj)
97             return true;
98         if (obj == null)
99             return false;
100         if (getClass() != obj.getClass())
101             return false;
102         Status other = (Status) obj;
103         if (code != other.code)
104             return false;
105         return true;
106     }
107 }