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