--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.store.raw.ContainerLock\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.store.raw;\r
+\r
+/**\r
+ A ContainerLock represents a qualifier that is to be used when\r
+ locking a container through a ContainerHandle.\r
+\r
+ <BR>\r
+ MT - Immutable\r
+\r
+ @see ContainerHandle\r
+ @see LockingPolicy\r
+*/\r
+\r
+public final class ContainerLock {\r
+\r
+ /** Integer representation of the type of the lock. */\r
+ private final int type;\r
+ /** Bit mask with one bit set. The position of the bit tells the type of\r
+ * the lock. */\r
+ private final int typeBit;\r
+ /** Bit mask which represents the lock types that are compatible with this\r
+ * lock type. */\r
+ private final int compat;\r
+\r
+ /** Number of types of container locks. */\r
+ public static final int C_NUMBER = 5;\r
+\r
+ /** Container lock compatibility table. */\r
+ private static final boolean[][] C_COMPAT = {\r
+\r
+ // Granted\r
+ // Request \ CIS CIX CS CU CX \r
+ // \r
+ /* CIS */ { true, true, true, false, false },\r
+ /* CIX */ { true, true, false, false, false },\r
+ /* CS */ { true, false, true, false, false },\r
+ /* CU */ { false, false, true, false, false },\r
+ /* CX */ { false, false, false, false, false }\r
+\r
+ };\r
+\r
+ private ContainerLock(int type) {\r
+ this.type = type;\r
+ typeBit = (1 << type);\r
+ int bitmask = 0;\r
+ for (int i = 0; i < C_NUMBER; i++) {\r
+ // set a bit in bitmask for each compatible lock type\r
+ if (C_COMPAT[type][i]) {\r
+ bitmask |= (1 << i);\r
+ }\r
+ }\r
+ compat = bitmask;\r
+ }\r
+\r
+ // Names of locks for virtual lock table print out\r
+ private static String[] shortnames = {"IS", "IX", "S", "U", "X" };\r
+\r
+ /** Container Intent Shared lock */\r
+ public static final ContainerLock CIS = new ContainerLock(0);\r
+ /** Container Intent Exclusive lock */\r
+ public static final ContainerLock CIX = new ContainerLock(1);\r
+ /** Container Shared lock */\r
+ public static final ContainerLock CS = new ContainerLock(2);\r
+ /** Container Update lock */\r
+ public static final ContainerLock CU = new ContainerLock(3);\r
+ /** Container Exclusive lock */\r
+ public static final ContainerLock CX = new ContainerLock(4);\r
+\r
+ /**\r
+ Get an integer representation of the type of the lock. This method is guaranteed\r
+ to return an integer >= 0 and < C_NUMBER. No correlation between the value\r
+ and one of the static variables (CIS etc.) is guaranteed, except that\r
+ the values returned do not change.\r
+ */\r
+ public int getType() {\r
+ return type;\r
+ }\r
+\r
+ public boolean isCompatible(ContainerLock granted) {\r
+ return (granted.typeBit & compat) != 0;\r
+ }\r
+\r
+ public String toString() {\r
+\r
+ return shortnames[getType()];\r
+ }\r
+}\r