1 /**
2  *  Copyright Terracotta, Inc.
3  *
4  *  Licensed under the Apache License, Version 2.0 (the "License");
5  *  you may not use this file except in compliance with the License.
6  *  You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  */

16
17 package net.sf.ehcache;
18
19 import java.io.Serializable;
20
21 /**
22  * A pre JDK1.5 compatible enum class to indicate the status of a {@link CacheManager} or {@link Cache}.
23  * <p/>
24  * ehcache historically used int values for status. This is unsuitable for third party use thus this class.
25  * Methods are provided to convert from the int status values to enum values and vice versa.
26  *
27  * @author Greg Luck
28  * @version $Id: Status.java 5631 2012-05-10 08:31:33Z teck $
29  * @since 1.2
30  */

31 public final class Status implements Serializable {
32     /**
33      * The cache is uninitialised. It cannot be used.
34      */

35     public static final Status STATUS_UNINITIALISED = new Status(0, "STATUS_UNINITIALISED");
36     /**
37      * The cache is alive. It can be used.
38      */

39     public static final Status STATUS_ALIVE = new Status(1, "STATUS_ALIVE");
40     /**
41      * The cache is shudown. It cannot be used.
42      */

43     public static final Status STATUS_SHUTDOWN = new Status(2, "STATUS_SHUTDOWN");
44
45     private static final long serialVersionUID = 2732730630423367732L;
46
47     private static final Status[] STATUSES = {STATUS_UNINITIALISED, STATUS_ALIVE, STATUS_SHUTDOWN};
48
49     private final String name;
50     private final int intValue;
51
52     private Status(int intValue, String name) {
53         this.intValue = intValue;
54         this.name = name;
55
56     }
57
58     /**
59      * Returns a string representation of the object. In general, the
60      * <code>toString</code> method returns a string that
61      * "textually represents" this object. The result should
62      * be a concise but informative representation that is easy for a
63      * person to read.
64      * It is recommended that all subclasses override this method.
65      * <p/>
66      * The <code>toString</code> method for class <code>Object</code>
67      * returns a string consisting of the name of the class of which the
68      * object is an instance, the at-sign character `<code>@</code>', and
69      * the unsigned hexadecimal representation of the hash code of the
70      * object. In other words, this method returns a string equal to the
71      * value of:
72      * <blockquote>
73      * <pre>
74      * getClass().getName() + '@' + Integer.toHexString(hashCode())
75      * </pre></blockquote>
76      *
77      * @return a string representation of the object.
78      */

79     public String toString() {
80         return name;
81     }
82
83     /**
84      * @param statusAsInt an int argument between 1 and 3.
85      * @return an enum Status
86      * @throws IllegalArgumentException if the argument is not between 1 and 3
87      */

88     public static Status convertIntToStatus(int statusAsInt) throws IllegalArgumentException {
89         if ((statusAsInt < STATUS_UNINITIALISED.intValue) || (statusAsInt > STATUS_SHUTDOWN.intValue)) {
90             throw new IllegalArgumentException("int value of statuses must be between 1 and three");
91         }
92         return STATUSES[statusAsInt];
93     }
94
95     /**
96      * Returns the int value of status, for backward compatibility with ehcache versions below 1.2
97      * @return the int value of this status.
98      */

99     public int intValue() {
100         return intValue;
101     }
102
103     /**
104      * Indicates whether some other object is "equal to" this one.
105      * <p/>
106      * The <code>equals</code> method implements an equivalence relation
107      * on non-null object references:
108      * <ul>
109      * <li>It is <i>reflexive</i>: for any non-null reference value
110      * <code>x</code>, <code>x.equals(x)</code> should return
111      * <code>true</code>.
112      * <li>It is <i>symmetric</i>: for any non-null reference values
113      * <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
114      * should return <code>true</code> if and only if
115      * <code>y.equals(x)</code> returns <code>true</code>.
116      * <li>It is <i>transitive</i>: for any non-null reference values
117      * <code>x</code>, <code>y</code>, and <code>z</code>, if
118      * <code>x.equals(y)</code> returns <code>true</code> and
119      * <code>y.equals(z)</code> returns <code>true</code>, then
120      * <code>x.equals(z)</code> should return <code>true</code>.
121      * <li>It is <i>consistent</i>: for any non-null reference values
122      * <code>x</code> and <code>y</code>, multiple invocations of
123      * <tt>x.equals(y)</tt> consistently return <code>true</code>
124      * or consistently return <code>false</code>, provided no
125      * information used in <code>equals</code> comparisons on the
126      * objects is modified.
127      * <li>For any non-null reference value <code>x</code>,
128      * <code>x.equals(null)</code> should return <code>false</code>.
129      * </ul>
130      * <p/>
131      * The <tt>equals</tt> method for class <code>Object</code> implements
132      * the most discriminating possible equivalence relation on objects;
133      * that is, for any non-null reference values <code>x</code> and
134      * <code>y</code>, this method returns <code>true</code> if and only
135      * if <code>x</code> and <code>y</code> refer to the same object
136      * (<code>x == y</code> has the value <code>true</code>).
137      * <p/>
138      * Note that it is generally necessary to override the <tt>hashCode</tt>
139      * method whenever this method is overridden, so as to maintain the
140      * general contract for the <tt>hashCode</tt> method, which states
141      * that equal objects must have equal hash codes.
142      *
143      * @param object the reference object with which to compare.
144      * @return <code>true</code> if this object is the same as the obj
145      *         argument; <code>false</code> otherwise.
146      * @see #hashCode()
147      * @see java.util.Hashtable
148      */

149     public boolean equals(Object object) {
150         if (!(object instanceof Status)) {
151             return false;
152         }
153         return ((Status) object).intValue == intValue;
154     }
155
156     /**
157      * Equality checker when the comparison object is of the same type.
158      * @param status the status to check
159      * @return true is the statuses are the same
160      */

161     public boolean equals(Status status) {
162         if (status == null) {
163             return false;
164         } else {
165             return (intValue == status.intValue);
166         }
167     }
168
169     /**
170      * Returns a hash code value for Status. It is the underlying int value of the status.
171      * @return a hash code value for this object.
172      * @see Object#hashCode()
173      * @see java.util.Hashtable
174      */

175     public int hashCode() {
176         return intValue;
177     }
178
179 }
180