1 /**
2 * Copyright 2003-2007 Greg Luck
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 512 2007-07-10 09:18:45Z gregluck $
29 * @since 1.2
30 * @noinspection SerializableHasSerializationMethods
31 */
32 public final class Status implements Serializable {
33 /**
34 * The cache is uninitialised. It cannot be used.
35 */
36 public static final Status STATUS_UNINITIALISED = new Status(0, "STATUS_UNINITIALISED");
37 /**
38 * The cache is alive. It can be used.
39 */
40 public static final Status STATUS_ALIVE = new Status(1, "STATUS_ALIVE");
41 /**
42 * The cache is shudown. It cannot be used.
43 */
44 public static final Status STATUS_SHUTDOWN = new Status(2, "STATUS_SHUTDOWN");
45
46 private static final long serialVersionUID = 2732730630423367732L;
47
48 private static final Status[] STATUSES = {STATUS_UNINITIALISED, STATUS_ALIVE, STATUS_SHUTDOWN};
49
50 private final String name;
51 private final int intValue;
52
53 private Status(int intValue, String name) {
54 this.intValue = intValue;
55 this.name = name;
56
57 }
58
59 /**
60 * Returns a string representation of the object. In general, the
61 * <code>toString</code> method returns a string that
62 * "textually represents" this object. The result should
63 * be a concise but informative representation that is easy for a
64 * person to read.
65 * It is recommended that all subclasses override this method.
66 * <p/>
67 * The <code>toString</code> method for class <code>Object</code>
68 * returns a string consisting of the name of the class of which the
69 * object is an instance, the at-sign character `<code>@</code>', and
70 * the unsigned hexadecimal representation of the hash code of the
71 * object. In other words, this method returns a string equal to the
72 * value of:
73 * <blockquote>
74 * <pre>
75 * getClass().getName() + '@' + Integer.toHexString(hashCode())
76 * </pre></blockquote>
77 *
78 * @return a string representation of the object.
79 */
80 public String toString() {
81 return name;
82 }
83
84 /**
85 * @param statusAsInt an int argument between 1 and 3.
86 * @return an enum Status
87 * @throws IllegalArgumentException if the argument is not between 1 and 3
88 */
89 public static Status convertIntToStatus(int statusAsInt) throws IllegalArgumentException {
90 if ((statusAsInt < STATUS_UNINITIALISED.intValue) || (statusAsInt > STATUS_SHUTDOWN.intValue)) {
91 throw new IllegalArgumentException("int value of statuses must be between 1 and three");
92 }
93 return STATUSES[statusAsInt];
94 }
95
96 /**
97 * Returns the int value of status, for backward compatibility with ehcache versions below 1.2
98 * @return the int value of this status.
99 */
100 public int intValue() {
101 return intValue;
102 }
103
104 /**
105 * Indicates whether some other object is "equal to" this one.
106 * <p/>
107 * The <code>equals</code> method implements an equivalence relation
108 * on non-null object references:
109 * <ul>
110 * <li>It is <i>reflexive</i>: for any non-null reference value
111 * <code>x</code>, <code>x.equals(x)</code> should return
112 * <code>true</code>.
113 * <li>It is <i>symmetric</i>: for any non-null reference values
114 * <code>x</code> and <code>y</code>, <code>x.equals(y)</code>
115 * should return <code>true</code> if and only if
116 * <code>y.equals(x)</code> returns <code>true</code>.
117 * <li>It is <i>transitive</i>: for any non-null reference values
118 * <code>x</code>, <code>y</code>, and <code>z</code>, if
119 * <code>x.equals(y)</code> returns <code>true</code> and
120 * <code>y.equals(z)</code> returns <code>true</code>, then
121 * <code>x.equals(z)</code> should return <code>true</code>.
122 * <li>It is <i>consistent</i>: for any non-null reference values
123 * <code>x</code> and <code>y</code>, multiple invocations of
124 * <tt>x.equals(y)</tt> consistently return <code>true</code>
125 * or consistently return <code>false</code>, provided no
126 * information used in <code>equals</code> comparisons on the
127 * objects is modified.
128 * <li>For any non-null reference value <code>x</code>,
129 * <code>x.equals(null)</code> should return <code>false</code>.
130 * </ul>
131 * <p/>
132 * The <tt>equals</tt> method for class <code>Object</code> implements
133 * the most discriminating possible equivalence relation on objects;
134 * that is, for any non-null reference values <code>x</code> and
135 * <code>y</code>, this method returns <code>true</code> if and only
136 * if <code>x</code> and <code>y</code> refer to the same object
137 * (<code>x == y</code> has the value <code>true</code>).
138 * <p/>
139 * Note that it is generally necessary to override the <tt>hashCode</tt>
140 * method whenever this method is overridden, so as to maintain the
141 * general contract for the <tt>hashCode</tt> method, which states
142 * that equal objects must have equal hash codes.
143 *
144 * @param object the reference object with which to compare.
145 * @return <code>true</code> if this object is the same as the obj
146 * argument; <code>false</code> otherwise.
147 * @see #hashCode()
148 * @see java.util.Hashtable
149 */
150 public boolean equals(Object object) {
151 if (!(object instanceof Status)) {
152 return false;
153 }
154 return ((Status) object).intValue == intValue;
155 }
156
157 /**
158 * Equality checker when the comparison object is of the same type.
159 * @param status the status to check
160 * @return true is the statuses are the same
161 */
162 public boolean equals(Status status) {
163 if (status == null) {
164 return false;
165 } else {
166 return (intValue == status.intValue);
167 }
168 }
169
170 /**
171 * Returns a hash code value for Status. It is the underlying int value of the status.
172 * @return a hash code value for this object.
173 * @see Object#hashCode()
174 * @see java.util.Hashtable
175 */
176 public int hashCode() {
177 return intValue;
178 }
179
180 }