001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.lib.util.concurrent;
020:
021: import java.io.Serializable;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.Set;
025:
026: import org.apache.commons.collections.set.MapBackedSet;
027:
028: /**
029: * A concurrent set whose values may be stored as weak or soft references.
030: *
031: * @author Abe White
032: * @nojavadoc
033: */
034: public class ConcurrentReferenceHashSet implements Set, Serializable {
035:
036: /**
037: * Hard reference marker.
038: */
039: public static final int HARD = 0;
040:
041: /**
042: * Soft reference marker.
043: */
044: public static final int SOFT = 1;
045:
046: /**
047: * Weak reference marker.
048: */
049: public static final int WEAK = 2;
050:
051: private static final Object DUMMY_VAL = new Object();
052:
053: private final Set _set;
054:
055: /**
056: * Construct a set with the given reference type.
057: */
058: public ConcurrentReferenceHashSet(int refType) {
059: if (refType == HARD)
060: _set = MapBackedSet.decorate(new ConcurrentHashMap(),
061: DUMMY_VAL);
062: else {
063: int mapRefType = (refType == WEAK) ? ConcurrentReferenceHashMap.WEAK
064: : ConcurrentReferenceHashMap.SOFT;
065: _set = MapBackedSet
066: .decorate(
067: new ConcurrentReferenceHashMap(mapRefType,
068: ConcurrentReferenceHashMap.HARD),
069: DUMMY_VAL);
070: }
071: }
072:
073: public boolean add(Object obj) {
074: return _set.add(obj);
075: }
076:
077: public boolean addAll(Collection coll) {
078: return _set.addAll(coll);
079: }
080:
081: public void clear() {
082: _set.clear();
083: }
084:
085: public boolean contains(Object obj) {
086: return _set.contains(obj);
087: }
088:
089: public boolean containsAll(Collection coll) {
090: return _set.containsAll(coll);
091: }
092:
093: public boolean isEmpty() {
094: return _set.isEmpty();
095: }
096:
097: public Iterator iterator() {
098: return _set.iterator();
099: }
100:
101: public boolean remove(Object obj) {
102: return _set.remove(obj);
103: }
104:
105: public boolean removeAll(Collection coll) {
106: return _set.removeAll(coll);
107: }
108:
109: public boolean retainAll(Collection coll) {
110: return _set.retainAll(coll);
111: }
112:
113: public int size() {
114: return _set.size();
115: }
116:
117: public Object[] toArray() {
118: return _set.toArray();
119: }
120:
121: public Object[] toArray(Object[] arr) {
122: return _set.toArray(arr);
123: }
124:
125: public int hashCode() {
126: return _set.hashCode();
127: }
128:
129: public boolean equals(Object obj) {
130: if (this == obj)
131: return true;
132: if (obj instanceof ConcurrentReferenceHashSet)
133: obj = ((ConcurrentReferenceHashSet) obj)._set;
134: return _set.equals(obj);
135: }
136: }
|