You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openjpa.apache.org by pc...@apache.org on 2006/07/01 00:37:29 UTC
svn commit: r418401 [13/32] - in /incubator/openjpa/trunk: openjpa-lib/
openjpa-lib/src/main/java/org/apache/openjpa/lib/ant/
openjpa-lib/src/main/java/org/apache/openjpa/lib/conf/
openjpa-lib/src/main/java/org/apache/openjpa/lib/jdbc/ openjpa-lib/src/...
Modified: incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/Arrays.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/Arrays.java?rev=418401&r1=418400&r2=418401&view=diff
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/Arrays.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/Arrays.java Fri Jun 30 15:37:18 2006
@@ -1,19 +1,15 @@
/*
* Copyright 2006 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
+ * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-
/*
* Written by Dawid Kurzyniec, based on code written by Doug Lea with assistance
* from members of JCP JSR-166 Expert Group. Released to the public domain,
@@ -22,15 +18,13 @@
package org.apache.openjpa.lib.util.concurrent;
import java.lang.reflect.Array;
-
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
-
class Arrays {
- private Arrays() {
- }
+
+ private Arrays() {}
public static void sort(long[] a) {
java.util.Arrays.sort(a);
@@ -100,11 +94,13 @@
java.util.Arrays.sort(a, c);
}
- public static void sort(Object[] a, int fromIndex, int toIndex, Comparator c) {
+ public static void sort(Object[] a, int fromIndex, int toIndex,
+ Comparator c) {
java.util.Arrays.sort(a, fromIndex, toIndex, c);
}
// Searching
+
public static int binarySearch(long[] a, long key) {
return java.util.Arrays.binarySearch(a, key);
}
@@ -142,6 +138,7 @@
}
// Equality Testing
+
public static boolean equals(long[] a, long[] a2) {
return java.util.Arrays.equals(a, a2);
}
@@ -150,7 +147,7 @@
return java.util.Arrays.equals(a, a2);
}
- public static boolean equals(short[] a, short[] a2) {
+ public static boolean equals(short[] a, short a2[]) {
return java.util.Arrays.equals(a, a2);
}
@@ -179,6 +176,7 @@
}
// Filling
+
public static void fill(long[] a, long val) {
java.util.Arrays.fill(a, val);
}
@@ -223,7 +221,8 @@
java.util.Arrays.fill(a, val);
}
- public static void fill(boolean[] a, int fromIndex, int toIndex, boolean val) {
+ public static void fill(boolean[] a, int fromIndex, int toIndex,
+ boolean val) {
java.util.Arrays.fill(a, fromIndex, toIndex, val);
}
@@ -231,7 +230,7 @@
java.util.Arrays.fill(a, val);
}
- public static void fill(double[] a, int fromIndex, int toIndex, double val) {
+ public static void fill(double[] a, int fromIndex, int toIndex,double val) {
java.util.Arrays.fill(a, fromIndex, toIndex, val);
}
@@ -247,7 +246,8 @@
java.util.Arrays.fill(a, val);
}
- public static void fill(Object[] a, int fromIndex, int toIndex, Object val) {
+ public static void fill(Object[] a, int fromIndex, int toIndex,
+ Object val) {
java.util.Arrays.fill(a, fromIndex, toIndex, val);
}
@@ -265,12 +265,10 @@
*/
public static Object[] copyOf(Object[] original, int newLength,
Class newType) {
- Object[] arr = (newType == Object[].class) ? new Object[newLength]
- : (Object[]) Array.newInstance(newType.getComponentType(),
- newLength);
- int len = ((original.length < newLength) ? original.length : newLength);
+ Object[] arr = (newType == Object[].class) ? new Object[newLength] :
+ (Object[])Array.newInstance(newType.getComponentType(), newLength);
+ int len = (original.length < newLength ? original.length : newLength);
System.arraycopy(original, 0, arr, 0, len);
-
return arr;
}
@@ -279,9 +277,8 @@
*/
public static byte[] copyOf(byte[] original, int newLength) {
byte[] arr = new byte[newLength];
- int len = ((original.length < newLength) ? original.length : newLength);
+ int len = (original.length < newLength ? original.length : newLength);
System.arraycopy(original, 0, arr, 0, len);
-
return arr;
}
@@ -290,9 +287,8 @@
*/
public static short[] copyOf(short[] original, int newLength) {
short[] arr = new short[newLength];
- int len = ((original.length < newLength) ? original.length : newLength);
+ int len = (original.length < newLength ? original.length : newLength);
System.arraycopy(original, 0, arr, 0, len);
-
return arr;
}
@@ -301,9 +297,8 @@
*/
public static int[] copyOf(int[] original, int newLength) {
int[] arr = new int[newLength];
- int len = ((original.length < newLength) ? original.length : newLength);
+ int len = (original.length < newLength ? original.length : newLength);
System.arraycopy(original, 0, arr, 0, len);
-
return arr;
}
@@ -312,9 +307,8 @@
*/
public static long[] copyOf(long[] original, int newLength) {
long[] arr = new long[newLength];
- int len = ((original.length < newLength) ? original.length : newLength);
+ int len = (original.length < newLength ? original.length : newLength);
System.arraycopy(original, 0, arr, 0, len);
-
return arr;
}
@@ -323,9 +317,8 @@
*/
public static char[] copyOf(char[] original, int newLength) {
char[] arr = new char[newLength];
- int len = ((original.length < newLength) ? original.length : newLength);
+ int len = (original.length < newLength ? original.length : newLength);
System.arraycopy(original, 0, arr, 0, len);
-
return arr;
}
@@ -334,9 +327,8 @@
*/
public static float[] copyOf(float[] original, int newLength) {
float[] arr = new float[newLength];
- int len = ((original.length < newLength) ? original.length : newLength);
+ int len = (original.length < newLength ? original.length : newLength);
System.arraycopy(original, 0, arr, 0, len);
-
return arr;
}
@@ -345,9 +337,8 @@
*/
public static double[] copyOf(double[] original, int newLength) {
double[] arr = new double[newLength];
- int len = ((original.length < newLength) ? original.length : newLength);
+ int len = (original.length < newLength ? original.length : newLength);
System.arraycopy(original, 0, arr, 0, len);
-
return arr;
}
@@ -356,9 +347,8 @@
*/
public static boolean[] copyOf(boolean[] original, int newLength) {
boolean[] arr = new boolean[newLength];
- int len = ((original.length < newLength) ? original.length : newLength);
+ int len = (original.length < newLength ? original.length : newLength);
System.arraycopy(original, 0, arr, 0, len);
-
return arr;
}
@@ -375,18 +365,13 @@
public static Object[] copyOfRange(Object[] original, int from, int to,
Class newType) {
int newLength = to - from;
-
- if (newLength < 0) {
+ if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
- }
-
- Object[] arr = (newType == Object[].class) ? new Object[newLength]
- : (Object[]) Array.newInstance(newType.getComponentType(),
- newLength);
- int ceil = original.length - from;
+ Object[] arr = (newType == Object[].class) ? new Object[newLength] :
+ (Object[])Array.newInstance(newType.getComponentType(), newLength);
+ int ceil = original.length-from;
int len = (ceil < newLength) ? ceil : newLength;
System.arraycopy(original, from, arr, 0, len);
-
return arr;
}
@@ -395,16 +380,12 @@
*/
public static byte[] copyOfRange(byte[] original, int from, int to) {
int newLength = to - from;
-
- if (newLength < 0) {
+ if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
- }
-
byte[] arr = new byte[newLength];
- int ceil = original.length - from;
+ int ceil = original.length-from;
int len = (ceil < newLength) ? ceil : newLength;
System.arraycopy(original, from, arr, 0, len);
-
return arr;
}
@@ -413,16 +394,12 @@
*/
public static short[] copyOfRange(short[] original, int from, int to) {
int newLength = to - from;
-
- if (newLength < 0) {
+ if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
- }
-
short[] arr = new short[newLength];
- int ceil = original.length - from;
+ int ceil = original.length-from;
int len = (ceil < newLength) ? ceil : newLength;
System.arraycopy(original, from, arr, 0, len);
-
return arr;
}
@@ -431,16 +408,12 @@
*/
public static int[] copyOfRange(int[] original, int from, int to) {
int newLength = to - from;
-
- if (newLength < 0) {
+ if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
- }
-
int[] arr = new int[newLength];
- int ceil = original.length - from;
+ int ceil = original.length-from;
int len = (ceil < newLength) ? ceil : newLength;
System.arraycopy(original, from, arr, 0, len);
-
return arr;
}
@@ -449,16 +422,12 @@
*/
public static long[] copyOfRange(long[] original, int from, int to) {
int newLength = to - from;
-
- if (newLength < 0) {
+ if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
- }
-
long[] arr = new long[newLength];
- int ceil = original.length - from;
+ int ceil = original.length-from;
int len = (ceil < newLength) ? ceil : newLength;
System.arraycopy(original, from, arr, 0, len);
-
return arr;
}
@@ -467,16 +436,12 @@
*/
public static char[] copyOfRange(char[] original, int from, int to) {
int newLength = to - from;
-
- if (newLength < 0) {
+ if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
- }
-
char[] arr = new char[newLength];
- int ceil = original.length - from;
+ int ceil = original.length-from;
int len = (ceil < newLength) ? ceil : newLength;
System.arraycopy(original, from, arr, 0, len);
-
return arr;
}
@@ -485,16 +450,12 @@
*/
public static float[] copyOfRange(float[] original, int from, int to) {
int newLength = to - from;
-
- if (newLength < 0) {
+ if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
- }
-
float[] arr = new float[newLength];
- int ceil = original.length - from;
+ int ceil = original.length-from;
int len = (ceil < newLength) ? ceil : newLength;
System.arraycopy(original, from, arr, 0, len);
-
return arr;
}
@@ -503,16 +464,12 @@
*/
public static double[] copyOfRange(double[] original, int from, int to) {
int newLength = to - from;
-
- if (newLength < 0) {
+ if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
- }
-
double[] arr = new double[newLength];
- int ceil = original.length - from;
+ int ceil = original.length-from;
int len = (ceil < newLength) ? ceil : newLength;
System.arraycopy(original, from, arr, 0, len);
-
return arr;
}
@@ -521,16 +478,12 @@
*/
public static boolean[] copyOfRange(boolean[] original, int from, int to) {
int newLength = to - from;
-
- if (newLength < 0) {
+ if (newLength < 0)
throw new IllegalArgumentException(from + " > " + to);
- }
-
boolean[] arr = new boolean[newLength];
- int ceil = original.length - from;
+ int ceil = original.length-from;
int len = (ceil < newLength) ? ceil : newLength;
System.arraycopy(original, from, arr, 0, len);
-
return arr;
}
@@ -541,248 +494,176 @@
/**
* @since 1.5
*/
- public static int hashCode(long[] a) {
- if (a == null) {
- return 0;
- }
-
+ public static int hashCode(long a[]) {
+ if (a == null) return 0;
int hash = 1;
-
- for (int i = 0; i < a.length; i++) {
+ for (int i=0; i<a.length; i++) {
long e = a[i];
- hash = (31 * hash) + (int) (e ^ (e >>> 32));
+ hash = 31*hash + (int)(e ^ (e >>> 32));
}
-
return hash;
}
/**
* @since 1.5
*/
- public static int hashCode(int[] a) {
- if (a == null) {
- return 0;
- }
-
+ public static int hashCode(int a[]) {
+ if (a == null) return 0;
int hash = 1;
-
- for (int i = 0; i < a.length; i++) {
- hash = (31 * hash) + a[i];
+ for (int i=0; i<a.length; i++) {
+ hash = 31*hash + a[i];
}
-
return hash;
}
/**
* @since 1.5
*/
- public static int hashCode(short[] a) {
- if (a == null) {
- return 0;
- }
-
+ public static int hashCode(short a[]) {
+ if (a == null) return 0;
int hash = 1;
-
- for (int i = 0; i < a.length; i++) {
- hash = (31 * hash) + a[i];
+ for (int i=0; i<a.length; i++) {
+ hash = 31*hash + a[i];
}
-
return hash;
}
/**
* @since 1.5
*/
- public static int hashCode(char[] a) {
- if (a == null) {
- return 0;
- }
-
+ public static int hashCode(char a[]) {
+ if (a == null) return 0;
int hash = 1;
-
- for (int i = 0; i < a.length; i++) {
- hash = (31 * hash) + a[i];
+ for (int i=0; i<a.length; i++) {
+ hash = 31*hash + a[i];
}
-
return hash;
}
/**
* @since 1.5
*/
- public static int hashCode(byte[] a) {
- if (a == null) {
- return 0;
- }
-
+ public static int hashCode(byte a[]) {
+ if (a == null) return 0;
int hash = 1;
-
- for (int i = 0; i < a.length; i++) {
- hash = (31 * hash) + a[i];
+ for (int i=0; i<a.length; i++) {
+ hash = 31*hash + a[i];
}
-
return hash;
}
/**
* @since 1.5
*/
- public static int hashCode(boolean[] a) {
- if (a == null) {
- return 0;
- }
-
+ public static int hashCode(boolean a[]) {
+ if (a == null) return 0;
int hash = 1;
-
- for (int i = 0; i < a.length; i++) {
- hash = (31 * hash) + (a[i] ? 1231 : 1237);
+ for (int i=0; i<a.length; i++) {
+ hash = 31*hash + (a[i] ? 1231 : 1237);
}
-
return hash;
}
/**
* @since 1.5
*/
- public static int hashCode(float[] a) {
- if (a == null) {
- return 0;
- }
-
+ public static int hashCode(float a[]) {
+ if (a == null) return 0;
int hash = 1;
-
- for (int i = 0; i < a.length; i++) {
- hash = (31 * hash) + Float.floatToIntBits(a[i]);
+ for (int i=0; i<a.length; i++) {
+ hash = 31*hash + Float.floatToIntBits(a[i]);
}
-
return hash;
}
/**
* @since 1.5
*/
- public static int hashCode(double[] a) {
- if (a == null) {
- return 0;
- }
-
+ public static int hashCode(double a[]) {
+ if (a == null) return 0;
int hash = 1;
-
- for (int i = 0; i < a.length; i++) {
+ for (int i=0; i<a.length; i++) {
long e = Double.doubleToLongBits(a[i]);
- hash = (31 * hash) + (int) (e ^ (e >>> 32));
+ hash = 31*hash + (int)(e ^ (e >>> 32));
}
-
return hash;
}
/**
* @since 1.5
*/
- public static int hashCode(Object[] a) {
- if (a == null) {
- return 0;
- }
-
+ public static int hashCode(Object a[]) {
+ if (a == null) return 0;
int hash = 1;
-
- for (int i = 0; i < a.length; i++) {
+ for (int i=0; i<a.length; i++) {
Object e = a[i];
- hash = (31 * hash) + ((e == null) ? 0 : e.hashCode());
+ hash = 31*hash + (e == null ? 0 : e.hashCode());
}
-
return hash;
}
/**
* @since 1.5
*/
- public static int deepHashCode(Object[] a) {
- if (a == null) {
- return 0;
- }
-
+ public static int deepHashCode(Object a[]) {
+ if (a == null) return 0;
int hash = 1;
-
- for (int i = 0; i < a.length; i++) {
+ for (int i=0; i<a.length; i++) {
Object e = a[i];
- hash = (31 * hash) +
- ((e instanceof Object[]) ? deepHashCode((Object[]) e)
- : ((e instanceof byte[])
- ? hashCode((byte[]) e)
- : ((e instanceof short[]) ? hashCode((short[]) e)
- : ((e instanceof int[])
- ? hashCode((int[]) e)
- : ((e instanceof long[]) ? hashCode((long[]) e)
- : ((e instanceof char[])
- ? hashCode((char[]) e)
- : ((e instanceof boolean[]) ? hashCode((boolean[]) e)
- : ((e instanceof float[])
- ? hashCode((float[]) e)
- : ((e instanceof double[]) ? hashCode((double[]) e)
- : ((e != null) ? e.hashCode() : 0))))))))));
+ hash = 31*hash +
+ (e instanceof Object[] ? deepHashCode((Object[])e) :
+ (e instanceof byte[] ? hashCode((byte[])e) :
+ (e instanceof short[] ? hashCode((short[])e) :
+ (e instanceof int[] ? hashCode((int[])e) :
+ (e instanceof long[] ? hashCode((long[])e) :
+ (e instanceof char[] ? hashCode((char[])e) :
+ (e instanceof boolean[] ? hashCode((boolean[])e) :
+ (e instanceof float[] ? hashCode((float[])e) :
+ (e instanceof double[] ? hashCode((double[])e) :
+ (e != null ? e.hashCode() : 0))))))))));
}
-
return hash;
+
}
/**
* @since 1.5
*/
public static boolean deepEquals(Object[] a1, Object[] a2) {
- if (a1 == a2) {
- return true;
- }
-
- if ((a1 == null) || (a2 == null)) {
- return false;
- }
-
+ if (a1 == a2) return true;
+ if (a1 == null || a2==null) return false;
int len = a1.length;
-
- if (len != a2.length) {
- return false;
- }
-
+ if (len != a2.length) return false;
for (int i = 0; i < len; i++) {
Object e1 = a1[i];
Object e2 = a2[i];
+ if (e1 == e2) continue;
+ if (e1 == null) return false;
+ boolean eq =
+ (e1.getClass() != e2.getClass() || e1.getClass().isArray()) ?
+ e1.equals(e2) :
+ (e1 instanceof Object[] && e2 instanceof Object[]) ?
+ deepEquals((Object[])e1, (Object[])e2) :
+ (e1 instanceof byte[] && e2 instanceof byte[]) ?
+ equals((byte[])e1, (byte[])e2) :
+ (e1 instanceof short[] && e2 instanceof short[]) ?
+ equals((short[])e1, (short[])e2) :
+ (e1 instanceof int[] && e2 instanceof int[]) ?
+ equals((int[])e1, (int[])e2) :
+ (e1 instanceof long[] && e2 instanceof long[]) ?
+ equals((long[])e1, (long[])e2) :
+ (e1 instanceof char[] && e2 instanceof char[]) ?
+ equals((char[])e1, (char[])e2) :
+ (e1 instanceof boolean[] && e2 instanceof boolean[]) ?
+ equals((boolean[])e1, (boolean[])e2) :
+ (e1 instanceof float[] && e2 instanceof float[]) ?
+ equals((float[])e1, (float[])e2) :
+ (e1 instanceof double[] && e2 instanceof double[]) ?
+ equals((double[])e1, (double[])e2) :
+ e1.equals(e2);
- if (e1 == e2) {
- continue;
- }
-
- if (e1 == null) {
- return false;
- }
-
- boolean eq = ((e1.getClass() != e2.getClass()) ||
- e1.getClass().isArray()) ? e1.equals(e2)
- : ((e1 instanceof Object[] &&
- e2 instanceof Object[])
- ? deepEquals((Object[]) e1, (Object[]) e2)
- : ((e1 instanceof byte[] && e2 instanceof byte[])
- ? equals((byte[]) e1, (byte[]) e2)
- : ((e1 instanceof short[] && e2 instanceof short[])
- ? equals((short[]) e1, (short[]) e2)
- : ((e1 instanceof int[] && e2 instanceof int[])
- ? equals((int[]) e1, (int[]) e2)
- : ((e1 instanceof long[] && e2 instanceof long[])
- ? equals((long[]) e1, (long[]) e2)
- : ((e1 instanceof char[] && e2 instanceof char[])
- ? equals((char[]) e1, (char[]) e2)
- : ((e1 instanceof boolean[] && e2 instanceof boolean[])
- ? equals((boolean[]) e1, (boolean[]) e2)
- : ((e1 instanceof float[] && e2 instanceof float[])
- ? equals((float[]) e1, (float[]) e2)
- : ((e1 instanceof double[] && e2 instanceof double[])
- ? equals((double[]) e1, (double[]) e2) : e1.equals(e2))))))))));
-
- if (!eq) {
- return false;
- }
+ if (!eq) return false;
}
-
return true;
}
@@ -790,22 +671,12 @@
* @since 1.5
*/
public static String toString(long[] a) {
- if (a == null) {
- return "null";
- }
-
- if (a.length == 0) {
- return "[]";
- }
-
+ if (a == null) return "null";
+ if (a.length == 0) return "[]";
StringBuffer buf = new StringBuffer();
buf.append('[').append(a[0]);
-
- for (int i = 1; i < a.length; i++)
- buf.append(", ").append(a[i]);
-
+ for (int i=1; i<a.length; i++) buf.append(", ").append(a[i]);
buf.append(']');
-
return buf.toString();
}
@@ -813,22 +684,12 @@
* @since 1.5
*/
public static String toString(int[] a) {
- if (a == null) {
- return "null";
- }
-
- if (a.length == 0) {
- return "[]";
- }
-
+ if (a == null) return "null";
+ if (a.length == 0) return "[]";
StringBuffer buf = new StringBuffer();
buf.append('[').append(a[0]);
-
- for (int i = 1; i < a.length; i++)
- buf.append(", ").append(a[i]);
-
+ for (int i=1; i<a.length; i++) buf.append(", ").append(a[i]);
buf.append(']');
-
return buf.toString();
}
@@ -836,22 +697,12 @@
* @since 1.5
*/
public static String toString(short[] a) {
- if (a == null) {
- return "null";
- }
-
- if (a.length == 0) {
- return "[]";
- }
-
+ if (a == null) return "null";
+ if (a.length == 0) return "[]";
StringBuffer buf = new StringBuffer();
buf.append('[').append(a[0]);
-
- for (int i = 1; i < a.length; i++)
- buf.append(", ").append(a[i]);
-
+ for (int i=1; i<a.length; i++) buf.append(", ").append(a[i]);
buf.append(']');
-
return buf.toString();
}
@@ -859,22 +710,12 @@
* @since 1.5
*/
public static String toString(char[] a) {
- if (a == null) {
- return "null";
- }
-
- if (a.length == 0) {
- return "[]";
- }
-
+ if (a == null) return "null";
+ if (a.length == 0) return "[]";
StringBuffer buf = new StringBuffer();
buf.append('[').append(a[0]);
-
- for (int i = 1; i < a.length; i++)
- buf.append(", ").append(a[i]);
-
+ for (int i=1; i<a.length; i++) buf.append(", ").append(a[i]);
buf.append(']');
-
return buf.toString();
}
@@ -882,22 +723,12 @@
* @since 1.5
*/
public static String toString(byte[] a) {
- if (a == null) {
- return "null";
- }
-
- if (a.length == 0) {
- return "[]";
- }
-
+ if (a == null) return "null";
+ if (a.length == 0) return "[]";
StringBuffer buf = new StringBuffer();
buf.append('[').append(a[0]);
-
- for (int i = 1; i < a.length; i++)
- buf.append(", ").append(a[i]);
-
+ for (int i=1; i<a.length; i++) buf.append(", ").append(a[i]);
buf.append(']');
-
return buf.toString();
}
@@ -905,22 +736,12 @@
* @since 1.5
*/
public static String toString(boolean[] a) {
- if (a == null) {
- return "null";
- }
-
- if (a.length == 0) {
- return "[]";
- }
-
+ if (a == null) return "null";
+ if (a.length == 0) return "[]";
StringBuffer buf = new StringBuffer();
buf.append('[').append(a[0]);
-
- for (int i = 1; i < a.length; i++)
- buf.append(", ").append(a[i]);
-
+ for (int i=1; i<a.length; i++) buf.append(", ").append(a[i]);
buf.append(']');
-
return buf.toString();
}
@@ -928,22 +749,12 @@
* @since 1.5
*/
public static String toString(float[] a) {
- if (a == null) {
- return "null";
- }
-
- if (a.length == 0) {
- return "[]";
- }
-
+ if (a == null) return "null";
+ if (a.length == 0) return "[]";
StringBuffer buf = new StringBuffer();
buf.append('[').append(a[0]);
-
- for (int i = 1; i < a.length; i++)
- buf.append(", ").append(a[i]);
-
+ for (int i=1; i<a.length; i++) buf.append(", ").append(a[i]);
buf.append(']');
-
return buf.toString();
}
@@ -951,22 +762,12 @@
* @since 1.5
*/
public static String toString(double[] a) {
- if (a == null) {
- return "null";
- }
-
- if (a.length == 0) {
- return "[]";
- }
-
+ if (a == null) return "null";
+ if (a.length == 0) return "[]";
StringBuffer buf = new StringBuffer();
buf.append('[').append(a[0]);
-
- for (int i = 1; i < a.length; i++)
- buf.append(", ").append(a[i]);
-
+ for (int i=1; i<a.length; i++) buf.append(", ").append(a[i]);
buf.append(']');
-
return buf.toString();
}
@@ -974,22 +775,12 @@
* @since 1.5
*/
public static String toString(Object[] a) {
- if (a == null) {
- return "null";
- }
-
- if (a.length == 0) {
- return "[]";
- }
-
+ if (a == null) return "null";
+ if (a.length == 0) return "[]";
StringBuffer buf = new StringBuffer();
buf.append('[').append(a[0]);
-
- for (int i = 1; i < a.length; i++)
- buf.append(", ").append(a[i]);
-
+ for (int i=1; i<a.length; i++) buf.append(", ").append(a[i]);
buf.append(']');
-
return buf.toString();
}
@@ -997,55 +788,38 @@
* @since 1.5
*/
public static String deepToString(Object[] a) {
- if (a == null) {
- return "null";
- }
-
+ if (a == null) return "null";
StringBuffer buf = new StringBuffer();
deepToString(a, buf, new ArrayList());
-
return buf.toString();
}
private static void deepToString(Object[] a, StringBuffer buf, List seen) {
seen.add(a);
buf.append('[');
-
for (int i = 0; i < a.length; i++) {
- if (i > 0) {
- buf.append(", ");
- }
-
+ if (i>0) buf.append(", ");
Object e = a[i];
-
if (e == null) {
buf.append("null");
} else if (!e.getClass().isArray()) {
buf.append(e.toString());
} else if (e instanceof Object[]) {
- if (seen.contains(e)) {
- buf.append("[...]");
- } else {
- deepToString((Object[]) e, buf, seen);
- }
+ if (seen.contains(e)) buf.append("[...]");
+ else deepToString((Object[])e, buf, seen);
} else {
// primitive arr
- buf.append((e instanceof byte[]) ? toString((byte[]) e)
- : ((e instanceof short[])
- ? toString((short[]) e)
- : ((e instanceof int[]) ? toString((int[]) e)
- : ((e instanceof long[])
- ? toString((long[]) e)
- : ((e instanceof char[]) ? toString((char[]) e)
- : ((e instanceof boolean[])
- ? toString((boolean[]) e)
- : ((e instanceof float[]) ? toString((float[]) e)
- : ((e instanceof double[])
- ? toString((double[]) e) : ""))))))));
+ buf.append( (e instanceof byte[]) ? toString( (byte[]) e) :
+ (e instanceof short[]) ? toString( (short[]) e) :
+ (e instanceof int[]) ? toString( (int[]) e) :
+ (e instanceof long[]) ? toString( (long[]) e) :
+ (e instanceof char[]) ? toString( (char[]) e) :
+ (e instanceof boolean[]) ? toString( (boolean[]) e) :
+ (e instanceof float[]) ? toString( (float[]) e) :
+ (e instanceof double[]) ? toString( (double[]) e) : "");
}
}
-
buf.append(']');
- seen.remove(seen.size() - 1);
+ seen.remove(seen.size()-1);
}
}
Modified: incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/ConcurrentHashMap.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/ConcurrentHashMap.java?rev=418401&r1=418400&r2=418401&view=diff
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/ConcurrentHashMap.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/ConcurrentHashMap.java Fri Jun 30 15:37:18 2006
@@ -1,13 +1,10 @@
/*
* Copyright 2006 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
+ * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
@@ -15,13 +12,10 @@
*/
package org.apache.openjpa.lib.util.concurrent;
-import org.apache.openjpa.lib.util.SizedMap;
-
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
-
import java.util.AbstractCollection;
import java.util.AbstractMap;
import java.util.AbstractSet;
@@ -31,27 +25,24 @@
import java.util.NoSuchElementException;
import java.util.Random;
import java.util.Set;
-
+import org.apache.openjpa.lib.util.SizedMap;
/**
* This class implements a HashMap which has limited synchronization.
* In particular mutators are generally synchronized while accessors
- * are generally not. Additionally the Iterators returned by this
+ * are generally not. Additionally the Iterators returned by this
* class are not "fail-fast", but instead try to continue to iterate
* over the data structure after changes have been made.
- *
- * The synchronization semantics are built right in to the
+ * The synchronization semantics are built right in to the
* implementation rather than using a delegating wrapper like the
* other collection classes do because it wasn't clear to me that the
* how the two should be seperated or that it would be useful to do
- * so. This can probably be a topic for further debate in the
- * future.
- *
- * This class is based heavily on the HashMap class in the Java
+ * so. This can probably be a topic for further debate in the future.
+ * This class is based heavily on the HashMap class in the Java
* collections package.
*/
-public class ConcurrentHashMap extends AbstractMap implements ConcurrentMap,
- SizedMap, Cloneable, Serializable {
+public class ConcurrentHashMap extends AbstractMap
+ implements ConcurrentMap, SizedMap, Cloneable, Serializable {
/**
* The default initial capacity - MUST be a power of two.
*/
@@ -66,36 +57,20 @@
/**
* The load fast used when none specified in constructor.
- **/
- private static final float DEFAULT_LOAD_FACTOR = 0.75f;
+ **/ private static final float DEFAULT_LOAD_FACTOR = 0.75f;
/**
* Cache of random numbers used in "random" methods, since generating them
- * is expensive. We hope each map changes enough between cycling through
- * this list that the overall effect is random enough.
+ * is expensive. We hope each map changes enough between cycling through
+ * this list that the overall effect is random enough.
*/
static final double[] RANDOMS = new double[1000];
-
static {
Random random = new Random();
-
for (int i = 0; i < RANDOMS.length; i++)
RANDOMS[i] = random.nextDouble();
}
- // internal utilities
-
- /**
- * Value representing null keys inside tables.
- */
- private static final Object NULL_KEY = new Object();
-
- // Types of Enumerations/Iterations
- private static final int KEYS = 0;
- private static final int VALUES = 1;
- private static final int ENTRIES = 2;
- private static final long serialVersionUID = -6452706556724125778L;
-
/**
* The table, resized as necessary. Length MUST Always be a power of two.
*/
@@ -107,14 +82,14 @@
private transient int size;
/**
- * The next size value at which to resize (capacity * load factor).
+ * The next size value at which to resize(capacity * load factor).
* @serial
*/
private int threshold;
/**
* The load factor for the hash table.
- *
+ *
* @serial
*/
private final float loadFactor;
@@ -125,19 +100,14 @@
private int randomEntry = 0;
/**
- * Maximum entries.
+ * Maximum entries.
*/
private int maxSize = Integer.MAX_VALUE;
- // Views
- private transient Set entrySet = null;
- private transient Set keySet = null;
- private transient Collection values = null;
-
/**
* Constructs an empty <tt>ConcurrentHashMap</tt> with the specified initial
* capacity and load factor.
- *
+ *
* @param initialCapacity The initial capacity.
* @param loadFactor The load factor.
* @throws IllegalArgumentException if the initial capacity is negative
@@ -148,21 +118,16 @@
throw new IllegalArgumentException("Illegal initial capacity: " +
initialCapacity);
}
-
- if (initialCapacity > MAXIMUM_CAPACITY) {
+ if (initialCapacity > MAXIMUM_CAPACITY)
initialCapacity = MAXIMUM_CAPACITY;
- }
-
- if ((loadFactor <= 0) || (loadFactor > 1)) {
+ if (loadFactor <= 0 || loadFactor > 1) {
throw new IllegalArgumentException("Illegal load factor: " +
loadFactor);
}
// Find a power of 2 >= initialCapacity
int capacity = 1;
-
- while (capacity < initialCapacity)
- capacity <<= 1;
+ while (capacity < initialCapacity) capacity <<= 1;
this.loadFactor = loadFactor;
threshold = (int) (capacity * loadFactor);
@@ -171,8 +136,8 @@
/**
* Constructs an empty <tt>ConcurrentHashMap</tt> with the specified initial
- * capacity and the default load factor (0.75).
- *
+ * capacity and the default load factor(0.75).
+ *
* @param initialCapacity the initial capacity.
* @throws IllegalArgumentException if the initial capacity is negative.
*/
@@ -182,7 +147,7 @@
/**
* Constructs an empty <tt>ConcurrentHashMap</tt> with the default initial
- * capacity (16) and the default load factor (0.75).
+ * capacity(16) and the default load factor(0.75).
*/
public ConcurrentHashMap() {
this(DEFAULT_INITIAL_CAPACITY, DEFAULT_LOAD_FACTOR);
@@ -190,31 +155,38 @@
/**
* Constructs a new <tt>ConcurrentHashMap</tt> with the same mappings as the
- * specified <tt>Map</tt>. The <tt>ConcurrentHashMap</tt> is created with
- * default load factor (0.75) and an initial capacity sufficient to
+ * specified <tt>Map</tt>. The <tt>ConcurrentHashMap</tt> is created with
+ * default load factor(0.75) and an initial capacity sufficient to
* hold the mappings in the specified <tt>Map</tt>.
- *
+ *
* @param m the map whose mappings are to be placed in this map.
* @throws NullPointerException if the specified map is null.
*/
public ConcurrentHashMap(Map m) {
this(Math.max((int) (m.size() / DEFAULT_LOAD_FACTOR) + 1,
- DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
+ DEFAULT_INITIAL_CAPACITY), DEFAULT_LOAD_FACTOR);
putAll(m);
}
+ // internal utilities
+
+ /**
+ * Value representing null keys inside tables.
+ */
+ private static final Object NULL_KEY = new Object();
+
/**
* Returns internal representation for key. Use NULL_KEY if key is null.
*/
private static Object maskNull(Object key) {
- return ((key == null) ? NULL_KEY : key);
+ return(key == null ? NULL_KEY : key);
}
/**
* Returns key represented by specified internal representation.
*/
private static Object unmaskNull(Object key) {
- return ((key == NULL_KEY) ? null : key);
+ return(key == NULL_KEY ? null : key);
}
/**
@@ -222,20 +194,19 @@
*/
private static int hash(Object x) {
int h = x.hashCode();
-
- return h - (h << 7); // i.e., -127 * h
+ return h - (h << 7); // i.e., -127 * h
}
/**
* Check for equality of non-null reference x and possibly-null y.
*/
private static boolean eq(Object x, Object y) {
- return (x == y) || x.equals(y);
+ return x == y || x.equals(y);
}
/**
* Returns the current capacity of backing table in this map.
- *
+ *
* @return the current capacity of backing table in this map.
*/
public final int capacity() {
@@ -244,7 +215,7 @@
/**
* Returns the load factor for this map.
- *
+ *
* @return the load factor for this map.
*/
public final float loadFactor() {
@@ -257,14 +228,12 @@
public void setMaxSize(int maxSize) {
this.maxSize = (maxSize < 0) ? Integer.MAX_VALUE : maxSize;
-
- if (this.maxSize != Integer.MAX_VALUE) {
+ if (this.maxSize != Integer.MAX_VALUE)
removeOverflow(this.maxSize);
- }
}
public boolean isFull() {
- return (maxSize != Integer.MAX_VALUE) && (size() >= maxSize);
+ return maxSize != Integer.MAX_VALUE && size() >= maxSize;
}
public void overflowRemoved(Object key, Object value) {
@@ -272,7 +241,7 @@
/**
* Returns the number of key-value mappings in this map.
- *
+ *
* @return the number of key-value mappings in this map.
*/
public final int size() {
@@ -281,7 +250,7 @@
/**
* Returns <tt>true</tt> if this map contains no key-value mappings.
- *
+ *
* @return <tt>true</tt> if this map contains no key-value mappings.
*/
public final boolean isEmpty() {
@@ -295,7 +264,7 @@
* that the map contains no mapping for the key; it is also possible that
* the map explicitly maps the key to <tt>null</tt>. The
* <tt>containsKey</tt> method may be used to distinguish these two cases.
- *
+ *
* @param key the key whose associated value is to be returned.
* @return the value to which this map maps the specified key, or
* <tt>null</tt> if the map contains no mapping for this key.
@@ -303,14 +272,13 @@
*/
public Object get(Object key) {
Entry e = getEntry(key);
-
- return (e == null) ? null : e.value;
+ return e == null? null: e.value;
}
/**
* Returns <tt>true</tt> if this map contains a mapping for the
* specified key.
- *
+ *
* @param keyThe key whose presence in this map is to be tested
* @return <tt>true</tt> if this map contains a mapping for the specified
* key.
@@ -321,20 +289,16 @@
/**
* Returns the entry associated with the specified key in the
- * ConcurrentHashMap. Returns null if the ConcurrentHashMap contains no
+ * ConcurrentHashMap. Returns null if the ConcurrentHashMap contains no
* mapping for this key.
*/
protected Entry getEntry(Object key) {
Object k = maskNull(key);
int hash = hash(k);
Entry[] tab = table;
-
- for (Entry e = tab[hash & (tab.length - 1)]; e != null; e = e.next) {
- if ((e.hash == hash) && eq(k, e.key)) {
- return e;
- }
+ for (Entry e = tab[hash & (tab.length-1)]; e != null; e = e.next) {
+ if (e.hash == hash && eq(k, e.key)) return e;
}
-
return null;
}
@@ -342,41 +306,33 @@
* Associates the specified value with the specified key in this map.
* If the map previously contained a mapping for this key, the old
* value is replaced.
- *
+ *
* @param key key with which the specified value is to be associated.
* @param value value to be associated with the specified key.
* @return previous value associated with specified key, or <tt>null</tt>
- * if there was no mapping for key. A <tt>null</tt> return can
+ * if there was no mapping for key. A <tt>null</tt> return can
* also indicate that the ConcurrentHashMap previously associated
* <tt>null</tt> with the specified key.
*/
public Object put(Object key, Object value) {
Object k = maskNull(key);
int hash = hash(k);
-
synchronized (this) {
int i = hash & (table.length - 1);
for (Entry e = table[i]; e != null; e = e.next) {
- if ((e.hash == hash) && eq(k, e.key)) {
+ if (e.hash == hash && eq(k, e.key)) {
Object oldValue = e.value;
e.value = value;
-
return oldValue;
}
}
- if (maxSize != Integer.MAX_VALUE) {
+ if (maxSize != Integer.MAX_VALUE)
removeOverflow(maxSize - 1);
- }
-
table[i] = createEntry(hash, k, value, table[i]);
-
- if (size++ >= threshold) {
- resize(2 * table.length);
- }
+ if (size++ >= threshold) resize(2 * table.length);
}
-
return null;
}
@@ -384,13 +340,10 @@
* Remove any entries equal to or over the max size.
*/
private void removeOverflow(int maxSize) {
- while (size > maxSize) {
+ while(size > maxSize) {
Map.Entry entry = removeRandom();
-
- if (entry == null) {
+ if (entry == null)
break;
- }
-
overflowRemoved(entry.getKey(), entry.getValue());
}
}
@@ -398,27 +351,20 @@
public Object putIfAbsent(Object key, Object value) {
Object k = maskNull(key);
int hash = hash(k);
-
synchronized (this) {
int i = hash & (table.length - 1);
for (Entry e = table[i]; e != null; e = e.next) {
- if ((e.hash == hash) && eq(k, e.key)) {
+ if (e.hash == hash && eq(k, e.key)) {
return e.value;
}
}
- if (maxSize != Integer.MAX_VALUE) {
+ if (maxSize != Integer.MAX_VALUE)
removeOverflow(maxSize - 1);
- }
-
table[i] = createEntry(hash, k, value, table[i]);
-
- if (size++ >= threshold) {
- resize(2 * table.length);
- }
+ if (size++ >= threshold) resize(2 * table.length);
}
-
return null;
}
@@ -426,23 +372,20 @@
* Rehashes the contents of this map into a new <tt>ConcurrentHashMap</tt>
* instance with a larger capacity. This method is called automatically when
* the number of keys in this map exceeds its capacity and load factor.
- *
+ *
* @param newCapacity the new capacity, MUST be a power of two.
*/
private void resize(int newCapacity) {
- // assert (newCapacity & -newCapacity) == newCapacity; // power of 2
+ // assert(newCapacity & -newCapacity) == newCapacity; // power of 2
Entry[] oldTable = table;
int oldCapacity = oldTable.length;
// check if needed
- if ((size < threshold) || (oldCapacity > newCapacity)) {
- return;
- }
+ if (size < threshold || oldCapacity > newCapacity) return;
Entry[] newTable = new Entry[newCapacity];
- int mask = newCapacity - 1;
-
- for (int i = oldCapacity; i-- > 0;) {
+ int mask = newCapacity-1;
+ for (int i = oldCapacity; i-- > 0; ) {
for (Entry e = oldTable[i]; e != null; e = e.next) {
Entry clone = (Entry) e.clone();
int j = clone.hash & mask;
@@ -450,43 +393,31 @@
newTable[j] = clone;
}
}
-
table = newTable;
- threshold = (int) (newCapacity * loadFactor);
+ threshold = (int)(newCapacity * loadFactor);
}
/**
* Copies all of the mappings from the specified map to this map
* These mappings will replace any mappings that
* this map had for any of the keys currently in the specified map.
- *
+ *
* @param t mappings to be stored in this map.
* @throws NullPointerException if the specified map is null.
*/
public final synchronized void putAll(Map t) {
// Expand enough to hold t's elements without resizing.
int n = t.size();
-
- if (n == 0) {
- return;
- }
-
+ if (n == 0) return;
if (n >= threshold) {
- n = (int) ((n / loadFactor) + 1);
-
- if (n > MAXIMUM_CAPACITY) {
- n = MAXIMUM_CAPACITY;
- }
-
+ n = (int)(n / loadFactor + 1);
+ if (n > MAXIMUM_CAPACITY) n = MAXIMUM_CAPACITY;
int capacity = table.length;
-
- while (capacity < n)
- capacity <<= 1;
-
+ while (capacity < n) capacity <<= 1;
resize(capacity);
}
- for (Iterator i = t.entrySet().iterator(); i.hasNext();) {
+ for (Iterator i = t.entrySet().iterator(); i.hasNext(); ) {
Map.Entry e = (Map.Entry) i.next();
put(e.getKey(), e.getValue());
}
@@ -494,55 +425,46 @@
/**
* Removes the mapping for this key from this map if present.
- *
+ *
* @param key key whose mapping is to be removed from the map.
* @return previous value associated with specified key, or <tt>null</tt>
- * if there was no mapping for key. A <tt>null</tt> return can
+ * if there was no mapping for key. A <tt>null</tt> return can
* also indicate that the map previously associated <tt>null</tt>
* with the specified key.
*/
public Object remove(Object key) {
Entry e = removeEntryForKey(key);
-
- return ((e == null) ? e : e.value);
+ return(e == null ? e : e.value);
}
/**
* Removes and returns the entry associated with the specified key in the
- * ConcurrentHashMap. Returns null if the ConcurrentHashMap contains no
+ * ConcurrentHashMap. Returns null if the ConcurrentHashMap contains no
* mapping for this key.
*/
private Entry removeEntryForKey(Object key) {
Object k = maskNull(key);
int hash = hash(k);
-
synchronized (this) {
int i = hash & (table.length - 1);
Entry e = table[i];
- if (e == null) {
- return null;
- }
-
- if ((e.hash == hash) && eq(k, e.key)) {
+ if (e == null) return null;
+ if (e.hash == hash && eq(k, e.key)) {
size--;
table[i] = e.next;
-
return e;
}
Entry prev = e;
-
for (e = e.next; e != null; prev = e, e = e.next) {
- if ((e.hash == hash) && eq(k, e.key)) {
+ if (e.hash == hash && eq(k, e.key)) {
size--;
prev.next = e.next;
-
return e;
}
}
}
-
return null;
}
@@ -550,41 +472,31 @@
* Special version of remove for EntrySet.
*/
private Entry removeMapping(Object o) {
- if (!(o instanceof Map.Entry)) {
- return null;
- }
+ if (!(o instanceof Map.Entry)) return null;
Map.Entry entry = (Map.Entry) o;
Object k = maskNull(entry.getKey());
int hash = hash(k);
-
synchronized (this) {
int i = hash & (table.length - 1);
Entry e = table[i];
- if (e == null) {
- return null;
- }
-
- if ((e.hash == hash) && e.equals(entry)) {
+ if (e == null) return null;
+ if (e.hash == hash && e.equals(entry)) {
size--;
table[i] = e.next;
-
return e;
}
Entry prev = e;
-
for (e = e.next; e != null; prev = e, e = e.next) {
- if ((e.hash == hash) && e.equals(entry)) {
+ if (e.hash == hash && e.equals(entry)) {
size--;
prev.next = e.next;
-
return e;
}
}
}
-
return null;
}
@@ -600,30 +512,23 @@
* Return an arbitrary entry index.
*/
private int randomEntryIndex() {
- if (randomEntry == RANDOMS.length) {
+ if(randomEntry == RANDOMS.length)
randomEntry = 0;
- }
-
- return (int) (RANDOMS[randomEntry++] * table.length);
+ return(int) (RANDOMS[randomEntry++] * table.length);
}
public Map.Entry removeRandom() {
- if (size == 0) {
+ if (size == 0)
return null;
- }
synchronized (this) {
int random = randomEntryIndex();
- int index = findEntry(random, (random % 2) == 0, false);
-
- if (index == -1) {
+ int index = findEntry(random, random % 2 == 0, false);
+ if (index == -1)
return null;
- }
-
Entry rem = table[index];
table[index] = rem.next;
size--;
-
return rem;
}
}
@@ -635,22 +540,16 @@
private int findEntry(int start, boolean forward, boolean searchedOther) {
if (forward) {
for (int i = start; i < table.length; i++)
- if (table[i] != null) {
+ if (table[i] != null)
return i;
- }
-
- return (searchedOther || (start == 0)) ? (-1)
- : findEntry(start - 1,
- false, true);
+ return(searchedOther || start == 0) ? -1
+ : findEntry(start - 1, false, true);
} else {
for (int i = start; i >= 0; i--)
- if (table[i] != null) {
+ if (table[i] != null)
return i;
- }
-
- return (searchedOther || (start == (table.length - 1))) ? (-1)
- : findEntry(start +
- 1, true, true);
+ return(searchedOther || start == table.length - 1) ? -1
+ : findEntry(start + 1, true, true);
}
}
@@ -663,26 +562,20 @@
/**
* Returns <tt>true</tt> if this map maps one or more keys to the
* specified value.
- *
+ *
* @param value value whose presence in this map is to be tested.
* @return <tt>true</tt> if this map maps one or more keys to the
* specified value.
*/
public final boolean containsValue(Object value) {
- if (value == null) {
- return containsNullValue();
- }
-
- Entry[] tab = table;
+ if (value == null) return containsNullValue();
- for (int i = 0; i < tab.length; i++) {
- for (Entry e = tab[i]; e != null; e = e.next) {
- if (value.equals(e.value)) {
- return true;
- }
+ Entry tab[] = table;
+ for (int i = 0; i < tab.length ; i++) {
+ for (Entry e = tab[i] ; e != null ; e = e.next) {
+ if (value.equals(e.value)) return true;
}
}
-
return false;
}
@@ -690,23 +583,19 @@
* Special-case code for containsValue with null argument
*/
private boolean containsNullValue() {
- Entry[] tab = table;
-
- for (int i = 0; i < tab.length; i++) {
- for (Entry e = tab[i]; e != null; e = e.next) {
- if (e.value == null) {
- return true;
- }
+ Entry tab[] = table;
+ for (int i = 0; i < tab.length ; i++) {
+ for (Entry e = tab[i] ; e != null ; e = e.next) {
+ if (e.value == null) return true;
}
}
-
return false;
}
/**
* Returns a shallow copy of this <tt>ConcurrentHashMap</tt> instance: the
* keys and values themselves are not cloned.
- *
+ *
* @return a shallow copy of this map.
*/
public final Object clone() {
@@ -717,116 +606,6 @@
return new Entry(h, k, v, n);
}
- /**
- * Returns a set view of the keys contained in this map. The set is
- * backed by the map, so changes to the map are reflected in the set, and
- * vice-versa. The set supports element removal, which removes the
- * corresponding mapping from this map, via the <tt>Iterator.remove</tt>,
- * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and
- * <tt>clear</tt> operations. It does not support the <tt>add</tt> or
- * <tt>addAll</tt> operations.
- *
- * @return a set view of the keys contained in this map.
- */
- public final Set keySet() {
- Set ks = keySet;
-
- return ((ks != null) ? ks : (keySet = new KeySet()));
- }
-
- /**
- * Returns a collection view of the values contained in this map. The
- * collection is backed by the map, so changes to the map are reflected in
- * the collection, and vice-versa. The collection supports element
- * removal, which removes the corresponding mapping from this map, via the
- * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
- * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations.
- * It does not support the <tt>add</tt> or <tt>addAll</tt> operations.
- *
- * @return a collection view of the values contained in this map.
- */
- public final Collection values() {
- Collection vs = values;
-
- return ((vs != null) ? vs : (values = new Values()));
- }
-
- /**
- * Returns a collection view of the mappings contained in this map. Each
- * element in the returned collection is a <tt>Map.Entry</tt>. The
- * collection is backed by the map, so changes to the map are reflected in
- * the collection, and vice-versa. The collection supports element
- * removal, which removes the corresponding mapping from the map, via the
- * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
- * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations.
- * It does not support the <tt>add</tt> or <tt>addAll</tt> operations.
- *
- * @return a collection view of the mappings contained in this map.
- * @see Map.Entry
- */
- public final Set entrySet() {
- Set es = entrySet;
-
- return ((es != null) ? es : (entrySet = new EntrySet()));
- }
-
- /**
- * Save the state of the <tt>ConcurrentHashMap</tt> instance to a stream
- * (i.e., serialize it).
- *
- * @serialData The <i>capacity</i> of the ConcurrentHashMap (the length of
- * the bucket array) is emitted (int), followed by the <i>size</i> of the
- * ConcurrentHashMap (the number of key-value mappings), followed by the key
- * (Object) and value (Object) for each key-value mapping represented by the
- * ConcurrentHashMap The key-value mappings are emitted in the order that
- * they are returned by <tt>entrySet().iterator()</tt>.
- *
- */
- private void writeObject(ObjectOutputStream s) throws IOException {
- // Write out the threshold, loadfactor, and any hidden stuff
- s.defaultWriteObject();
-
- // Write out number of buckets
- s.writeInt(table.length);
-
- // Write out size (number of Mappings)
- s.writeInt(size);
- s.writeInt(maxSize);
-
- // Write out keys and values (alternating)
- for (Iterator i = entrySet().iterator(); i.hasNext();) {
- Map.Entry e = (Map.Entry) i.next();
- s.writeObject(e.getKey());
- s.writeObject(e.getValue());
- }
- }
-
- /**
- * Reconstitute the <tt>ConcurrentHashMap</tt> instance from a stream (i.e.,
- * deserialize it).
- */
- private void readObject(ObjectInputStream s)
- throws IOException, ClassNotFoundException {
- // Read in the threshold, loadfactor, and any hidden stuff
- s.defaultReadObject();
-
- // Read in number of buckets and allocate the bucket array;
- int numBuckets = s.readInt();
- table = new Entry[numBuckets];
-
- // Read in size (number of Mappings)
- int size = s.readInt();
- int maxSize = s.readInt();
-
- // Read the keys and values, and put the mappings in the
- // ConcurrentHashMap
- for (int i = 0; i < size; i++) {
- Object key = s.readObject();
- Object value = s.readObject();
- put(key, value);
- }
- }
-
protected static class Entry implements Map.Entry {
final Object key;
Object value;
@@ -854,34 +633,26 @@
public Object setValue(Object newValue) {
Object oldValue = value;
value = newValue;
-
return oldValue;
}
public boolean equals(Object o) {
- if (!(o instanceof Map.Entry)) {
- return false;
- }
-
+ if (!(o instanceof Map.Entry)) return false;
Map.Entry e = (Map.Entry) o;
Object k1 = getKey();
Object k2 = e.getKey();
-
- if ((k1 == k2) || ((k1 != null) && k1.equals(k2))) {
+ if (k1 == k2 || (k1 != null && k1.equals(k2))) {
Object v1 = getValue();
Object v2 = e.getValue();
-
- if ((v1 == v2) || ((v1 != null) && v1.equals(v2))) {
+ if (v1 == v2 || (v1 != null && v1.equals(v2)))
return true;
- }
}
-
return false;
}
public int hashCode() {
- return ((key == NULL_KEY) ? 0 : key.hashCode()) ^
- ((value == null) ? 0 : value.hashCode());
+ return(key==NULL_KEY ? 0 : key.hashCode()) ^
+ (value==null ? 0 : value.hashCode());
}
public String toString() {
@@ -895,6 +666,11 @@
}
}
+ // Types of Enumerations/Iterations
+ private static final int KEYS = 0;
+ private static final int VALUES = 1;
+ private static final int ENTRIES = 2;
+
/**
* Map iterator.
*/
@@ -917,68 +693,77 @@
if (entry != null) {
return true;
}
-
while (index >= stopIndex) {
if ((entry = table[index--]) != null) {
return true;
}
}
-
if (stopIndex == 0) {
index = table.length - 1;
stopIndex = startIndex + 1;
-
while (index >= stopIndex) {
if ((entry = table[index--]) != null) {
return true;
}
}
}
-
return false;
}
public Object next() {
- if (!hasNext()) {
+ if (!hasNext())
throw new NoSuchElementException();
- }
-
Entry e = lastReturned = entry;
entry = e.next;
-
- return (type == KEYS) ? e.key : ((type == VALUES) ? e.value : e);
+ return type == KEYS ? e.key : (type == VALUES ? e.value : e);
}
public void remove() {
- if (lastReturned == null) {
+ if (lastReturned == null)
throw new IllegalStateException();
- }
-
synchronized (ConcurrentHashMap.this) {
Entry[] tab = ConcurrentHashMap.this.table;
int index = (lastReturned.hash & 0x7FFFFFFF) % tab.length;
for (Entry e = tab[index], prev = null; e != null;
- prev = e, e = e.next) {
+ prev = e, e = e.next) {
if (e == lastReturned) {
- if (prev == null) {
+ if (prev == null)
tab[index] = e.next;
- } else {
+ else
prev.next = e.next;
- }
-
size--;
lastReturned = null;
-
return;
}
}
-
throw new Error("Iterated off table when doing remove");
}
}
}
+ // Views
+
+ private transient Set entrySet = null;
+ private transient Set keySet = null;
+ private transient Collection values = null;
+
+ /**
+ * Returns a set view of the keys contained in this map. The set is
+ * backed by the map, so changes to the map are reflected in the set, and
+ * vice-versa. The set supports element removal, which removes the
+ * corresponding mapping from this map, via the <tt>Iterator.remove</tt>,
+ * <tt>Set.remove</tt>, <tt>removeAll</tt>, <tt>retainAll</tt>, and
+ * <tt>clear</tt> operations. It does not support the <tt>add</tt> or
+ * <tt>addAll</tt> operations.
+ *
+ * @return a set view of the keys contained in this map.
+ */
+ public final Set keySet() {
+ Set ks = keySet;
+ return(ks != null ? ks : (keySet = new KeySet()));
+ }
+
private final class KeySet extends AbstractSet {
public Iterator iterator() {
return new HashIterator(KEYS, table.length - 1);
@@ -1001,6 +786,22 @@
}
}
+ /**
+ * Returns a collection view of the values contained in this map. The
+ * collection is backed by the map, so changes to the map are reflected in
+ * the collection, and vice-versa. The collection supports element
+ * removal, which removes the corresponding mapping from this map, via the
+ * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
+ * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations.
+ * It does not support the <tt>add</tt> or <tt>addAll</tt> operations.
+ *
+ * @return a collection view of the values contained in this map.
+ */
+ public final Collection values() {
+ Collection vs = values;
+ return(vs != null ? vs : (values = new Values()));
+ }
+
private final class Values extends AbstractCollection {
public Iterator iterator() {
return new HashIterator(VALUES, table.length - 1);
@@ -1019,20 +820,34 @@
}
}
+ /**
+ * Returns a collection view of the mappings contained in this map. Each
+ * element in the returned collection is a <tt>Map.Entry</tt>. The
+ * collection is backed by the map, so changes to the map are reflected in
+ * the collection, and vice-versa. The collection supports element
+ * removal, which removes the corresponding mapping from the map, via the
+ * <tt>Iterator.remove</tt>, <tt>Collection.remove</tt>,
+ * <tt>removeAll</tt>, <tt>retainAll</tt>, and <tt>clear</tt> operations.
+ * It does not support the <tt>add</tt> or <tt>addAll</tt> operations.
+ *
+ * @return a collection view of the mappings contained in this map.
+ * @see Map.Entry
+ */
+ public final Set entrySet() {
+ Set es = entrySet;
+ return(es != null ? es : (entrySet = new EntrySet()));
+ }
+
private final class EntrySet extends AbstractSet {
public Iterator iterator() {
return new HashIterator(ENTRIES, table.length - 1);
}
public boolean contains(Object o) {
- if (!(o instanceof Map.Entry)) {
- return false;
- }
-
+ if (!(o instanceof Map.Entry)) return false;
Map.Entry e = (Map.Entry) o;
Entry candidate = getEntry(e.getKey());
-
- return (candidate != null) && candidate.equals(e);
+ return candidate != null && candidate.equals(e);
}
public boolean remove(Object o) {
@@ -1045,6 +860,65 @@
public void clear() {
ConcurrentHashMap.this.clear();
+ }
+ }
+
+ /**
+ * Save the state of the <tt>ConcurrentHashMap</tt> instance to a stream
+ * (i.e., serialize it).
+ *
+ * @serialData The <i>capacity</i> of the ConcurrentHashMap(the length of
+ * the bucket array) is emitted(int), followed by the <i>size</i> of the
+ * ConcurrentHashMap(the number of key-value mappings), followed by the key
+ * (Object) and value(Object) for each key-value mapping represented by the
+ * ConcurrentHashMap The key-value mappings are emitted in the order that
+ * they are returned by <tt>entrySet().iterator()</tt>.
+ *
+ */
+ private void writeObject(ObjectOutputStream s) throws IOException {
+ // Write out the threshold, loadfactor, and any hidden stuff
+ s.defaultWriteObject();
+
+ // Write out number of buckets
+ s.writeInt(table.length);
+
+ // Write out size(number of Mappings)
+ s.writeInt(size);
+ s.writeInt(maxSize);
+
+ // Write out keys and values(alternating)
+ for (Iterator i = entrySet().iterator(); i.hasNext(); ) {
+ Map.Entry e = (Map.Entry) i.next();
+ s.writeObject(e.getKey());
+ s.writeObject(e.getValue());
+ }
+ }
+
+ private static final long serialVersionUID = -6452706556724125778L;
+
+ /**
+ * Reconstitute the <tt>ConcurrentHashMap</tt> instance from a stream(i.e.,
+ * deserialize it).
+ */
+ private void readObject(ObjectInputStream s)
+ throws IOException, ClassNotFoundException {
+ // Read in the threshold, loadfactor, and any hidden stuff
+ s.defaultReadObject();
+
+ // Read in number of buckets and allocate the bucket array;
+ int numBuckets = s.readInt();
+ table = new Entry[numBuckets];
+
+ // Read in size(number of Mappings)
+ int size = s.readInt();
+ int maxSize = s.readInt();
+
+ // Read the keys and values, and put the mappings in the
+ // ConcurrentHashMap
+ for (int i=0; i<size; i++) {
+ Object key = s.readObject();
+ Object value = s.readObject();
+ put(key, value);
}
}
}
Modified: incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/ConcurrentHashSet.java
URL: http://svn.apache.org/viewvc/incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/ConcurrentHashSet.java?rev=418401&r1=418400&r2=418401&view=diff
==============================================================================
--- incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/ConcurrentHashSet.java (original)
+++ incubator/openjpa/trunk/openjpa-lib/src/main/java/org/apache/openjpa/lib/util/concurrent/ConcurrentHashSet.java Fri Jun 30 15:37:18 2006
@@ -1,13 +1,10 @@
/*
* Copyright 2006 The Apache Software Foundation.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
+ * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
@@ -15,25 +12,24 @@
*/
package org.apache.openjpa.lib.util.concurrent;
-import org.apache.commons.collections.map.*;
-import org.apache.commons.collections.set.*;
-
import java.io.*;
-
import java.util.*;
-
+import org.apache.commons.collections.map.*;
+import org.apache.commons.collections.set.*;
/**
- * <p>A concurrent set.</p>
- *
- * @author Abe White
- * @nojavadoc */
+ * A concurrent set.
+ *
+ * @author Abe White
+ * @nojavadoc
+ */
public class ConcurrentHashSet implements Set, Serializable {
private static final Object DUMMY_VAL = new Object();
+
private final Set _set;
/**
- * Construct a set with the given reference type.
+ * Construct a set with the given reference type.
*/
public ConcurrentHashSet() {
_set = MapBackedSet.decorate(new ConcurrentHashMap(), DUMMY_VAL);
@@ -96,14 +92,10 @@
}
public boolean equals(Object obj) {
- if (this == obj) {
+ if (this == obj)
return true;
- }
-
- if (obj instanceof ConcurrentHashSet) {
+ if (obj instanceof ConcurrentHashSet)
obj = ((ConcurrentHashSet) obj)._set;
- }
-
return _set.equals(obj);
}
}