Return-Path: X-Original-To: apmail-river-commits-archive@www.apache.org Delivered-To: apmail-river-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 490C29F55 for ; Wed, 21 Sep 2011 15:02:36 +0000 (UTC) Received: (qmail 26015 invoked by uid 500); 21 Sep 2011 15:02:36 -0000 Delivered-To: apmail-river-commits-archive@river.apache.org Received: (qmail 25993 invoked by uid 500); 21 Sep 2011 15:02:36 -0000 Mailing-List: contact commits-help@river.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@river.apache.org Delivered-To: mailing list commits@river.apache.org Received: (qmail 25972 invoked by uid 99); 21 Sep 2011 15:02:35 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 21 Sep 2011 15:02:35 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 21 Sep 2011 15:02:27 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 228BB238897D; Wed, 21 Sep 2011 15:02:05 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1173698 [2/2] - /river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ Date: Wed, 21 Sep 2011 15:02:03 -0000 To: commits@river.apache.org From: peter_firmstone@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20110921150205.228BB238897D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceList.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceList.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceList.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceList.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,155 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.util.Collection; +import java.util.List; +import java.util.ListIterator; + +/** + * A List implementation that uses References. + * + * This enables a Collection or List to contain, softly, weakly or strongly referenced + * objects. Objects that are no longer reachable will simply vanish from + * the Collection. + * + * For example, this could be used as an Object cache containing softly reachable + * objects which are only collected when the jvm is experiencing low memory + * conditions. + * + * Synchronisation must be performed by the underlying List, it cannot be + * performed externally, since every read is also potentially a mutable + * change, caused by removal of garbage collected Objects. + * + * Removal of garbage collected objects is performed before iteration, but not + * during, instead some Object's returned during iteration may be null. + * + * @author Peter Firmstone. + */ +class ReferenceList extends ReferenceCollection implements List { + private final List> list; + ReferenceList(List> list, Ref type){ + super(list, type); + this.list = list; + } + + private ReferenceList(List> list, Ref type, ReferenceQueue queue){ + super(list, type, queue); + this.list = list; + } + + public boolean addAll(int index, Collection c) { + processQueue(); + return list.addAll(index, wrapColl(c, true)); + } + + public T get(int index) { + processQueue(); + return list.get(index).get(); + } + + public T set(int index, T element) { + processQueue(); + return list.set(index, wrapObj(element, true)).get(); + } + + public void add(int index, T element) { + processQueue(); + list.add(index, wrapObj(element, true)); + } + + public T remove(int index) { + processQueue(); + return list.remove(index).get(); + } + + @SuppressWarnings("unchecked") + public int indexOf(Object o) { + processQueue(); + return list.indexOf(wrapObj((T) o, false)); + } + + @SuppressWarnings("unchecked") + public int lastIndexOf(Object o) { + processQueue(); + return list.lastIndexOf(wrapObj((T)o, false)); + } + + public ListIterator listIterator() { + processQueue(); + return new ListIteratorWrap(list.listIterator(), this); + } + + public ListIterator listIterator(int index) { + processQueue(); + return new ListIteratorWrap(list.listIterator(index), this); + } + + public List subList(int fromIndex, int toIndex) { + processQueue(); + List sub = new ReferenceList(list.subList(fromIndex, toIndex), getType(), getQueue()); + return sub; + } + + private class ListIteratorWrap implements ListIterator{ + ListIterator> iterator; + ReferenceCollection col; + private ListIteratorWrap(ListIterator> iterator, ReferenceCollection c){ + this.iterator = iterator; + } + + public boolean hasNext() { + return iterator.hasNext(); + } + + public T next() { + return iterator.next().get(); + } + + public boolean hasPrevious() { + return iterator.hasPrevious(); + } + + public T previous() { + return iterator.previous().get(); + } + + public int nextIndex() { + return iterator.nextIndex(); + } + + public int previousIndex() { + return iterator.previousIndex(); + } + + public void remove() { + iterator.remove(); + } + + public void set(T e) { + iterator.set(col.wrapObj(e, true)); + } + + public void add(T e) { + iterator.add(col.wrapObj( e, true)); + } + } +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceList.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceMap.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceMap.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceMap.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceMap.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,282 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.util.Collection; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Set; + +/** + * ReferenceMap is a wrapper object that encapsulates another Map implementation + * which it uses to store references. + * + * Synchronisation must be performed by the underlying map, using external + * synchronisation will fail, since each read is also potentially a + * mutation to remove a garbage collected reference. + * + * Implementation note: + * + * ReferenceQueue's must be allowed to be used by any views when creating + * new References using the wrapper, so the Reference is passed to the + * correct ReferenceQueue when it becomes unreachable. + * + * There is only one ReferenceQueue for each Collection, whether that is a + * Collection of Key's or Values. Defensive copies cannot be made by the + * underlying encapsulated collections. + * + * @param + * @param + * @author Peter Firmstone. + */ +class ReferenceMap implements Map, ReferenceQueueProcessor { + + // ConcurrentHashMap must be protected from null values; + private final Ref keyRef; + private final Ref valRef; + private final Map, Reference> map; + private final ReferenceQueue keyQueue; + private final ReferenceQueue valQueue; + + ReferenceMap(Map,Reference> map, Ref key, Ref val){ + if (map == null || key == null || val == null ) throw new IllegalArgumentException("Null not allowed"); + this.map = map; + keyQueue = new ReferenceQueue(); + valQueue = new ReferenceQueue(); + keyRef = key; + valRef = val; + } + ReferenceMap(Map,Reference> map, Ref key, Ref val, ReferenceQueue keyQueue, ReferenceQueue valQueue){ + if (map == null || key == null || val == null ) throw new IllegalArgumentException("Null not allowed"); + this.map = map; + this.keyQueue = keyQueue; + this.valQueue = valQueue; + keyRef = key; + valRef = val; + } + protected ReferenceQueue getValQueue(){ + return valQueue; + } + + protected ReferenceQueue getKeyQueue(){ + return keyQueue; + } + + /* + * The following three methods are suitable implementations for subclasses also. + */ + @Override + public int hashCode() { + int hash = 3; + hash = 47 * hash + (this.keyRef != null ? this.keyRef.hashCode() : 0); + hash = 47 * hash + (this.valRef != null ? this.valRef.hashCode() : 0); + hash = 47 * hash + (this.map != null ? this.map.hashCode() : 0); + hash = 47 * hash + (this.keyQueue != null ? this.keyQueue.hashCode() : 0); + hash = 47 * hash + (this.valQueue != null ? this.valQueue.hashCode() : 0); + hash = 47 * hash + (this.getClass().hashCode()); + return hash; + } + + public boolean equals(Object o){ + if (o == this) return true; + if ( !(this.getClass().equals(o.getClass())) ) return false; + if (!( o instanceof ReferenceMap)) return false; + ReferenceMap that = (ReferenceMap) o; + if ( !this.keyRef.equals(that.keyRef) ) return false; + if ( !this.valRef.equals(that.valRef) ) return false; + if ( !this.keyQueue.equals(that.keyQueue) ) return false; + if ( !this.valQueue.equals(that.valQueue) ) return false; + return ( map.equals(that.map)); + } + + public String toString(){ + return map.toString(); + } + + /** + * Removes all associations from this map. + */ + public void clear() { + processQueue(); + map.clear(); + } + + @SuppressWarnings(value = "unchecked") + public boolean containsKey(Object key) { + processQueue(); + if (key == null) { + return false; + } + Reference kRef = wrapKey((K) key, false); + return map.containsKey(kRef); + } + + @SuppressWarnings("unchecked") + public boolean containsValue(Object value) { + processQueue(); + if (value == null) { + return false; + } + return map.containsValue(wrapVal((V) value, false)); + } + + /** + * Returns a Set view of the mappings contained in the underlying map. + * + * The behaviour of this Set depends on the underlying Map passed in + * at construction time. + * + * The set supports element removal, which removes the corresponding + * mapping from the map, via the Iterator.remove, Set.remove, removeAll, + * retainAll and clear operations. + * + * This method does not support the add or addAll operations. + * + * @return + */ + public Set> entrySet() { + return new EntrySetFacade, Entry,Reference>>( + map.entrySet(), + new EntryFacadeConverter(valQueue, valRef, this) + ); + } + + /** + * Returns value associated with given key, or null if none. + */ + public V get(Object key) { + processQueue(); + if (key == null) { + return null; + } + @SuppressWarnings(value = "unchecked") + Reference refVal = map.get(wrapKey((K) key, false)); + if (refVal != null) { + return refVal.get(); + } + return null; + } + + public boolean isEmpty() { + processQueue(); + return map.isEmpty(); + } + + /** + * The key Set returned, is encapsulated by ReferenceSet, which encapsulates + * it's objects using the same Ref type as ReferenceMap + * + * @see Ref + * @see ReferenceSet + * @return + */ + public Set keySet() { + processQueue(); + return new ReferenceSet(map.keySet(), keyRef, keyQueue); + } + + public void processQueue() { + Reference r = null; + while ((r = keyQueue.poll()) != null) { + map.remove(r); + } + Collection> values = map.values(); + while ((r = valQueue.poll()) != null) { + values.remove(r); //Removes mapping. + } + } + + /** + * Associates value with given key, returning value previously associated + * with key, or null if none. + * @param key - Key + * @param value - Value + * @return previous value or null + */ + public V put(K key, V value) { + processQueue(); + if (key == null) { + return null; + } + Reference val = map.put(wrapKey(key, true), wrapVal(value, true)); + if (val != null) { + return val.get(); + } + return null; + } + + /** + * + * @param m + */ + public void putAll(Map m) { + if (m.isEmpty()) return; + Map, Reference> refMap + = new HashMap, Reference>(m.size()); + Iterator> i = m.entrySet().iterator(); + while (i.hasNext()){ + Map.Entry ent = i.next(); + refMap.put(wrapKey(ent.getKey(), true),wrapVal(ent.getValue(), true)); + } + map.putAll(refMap); + } + + /** + * Removes association for given key, returning value previously associated + * with key, or null if none. + */ + public V remove(Object key) { + processQueue(); + if (key == null) { + return null; + } + @SuppressWarnings(value = "unchecked") + Reference val = map.remove(wrapKey((K) key, false)); + if (val != null) { + return val.get(); + } + return null; + } + + public int size() { + processQueue(); + return map.size(); + } + + /** + * Returns collection containing all values currently held in this map. + */ + public Collection values() { + processQueue(); + return new ReferenceCollection(map.values(), valRef, valQueue); + } + + Reference wrapVal(V val, boolean enque) { + return ReferenceFactory.create(val, enque == true ? valQueue : null, valRef); + } + + Reference wrapKey(K key, boolean enque) { + return ReferenceFactory.create(key, enque == true ? keyQueue : null, keyRef); + } + +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceMap.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceNavigableMap.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceNavigableMap.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceNavigableMap.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceNavigableMap.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.util.Map.Entry; +import java.util.NavigableMap; +import java.util.NavigableSet; + +/** + * + * @author Peter Firmstone. + */ +class ReferenceNavigableMap extends ReferenceSortedMap implements NavigableMap { + private final NavigableMap, Reference> map; + private final Ref keyRef; + private final Ref valRef; + + ReferenceNavigableMap(NavigableMap, Reference> map, Ref keyRef, Ref valRef){ + super(map, keyRef, valRef); + this.map = map; + this.keyRef = keyRef; + this.valRef = valRef; + } + + ReferenceNavigableMap( NavigableMap, Reference> map, + Ref keyRef, Ref valRef, ReferenceQueue keyQueue, ReferenceQueue valQueue){ + super(map, keyRef, valRef, keyQueue, valQueue); + this.map = map; + this.keyRef = keyRef; + this.valRef = valRef; + } + + public Entry lowerEntry(K key) { + processQueue(); + return new ReferenceEntryFacade( + map.lowerEntry(wrapKey(key, false)), + valRef, + getValQueue() + ); + } + + public K lowerKey(K key) { + processQueue(); + return map.lowerKey(wrapKey(key, false)).get(); + } + + public Entry floorEntry(K key) { + processQueue(); + return new ReferenceEntryFacade( + map.floorEntry(wrapKey(key, false)), + valRef, + getValQueue() + ); + } + + public K floorKey(K key) { + processQueue(); + return map.floorKey(wrapKey(key, false)).get(); + } + + public Entry ceilingEntry(K key) { + processQueue(); + return new ReferenceEntryFacade( + map.ceilingEntry(wrapKey(key, false)), + valRef, + getValQueue() + ); + } + + public K ceilingKey(K key) { + processQueue(); + return map.ceilingKey(wrapKey(key, false)).get(); + } + + public Entry higherEntry(K key) { + processQueue(); + return new ReferenceEntryFacade( + map.higherEntry(wrapKey(key, false)), + valRef, + getValQueue() + ); + } + + public K higherKey(K key) { + processQueue(); + return map.higherKey(wrapKey(key, false)).get(); + } + + public Entry firstEntry() { + processQueue(); + return new ReferenceEntryFacade( + map.firstEntry(), + valRef, + getValQueue() + ); + } + + public Entry lastEntry() { + processQueue(); + return new ReferenceEntryFacade( + map.lastEntry(), + valRef, + getValQueue() + ); + } + + public Entry pollFirstEntry() { + processQueue(); + return new ReferenceEntryFacade( + map.pollFirstEntry(), + valRef, + getValQueue() + ); + } + + public Entry pollLastEntry() { + processQueue(); + return new ReferenceEntryFacade( + map.pollLastEntry(), + valRef, + getValQueue() + ); + } + + public NavigableMap descendingMap() { + processQueue(); + return new ReferenceNavigableMap( + map.descendingMap(), + keyRef, + valRef, + getKeyQueue(), + getValQueue() + ); + } + + public NavigableSet navigableKeySet() { + processQueue(); + return new ReferenceNavigableSet(map.navigableKeySet(), keyRef, getKeyQueue()); + } + + public NavigableSet descendingKeySet() { + processQueue(); + return new ReferenceNavigableSet(map.descendingKeySet(), keyRef, getKeyQueue()); + } + + public NavigableMap subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive) { + processQueue(); + return new ReferenceNavigableMap( + map.subMap( + wrapKey(fromKey, false), + fromInclusive, + wrapKey(toKey, false), + toInclusive + ), + keyRef, + valRef, + getKeyQueue(), + getValQueue() + ); + + } + + public NavigableMap headMap(K toKey, boolean inclusive) { + processQueue(); + return new ReferenceNavigableMap( + map.headMap(wrapKey(toKey, false),inclusive), + keyRef, + valRef, + getKeyQueue(), + getValQueue() + ); + } + + public NavigableMap tailMap(K fromKey, boolean inclusive) { + processQueue(); + return new ReferenceNavigableMap( + map.tailMap(wrapKey(fromKey, false),inclusive), + keyRef, + valRef, + getKeyQueue(), + getValQueue() + ); + } +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceNavigableMap.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceNavigableSet.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceNavigableSet.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceNavigableSet.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceNavigableSet.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,118 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.util.Iterator; +import java.util.NavigableSet; + +/** + * + * @param + * @author Peter Firmstone. + */ +public class ReferenceNavigableSet + extends ReferenceSortedSet implements NavigableSet { + private final NavigableSet> set; + private final Ref type; + + public ReferenceNavigableSet(NavigableSet> set, Ref type){ + super(set, type); + this.set = set; + this.type = type; + } + + ReferenceNavigableSet(NavigableSet> set, Ref type, ReferenceQueue queue){ + super(set, type, queue); + this.set = set; + this.type = type; + } + + public T lower(T e) { + processQueue(); + return set.lower(wrapObj(e, false)).get(); + } + + public T floor(T e) { + processQueue(); + return set.floor(wrapObj(e, false)).get(); + } + + public T ceiling(T e) { + processQueue(); + return set.ceiling(wrapObj(e, false)).get(); + } + + public T higher(T e) { + processQueue(); + return set.higher(wrapObj(e, false)).get(); + } + + public T pollFirst() { + processQueue(); + return set.pollFirst().get(); + } + + public T pollLast() { + processQueue(); + return set.pollLast().get(); + } + + public NavigableSet descendingSet() { + processQueue(); + return new ReferenceNavigableSet(set.descendingSet(), type, getQueue()); + } + + public Iterator descendingIterator() { + processQueue(); + return new ReferenceIterator(set.descendingIterator()); + } + + public NavigableSet subSet(T fromElement, boolean fromInclusive, T toElement, boolean toInclusive) { + processQueue(); + return new ReferenceNavigableSet( + set.subSet( + wrapObj(fromElement, false), + fromInclusive, + wrapObj(toElement, false), + toInclusive + ), + type, + getQueue() + ); + } + + public NavigableSet headSet(T toElement, boolean inclusive) { + processQueue(); + return new ReferenceNavigableSet( + set.headSet(wrapObj(toElement, false), inclusive), + type, getQueue() + ); + } + + public NavigableSet tailSet(T fromElement, boolean inclusive) { + processQueue(); + return new ReferenceNavigableSet( + set.tailSet(wrapObj(fromElement, false), inclusive), + type, getQueue() + ); + } + +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceNavigableSet.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceQueueProcessor.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceQueueProcessor.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceQueueProcessor.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceQueueProcessor.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,29 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +/** + * + * @author peter + */ +interface ReferenceQueueProcessor { + + void processQueue(); + +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceQueueProcessor.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSet.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSet.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSet.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSet.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.util.Set; + +/** + * A Referenced Set. + * + * + * @see ReferenceCollection + * @author Peter Firmstone. + */ +class ReferenceSet extends ReferenceCollection implements Set{ + + ReferenceSet(Set> col, Ref type){ + super(col, type); + } + + ReferenceSet(Set> col, Ref type, ReferenceQueue queue){ + super(col, type, queue); + } +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSet.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedMap.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedMap.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedMap.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedMap.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,109 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.util.Comparator; +import java.util.SortedMap; + +/** + * + * @param + * @param + * @author peter + */ +class ReferenceSortedMap extends ReferenceMap implements SortedMap{ + private SortedMap, Reference> map; + private Ref keyRef; + private Ref valRef; + + ReferenceSortedMap(SortedMap, Reference> map, Ref keyRef, Ref valRef){ + super(map, keyRef, valRef); + this.map = map; + this.keyRef = keyRef; + this.valRef = valRef; + } + + ReferenceSortedMap( SortedMap, Reference> map, + Ref keyRef, Ref valRef, ReferenceQueue keyQueue, ReferenceQueue valQueue){ + super(map, keyRef, valRef, keyQueue, valQueue); + this.map = map; + this.keyRef = keyRef; + this.valRef = valRef; + } + + @SuppressWarnings("unchecked") + public Comparator comparator() { + processQueue(); + Comparator> c = map.comparator(); + if ( c instanceof ReferenceComparator){ + return ((ReferenceComparator) c).get(); + } + return null; + } + + public SortedMap subMap(K fromKey, K toKey) { + processQueue(); + return new ReferenceSortedMap( + map.subMap(wrapKey(fromKey, false), wrapKey(fromKey, false)), + keyRef, + valRef, + getKeyQueue(), + getValQueue() + ); + } + + + public SortedMap headMap(K toKey) { + processQueue(); + return new ReferenceSortedMap( + map.headMap(wrapKey(toKey, false)), + keyRef, + valRef, + getKeyQueue(), + getValQueue() + ); + } + + + public SortedMap tailMap(K fromKey) { + processQueue(); + return new ReferenceSortedMap( + map.tailMap(wrapKey(fromKey, false)), + keyRef, + valRef, + getKeyQueue(), + getValQueue() + ); + } + + + public K firstKey() { + processQueue(); + return map.firstKey().get(); + } + + + public K lastKey() { + processQueue(); + return map.lastKey().get(); + } + +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedMap.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedSet.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedSet.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedSet.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedSet.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,90 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.Reference; +import java.lang.ref.ReferenceQueue; +import java.util.Comparator; +import java.util.SortedSet; + +/** + * Referenced set supports sorting Object based on their natural ordering + * or a Comparator, which must be wrapped in a ReferenceComparator. + * + * @param + * @see Comparable + * @see Comparator + * @see ReferenceComparator + * @author Peter Firmstone. + */ +class ReferenceSortedSet extends ReferenceSet implements SortedSet { + private final SortedSet> set; + private final Ref type; + ReferenceSortedSet( SortedSet> set, Ref type){ + super(set, type); + this.set = set; + this.type = type; + } + + ReferenceSortedSet(SortedSet> set, Ref type, ReferenceQueue queue){ + super(set, type, queue); + this.set = set; + this.type = type; + } + + @SuppressWarnings("unchecked") + public Comparator comparator() { + processQueue(); + Comparator> c = set.comparator(); + if ( c instanceof ReferenceComparator){ + return ((ReferenceComparator) c).get(); + } + return null; + } + + public SortedSet subSet(T fromElement, T toElement) { + processQueue(); + Reference from = wrapObj(fromElement, false); + Reference to = wrapObj(toElement, false); + return new ReferenceSortedSet( set.subSet(from, to), type, getQueue()); + } + + public SortedSet headSet(T toElement) { + processQueue(); + Reference to = wrapObj(toElement, false); + return new ReferenceSortedSet(set.headSet(to), type, getQueue()); + } + + public SortedSet tailSet(T fromElement) { + processQueue(); + Reference from = wrapObj(fromElement, false); + return new ReferenceSortedSet(set.tailSet(from), type, getQueue()); + } + + public T first() { + processQueue(); + return set.first().get(); + } + + public T last() { + processQueue(); + return set.last().get(); + } + +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferenceSortedSet.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferencedQueue.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferencedQueue.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferencedQueue.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferencedQueue.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.Reference; +import java.util.Queue; + +/** + * + * @param + * @author Peter Firmstone. + */ +public class ReferencedQueue extends ReferenceCollection implements Queue { + private final Queue> queue; + private final Ref type; + + public ReferencedQueue( Queue> queue, Ref type){ + super(queue, type); + this.queue = queue; + this.type = type; + } + public boolean offer(T e) { + processQueue(); + Reference r = wrapObj(e, true); + return queue.offer(r); + } + + public T remove() { + processQueue(); + return queue.remove().get(); + } + + public T poll() { + processQueue(); + return queue.poll().get(); + } + + public T element() { + processQueue(); + return queue.element().get(); + } + + public T peek() { + processQueue(); + return queue.peek().get(); + } + +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/ReferencedQueue.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/SoftIdentityReferenceKey.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/SoftIdentityReferenceKey.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/SoftIdentityReferenceKey.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/SoftIdentityReferenceKey.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.ReferenceQueue; +import java.lang.ref.SoftReference; + +/** + * Implementation as per Ref.SOFT_IDENTITY + * + * @see Ref#SOFT_IDENTITY + * @author Peter Firmstone. + */ +class SoftIdentityReferenceKey extends SoftReference { + private final int hash; + + SoftIdentityReferenceKey(T k) { + super(k); + int hash = 7; + hash = 29 * hash + System.identityHashCode(k); + hash = 29 * hash + k.getClass().hashCode(); + this.hash = hash; + } + + SoftIdentityReferenceKey(T k, ReferenceQueue q) { + super(k,q); + int hash = 7; + hash = 29 * hash + System.identityHashCode(k); + hash = 29 * hash + k.getClass().hashCode(); + this.hash = hash; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (!(o instanceof SoftIdentityReferenceKey)) { + return false; + } + Object k1 = get(); + Object k2 = ((SoftIdentityReferenceKey) o).get(); + if ( k1 != null && k1 == k2 ) return true; + return ( k1 == null && k2 == null && hash == o.hashCode()); + } + + @Override + public int hashCode() { + return hash; + } + + @Override + public String toString(){ + Object s = get(); + if (s != null) return s.toString(); + return super.toString(); + } + +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/SoftIdentityReferenceKey.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/SoftReferenceKey.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/SoftReferenceKey.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/SoftReferenceKey.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/SoftReferenceKey.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.ReferenceQueue; +import java.lang.ref.SoftReference; + +/** + * Implemented as per Ref.SOFT + * + * @see Ref#SOFT + * @author Peter Firmstone. + */ +class SoftReferenceKey extends SoftReference implements Comparable>{ + private final int hash; // Once the object is garbage collected, use it's identity. + + SoftReferenceKey(T k) { + super(k); + int hash = 3; + hash = 67 * hash + System.identityHashCode(k); + hash = 67 * hash + k.getClass().hashCode(); + this.hash = hash; + } + + SoftReferenceKey(T k, ReferenceQueue q) { + super(k,q); + int hash = 3; + hash = 67 * hash + System.identityHashCode(k); + hash = 67 * hash + k.getClass().hashCode(); + this.hash = hash; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; // Same reference. + if (!(o instanceof SoftReferenceKey)) return false; + Object k1 = get(); + Object k2 = ((SoftReferenceKey) o).get(); + if ( k1 != null && k1.equals(k2)) return true; + return ( k1 == null && k2 == null && hash == o.hashCode()); + } + + @Override + public int hashCode() { + T referent = get(); + int hash = this.hash; + if (referent != null) hash = referent.hashCode(); + return hash; + } + + @Override + public String toString(){ + Object s = get(); + if (s != null) return s.toString(); + return super.toString(); + } + + @SuppressWarnings("unchecked") + public int compareTo(SoftReferenceKey o) { + T t = get(); + T r = o.get(); + if ( t != null && r != null) { + if ( t instanceof Comparable){ + return ((Comparable) t).compareTo(r); + } + } + if ( hashCode() < o.hashCode()) return -1; + if ( hashCode() == o.hashCode()) return 0; + return 1; + } + +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/SoftReferenceKey.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/StrongReferenceKey.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/StrongReferenceKey.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/StrongReferenceKey.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/StrongReferenceKey.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,110 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; + +/** + * Implemented as per Ref.STRONG + * + * @see Ref#STRONG + * @author Peter Firmstone + */ +class StrongReferenceKey extends WeakReference implements Comparable>{ + private T referent; + + /** + * Creates a new strong reference that refers to the given object. The new + * reference is not registered with any queue. + * + * @param referent object the new weak reference will refer to + */ + StrongReferenceKey(T referent){ + super(null); + this.referent = referent ; + } + + /** + * Creates a new strong reference that refers to the given object. The + * reference queue is silently ignored. + * + * @param referent object the new weak reference will refer to + * @param q queue is never used. + */ + StrongReferenceKey(T referent, ReferenceQueue q) { + this(referent); + } + + @Override + public int hashCode() { + int hash = 7; + hash = 59 * hash + (this.referent != null ? this.referent.hashCode() : 0); + return hash; + } + + public boolean equals(Object o){ + if ( !(o instanceof StrongReferenceKey)) return false; + StrongReferenceKey that = (StrongReferenceKey) o; + if (this.referent != that.referent) return false; + if (this.referent != null && this.referent.equals(that.referent)) return true; + return false; + } + + @Override + public String toString(){ + Object s = get(); + if (s != null) return s.toString(); + return super.toString(); + } + + @SuppressWarnings("unchecked") + public int compareTo(StrongReferenceKey o) { + T t = get(); + T r = o.get(); + if ( t != null && r != null) { + if ( t instanceof Comparable){ + return ((Comparable) t).compareTo(r); + } + } + if ( hashCode() < o.hashCode()) return -1; + if ( hashCode() == o.hashCode()) return 0; + return 1; + } + + @Override + public T get() { + return this.referent; + } + + @Override + public void clear() { + this.referent = null; + } + + @Override + public boolean isEnqueued() { + return false; + } + + @Override + public boolean enqueue() { + return false; + } +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/StrongReferenceKey.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/WeakIdentityReferenceKey.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/WeakIdentityReferenceKey.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/WeakIdentityReferenceKey.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/WeakIdentityReferenceKey.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,74 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; + +/** + * Implemented as per Ref.WEAK_IDENTITY + * + * @see Ref#WEAK_IDENTITY + * @author Peter Firmstone. + */ +class WeakIdentityReferenceKey extends WeakReference { + private final int hash; + + WeakIdentityReferenceKey(T k) { + super(k); + int hash = 5; + hash = 13 * hash + System.identityHashCode(k); + hash = 13 * hash + k.getClass().hashCode(); + this.hash = hash; + } + + WeakIdentityReferenceKey(T k, ReferenceQueue q) { + super(k,q); + int hash = 5; + hash = 13 * hash + System.identityHashCode(k); + hash = 13 * hash + k.getClass().hashCode(); + this.hash = hash; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } else if (!(o instanceof WeakIdentityReferenceKey)) { + return false; + } + Object k1 = get(); + Object k2 = ((WeakIdentityReferenceKey) o).get(); + if ( k1 != null && k1 == k2 ) return true; + return ( k1 == null && k2 == null && hash == o.hashCode()); + } + + @Override + public int hashCode() { + return hash; + } + + @Override + public String toString(){ + Object s = get(); + if (s != null) return s.toString(); + return super.toString(); + } + +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/WeakIdentityReferenceKey.java ------------------------------------------------------------------------------ svn:eol-style = native Added: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/WeakReferenceKey.java URL: http://svn.apache.org/viewvc/river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/WeakReferenceKey.java?rev=1173698&view=auto ============================================================================== --- river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/WeakReferenceKey.java (added) +++ river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/WeakReferenceKey.java Wed Sep 21 15:02:02 2011 @@ -0,0 +1,92 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 + * 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. + */ + +package org.apache.river.impl.util; + +import java.lang.ref.ReferenceQueue; +import java.lang.ref.WeakReference; + +/** + * Implemented as per Ref.WEAK + * + * @see Ref#WEAK + * @author Peter Firmstone + */ +class WeakReferenceKey extends WeakReference implements Comparable> { + private final int hash; // Only used after referent has been garbage collected. + + WeakReferenceKey(T k) { + super(k); + int hash = 7; + hash = 43 * hash + System.identityHashCode(k); + hash = 43 * hash + k.getClass().hashCode(); + this.hash = hash; + } + + WeakReferenceKey(T k, ReferenceQueue q) { + super(k,q); + int hash = 7; + hash = 43 * hash + System.identityHashCode(k); + hash = 43 * hash + k.getClass().hashCode(); + this.hash = hash; + } + + /* ReferenceQueue is not compared, because a lookup key is used to locate + * an existing key and ReferenceQueue is null in lookup key's. + * + * ReferenceQueue is not part of hashCode or equals. + */ + @Override + public boolean equals(Object o) { + if (this == o) return true; // Same reference. + if (!(o instanceof WeakReferenceKey)) return false; + Object k1 = get(); + Object k2 = ((WeakReferenceKey) o).get(); + if ( k1 != null && k1.equals(k2)) return true; + return ( k1 == null && k2 == null && hash == o.hashCode()); // Both objects were collected. + } + + @Override + public int hashCode() { + Object referent = get(); + int hash = this.hash; + if (referent != null) hash = referent.hashCode(); + return hash; + } + + @Override + public String toString(){ + Object s = get(); + if (s != null) return s.toString(); + return super.toString(); + } + + public int compareTo(WeakReferenceKey o) { + T t = get(); + T r = o.get(); + if ( t != null && r != null) { + if ( t instanceof Comparable){ + return ((Comparable) t).compareTo(r); + } + } + if ( hashCode() < o.hashCode()) return -1; + if ( hashCode() == o.hashCode()) return 0; + return 1; + } + +} Propchange: river/jtsk/skunk/peterConcurrentPolicy/src/org/apache/river/impl/util/WeakReferenceKey.java ------------------------------------------------------------------------------ svn:eol-style = native