1 /**
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Intalio, Inc. For written permission,
18 * please contact info@exolab.org.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Intalio, Inc. Exolab is a registered
23 * trademark of Intalio, Inc.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY INTALIO, INC. AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * INTALIO, INC. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 1999 (C) Intalio, Inc. All Rights Reserved.
42 *
43 * $Id$
44 */
45
46 package org.exolab.castor.mapping;
47
48 import java.io.Serializable;
49 import java.util.HashMap;
50 import java.util.Map;
51
52 /**
53 * The access mode for a class. This object is used by class
54 * descriptors to specify the access mode for a class.
55 * <p>
56 * In persistent storage each class is defined as having one of three
57 * access modes:
58 * <ul>
59 * <li>Read only
60 * <li>Shared (aka optimistic locking)
61 * <li>Exclusive (aka pessimistic locking)
62 * <li>DbLocked (database lock)
63 * </ul>
64 * Transactions typically access objects based on the specified access
65 * mode. A transaction may be requested to access any object as read
66 * only or exclusive, but may not access exclusive objects as shared.
67 *
68 * @author <a href="arkin@intalio.com">Assaf Arkin</a>
69 * @author <a href="mailto:ralf DOT joachim AT syscon DOT eu">Ralf Joachim</a>
70 * @version $Revision$ $Date: 2006-04-25 15:08:23 -0600 (Tue, 25 Apr 2006) $
71 */
72 public class AccessMode implements Cloneable, Comparable, Serializable {
73 //-------------------------------------------------------------------------
74
75 /** SerialVersionUID */
76 private static final long serialVersionUID = -7113303922354626951L;
77
78 private static final Map IDS = new HashMap(7);
79 private static final Map NAMES = new HashMap(7);
80
81 //-------------------------------------------------------------------------
82
83 /**
84 * Read only access. Objects can be read but are not made
85 * persistent and changes to objects are not reflected in
86 * persistent storage.
87 */
88 public static final AccessMode ReadOnly
89 = new AccessMode((short) 0, "read-only");
90
91 /**
92 * Shared access. Objects can be read by multiple concurrent
93 * transactions. Equivalent to optimistic locking.
94 */
95 public static final AccessMode Shared
96 = new AccessMode((short) 1, "shared");
97
98 /**
99 * Exclusive access. Objects can be access by a single transaction
100 * at any given time. Equivalent to pessimistic locking.
101 */
102 public static final AccessMode Exclusive
103 = new AccessMode((short) 2, "exclusive");
104
105 /**
106 * DbLocked access. Objects can be access by a single transaction
107 * at any given time, and a lock is acquired in the database.
108 */
109 public static final AccessMode DbLocked
110 = new AccessMode((short) 3, "db-locked");
111
112 //-------------------------------------------------------------------------
113
114 /**
115 * Returns the access mode from the name. If <tt>accessMode</tt>
116 * is null, return the default access mode ({@link #Shared}).
117 * Otherwise returns the named access mode.
118 *
119 * @param accessMode The access mode name
120 * @return The access mode
121 */
122 public static AccessMode valueOf(final String accessMode) {
123 AccessMode mode = (AccessMode) NAMES.get(accessMode);
124 if (mode != null) {
125 return mode;
126 }
127 throw new IllegalArgumentException("Unrecognized access mode");
128 }
129
130 public static AccessMode valueOf(final short accessMode) {
131 AccessMode mode = (AccessMode) IDS.get(new Short(accessMode));
132 if (mode != null) {
133 return mode;
134 }
135 throw new IllegalArgumentException("Unrecognized access mode");
136 }
137
138 //-------------------------------------------------------------------------
139
140 /**
141 * The id of this access mode as originally used at Database.load() and
142 * Query.execute().
143 */
144 private short _id;
145
146 /**
147 * The name of this access mode as it would appear in a mapping file.
148 */
149 private String _name;
150
151 //-------------------------------------------------------------------------
152
153 private AccessMode(final short id, final String name) {
154 _id = id;
155 _name = name;
156
157 IDS.put(new Short(id), this);
158 NAMES.put(name, this);
159 }
160
161 public short getId() { return _id; }
162 public String getName() { return _name; }
163
164 //-------------------------------------------------------------------------
165
166 /**
167 * Returns the String representation of this kind.
168 *
169 * @return String representation of this kind.
170 */
171 public String toString() {
172 return _name;
173 }
174
175 /**
176 * Clone only returns the one and only instance of this kind.
177 *
178 * @return The original instance.
179 */
180 public Object clone() {
181 return this;
182 }
183
184 /**
185 * Returns if the specified object and this are one and the same instance.
186 *
187 * @param other Object to be compared with this instance.
188 * @return <code>true</code> if other equals this else <code>false</code>.
189 */
190 public boolean equals(final Object other) {
191 return (this == other);
192 }
193
194 /**
195 * Returns the hash code of this object.
196 *
197 * @return Hash code of this object.
198 */
199 public int hashCode() {
200 return _id;
201 }
202
203 /**
204 * Compares id against id of the specified object. So this
205 * method is inconsistent with {@link #equals(Object)}.
206 *
207 * @param other Object to be compared with this instance.
208 * @return A negative integer, zero, or a positive integer as this object
209 * is less than, equal to, or greater than the specified object.
210 */
211 public int compareTo(final Object other) {
212 return compareTo((AccessMode) other);
213 }
214
215 public int compareTo(final AccessMode other) {
216 return _id - other._id;
217 }
218
219 /**
220 * Called during deserialization.
221 *
222 * @return The existing instance of the enum. <br>So you can use '=='
223 * like 'equals' even if you use a deserialized Enum.
224 */
225 protected Object readResolve() {
226 return NAMES.get(_name);
227 }
228
229 //-------------------------------------------------------------------------
230 }