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<T> extends ReferenceCollection<T> implements List<T> {
+ private final List<Reference<T>> list;
+ ReferenceList(List<Reference<T>> list, Ref type){
+ super(list, type);
+ this.list = list;
+ }
+
+ private ReferenceList(List<Reference<T>> list, Ref type, ReferenceQueue<T>
queue){
+ super(list, type, queue);
+ this.list = list;
+ }
+
+ public boolean addAll(int index, Collection<? extends T> 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<T> listIterator() {
+ processQueue();
+ return new ListIteratorWrap<T>(list.listIterator(), this);
+ }
+
+ public ListIterator<T> listIterator(int index) {
+ processQueue();
+ return new ListIteratorWrap<T>(list.listIterator(index), this);
+ }
+
+ public List<T> subList(int fromIndex, int toIndex) {
+ processQueue();
+ List<T> sub = new ReferenceList<T>(list.subList(fromIndex, toIndex),
getType(), getQueue());
+ return sub;
+ }
+
+ private class ListIteratorWrap<T> implements ListIterator<T>{
+ ListIterator<Reference<T>> iterator;
+ ReferenceCollection<T> col;
+ private ListIteratorWrap(ListIterator<Reference<T>> iterator,
ReferenceCollection<T> 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 <K>
+ * @param <V>
+ * @author Peter Firmstone.
+ */
+class ReferenceMap<K, V> implements Map<K, V>, ReferenceQueueProcessor {
+
+ // ConcurrentHashMap must be protected from null values;
+ private final Ref keyRef;
+ private final Ref valRef;
+ private final Map<Reference<K>, Reference<V>> map;
+ private final ReferenceQueue<K> keyQueue;
+ private final ReferenceQueue<V> valQueue;
+
+ ReferenceMap(Map<Reference<K>,Reference<V>> map, Ref key, Ref val){
+ if (map == null || key == null || val == null ) throw new
IllegalArgumentException("Null not allowed");
+ this.map = map;
+ keyQueue = new ReferenceQueue<K>();
+ valQueue = new ReferenceQueue<V>();
+ keyRef = key;
+ valRef = val;
+ }
+ ReferenceMap(Map<Reference<K>,Reference<V>> map, Ref key, Ref val,
ReferenceQueue<K> keyQueue, ReferenceQueue<V> 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<V> getValQueue(){
+ return valQueue;
+ }
+
+ protected ReferenceQueue<K> 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<K> 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<Entry<K,V>> entrySet() {
+ return new EntrySetFacade<Entry<K,V>,
Entry<Reference<K>,Reference<V>>>(
+ map.entrySet(),
+ new EntryFacadeConverter<K,V>(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<V> 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<K> keySet() {
+ processQueue();
+ return new ReferenceSet<K>(map.keySet(), keyRef, keyQueue);
+ }
+
+ public void processQueue() {
+ Reference r = null;
+ while ((r = keyQueue.poll()) != null) {
+ map.remove(r);
+ }
+ Collection<Reference<V>> 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<V> val = map.put(wrapKey(key, true), wrapVal(value, true));
+ if (val != null) {
+ return val.get();
+ }
+ return null;
+ }
+
+ /**
+ *
+ * @param m
+ */
+ public void putAll(Map<? extends K, ? extends V> m) {
+ if (m.isEmpty()) return;
+ Map<Reference<K>, Reference<V>> refMap
+ = new HashMap<Reference<K>, Reference<V>>(m.size());
+ Iterator<? extends Map.Entry<? extends K, ? extends V>> i =
m.entrySet().iterator();
+ while (i.hasNext()){
+ Map.Entry<? extends K, ? extends V> 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<V> 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<V> values() {
+ processQueue();
+ return new ReferenceCollection<V>(map.values(), valRef, valQueue);
+ }
+
+ Reference<V> wrapVal(V val, boolean enque) {
+ return ReferenceFactory.create(val, enque == true ? valQueue : null,
valRef);
+ }
+
+ Reference<K> 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<K,V> extends ReferenceSortedMap<K,V> implements
NavigableMap<K,V> {
+ private final NavigableMap<Reference<K>, Reference<V>> map;
+ private final Ref keyRef;
+ private final Ref valRef;
+
+ ReferenceNavigableMap(NavigableMap<Reference<K>, Reference<V>> map, Ref
keyRef, Ref valRef){
+ super(map, keyRef, valRef);
+ this.map = map;
+ this.keyRef = keyRef;
+ this.valRef = valRef;
+ }
+
+ ReferenceNavigableMap( NavigableMap<Reference<K>, Reference<V>> map,
+ Ref keyRef, Ref valRef, ReferenceQueue<K> keyQueue,
ReferenceQueue<V> valQueue){
+ super(map, keyRef, valRef, keyQueue, valQueue);
+ this.map = map;
+ this.keyRef = keyRef;
+ this.valRef = valRef;
+ }
+
+ public Entry<K, V> lowerEntry(K key) {
+ processQueue();
+ return new ReferenceEntryFacade<K,V>(
+ map.lowerEntry(wrapKey(key, false)),
+ valRef,
+ getValQueue()
+ );
+ }
+
+ public K lowerKey(K key) {
+ processQueue();
+ return map.lowerKey(wrapKey(key, false)).get();
+ }
+
+ public Entry<K, V> floorEntry(K key) {
+ processQueue();
+ return new ReferenceEntryFacade<K,V>(
+ map.floorEntry(wrapKey(key, false)),
+ valRef,
+ getValQueue()
+ );
+ }
+
+ public K floorKey(K key) {
+ processQueue();
+ return map.floorKey(wrapKey(key, false)).get();
+ }
+
+ public Entry<K, V> ceilingEntry(K key) {
+ processQueue();
+ return new ReferenceEntryFacade<K,V>(
+ map.ceilingEntry(wrapKey(key, false)),
+ valRef,
+ getValQueue()
+ );
+ }
+
+ public K ceilingKey(K key) {
+ processQueue();
+ return map.ceilingKey(wrapKey(key, false)).get();
+ }
+
+ public Entry<K, V> higherEntry(K key) {
+ processQueue();
+ return new ReferenceEntryFacade<K,V>(
+ map.higherEntry(wrapKey(key, false)),
+ valRef,
+ getValQueue()
+ );
+ }
+
+ public K higherKey(K key) {
+ processQueue();
+ return map.higherKey(wrapKey(key, false)).get();
+ }
+
+ public Entry<K, V> firstEntry() {
+ processQueue();
+ return new ReferenceEntryFacade<K,V>(
+ map.firstEntry(),
+ valRef,
+ getValQueue()
+ );
+ }
+
+ public Entry<K, V> lastEntry() {
+ processQueue();
+ return new ReferenceEntryFacade<K,V>(
+ map.lastEntry(),
+ valRef,
+ getValQueue()
+ );
+ }
+
+ public Entry<K, V> pollFirstEntry() {
+ processQueue();
+ return new ReferenceEntryFacade<K,V>(
+ map.pollFirstEntry(),
+ valRef,
+ getValQueue()
+ );
+ }
+
+ public Entry<K, V> pollLastEntry() {
+ processQueue();
+ return new ReferenceEntryFacade<K,V>(
+ map.pollLastEntry(),
+ valRef,
+ getValQueue()
+ );
+ }
+
+ public NavigableMap<K, V> descendingMap() {
+ processQueue();
+ return new ReferenceNavigableMap<K,V>(
+ map.descendingMap(),
+ keyRef,
+ valRef,
+ getKeyQueue(),
+ getValQueue()
+ );
+ }
+
+ public NavigableSet<K> navigableKeySet() {
+ processQueue();
+ return new ReferenceNavigableSet<K>(map.navigableKeySet(), keyRef,
getKeyQueue());
+ }
+
+ public NavigableSet<K> descendingKeySet() {
+ processQueue();
+ return new ReferenceNavigableSet<K>(map.descendingKeySet(), keyRef,
getKeyQueue());
+ }
+
+ public NavigableMap<K, V> subMap(K fromKey, boolean fromInclusive, K
toKey, boolean toInclusive) {
+ processQueue();
+ return new ReferenceNavigableMap<K,V>(
+ map.subMap(
+ wrapKey(fromKey, false),
+ fromInclusive,
+ wrapKey(toKey, false),
+ toInclusive
+ ),
+ keyRef,
+ valRef,
+ getKeyQueue(),
+ getValQueue()
+ );
+
+ }
+
+ public NavigableMap<K, V> headMap(K toKey, boolean inclusive) {
+ processQueue();
+ return new ReferenceNavigableMap<K,V>(
+ map.headMap(wrapKey(toKey, false),inclusive),
+ keyRef,
+ valRef,
+ getKeyQueue(),
+ getValQueue()
+ );
+ }
+
+ public NavigableMap<K, V> tailMap(K fromKey, boolean inclusive) {
+ processQueue();
+ return new ReferenceNavigableMap<K,V>(
+ 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 <T>
+ * @author Peter Firmstone.
+ */
+public class ReferenceNavigableSet<T>
+ extends ReferenceSortedSet<T> implements NavigableSet<T> {
+ private final NavigableSet<Reference<T>> set;
+ private final Ref type;
+
+ public ReferenceNavigableSet(NavigableSet<Reference<T>> set, Ref type){
+ super(set, type);
+ this.set = set;
+ this.type = type;
+ }
+
+ ReferenceNavigableSet(NavigableSet<Reference<T>> set, Ref type,
ReferenceQueue<T> 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<T> descendingSet() {
+ processQueue();
+ return new ReferenceNavigableSet<T>(set.descendingSet(), type,
getQueue());
+ }
+
+ public Iterator<T> descendingIterator() {
+ processQueue();
+ return new ReferenceIterator<T>(set.descendingIterator());
+ }
+
+ public NavigableSet<T> subSet(T fromElement, boolean fromInclusive, T
toElement, boolean toInclusive) {
+ processQueue();
+ return new ReferenceNavigableSet<T>(
+ set.subSet(
+ wrapObj(fromElement, false),
+ fromInclusive,
+ wrapObj(toElement, false),
+ toInclusive
+ ),
+ type,
+ getQueue()
+ );
+ }
+
+ public NavigableSet<T> headSet(T toElement, boolean inclusive) {
+ processQueue();
+ return new ReferenceNavigableSet<T>(
+ set.headSet(wrapObj(toElement, false), inclusive),
+ type, getQueue()
+ );
+ }
+
+ public NavigableSet<T> tailSet(T fromElement, boolean inclusive) {
+ processQueue();
+ return new ReferenceNavigableSet<T>(
+ 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<T> extends ReferenceCollection<T> implements Set<T>{
+
+ ReferenceSet(Set<Reference<T>> col, Ref type){
+ super(col, type);
+ }
+
+ ReferenceSet(Set<Reference<T>> col, Ref type, ReferenceQueue<T> 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 <K>
+ * @param <V>
+ * @author peter
+ */
+class ReferenceSortedMap<K,V> extends ReferenceMap<K,V> implements
SortedMap<K,V>{
+ private SortedMap<Reference<K>, Reference<V>> map;
+ private Ref keyRef;
+ private Ref valRef;
+
+ ReferenceSortedMap(SortedMap<Reference<K>, Reference<V>> map, Ref keyRef,
Ref valRef){
+ super(map, keyRef, valRef);
+ this.map = map;
+ this.keyRef = keyRef;
+ this.valRef = valRef;
+ }
+
+ ReferenceSortedMap( SortedMap<Reference<K>, Reference<V>> map,
+ Ref keyRef, Ref valRef, ReferenceQueue<K> keyQueue,
ReferenceQueue<V> valQueue){
+ super(map, keyRef, valRef, keyQueue, valQueue);
+ this.map = map;
+ this.keyRef = keyRef;
+ this.valRef = valRef;
+ }
+
+ @SuppressWarnings("unchecked")
+ public Comparator<? super K> comparator() {
+ processQueue();
+ Comparator<? super Reference<K>> c = map.comparator();
+ if ( c instanceof ReferenceComparator){
+ return ((ReferenceComparator) c).get();
+ }
+ return null;
+ }
+
+ public SortedMap<K, V> subMap(K fromKey, K toKey) {
+ processQueue();
+ return new ReferenceSortedMap<K,V>(
+ map.subMap(wrapKey(fromKey, false), wrapKey(fromKey, false)),
+ keyRef,
+ valRef,
+ getKeyQueue(),
+ getValQueue()
+ );
+ }
+
+
+ public SortedMap<K, V> headMap(K toKey) {
+ processQueue();
+ return new ReferenceSortedMap<K,V>(
+ map.headMap(wrapKey(toKey, false)),
+ keyRef,
+ valRef,
+ getKeyQueue(),
+ getValQueue()
+ );
+ }
+
+
+ public SortedMap<K, V> tailMap(K fromKey) {
+ processQueue();
+ return new ReferenceSortedMap<K,V>(
+ 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 <T>
+ * @see Comparable
+ * @see Comparator
+ * @see ReferenceComparator
+ * @author Peter Firmstone.
+ */
+class ReferenceSortedSet<T> extends ReferenceSet<T> implements SortedSet<T> {
+ private final SortedSet<Reference<T>> set;
+ private final Ref type;
+ ReferenceSortedSet( SortedSet<Reference<T>> set, Ref type){
+ super(set, type);
+ this.set = set;
+ this.type = type;
+ }
+
+ ReferenceSortedSet(SortedSet<Reference<T>> set, Ref type,
ReferenceQueue<T> queue){
+ super(set, type, queue);
+ this.set = set;
+ this.type = type;
+ }
+
+ @SuppressWarnings("unchecked")
+ public Comparator<? super T> comparator() {
+ processQueue();
+ Comparator<? super Reference<T>> c = set.comparator();
+ if ( c instanceof ReferenceComparator){
+ return ((ReferenceComparator) c).get();
+ }
+ return null;
+ }
+
+ public SortedSet<T> subSet(T fromElement, T toElement) {
+ processQueue();
+ Reference<T> from = wrapObj(fromElement, false);
+ Reference<T> to = wrapObj(toElement, false);
+ return new ReferenceSortedSet<T>( set.subSet(from, to), type,
getQueue());
+ }
+
+ public SortedSet<T> headSet(T toElement) {
+ processQueue();
+ Reference<T> to = wrapObj(toElement, false);
+ return new ReferenceSortedSet<T>(set.headSet(to), type, getQueue());
+ }
+
+ public SortedSet<T> tailSet(T fromElement) {
+ processQueue();
+ Reference<T> from = wrapObj(fromElement, false);
+ return new ReferenceSortedSet<T>(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 <T>
+ * @author Peter Firmstone.
+ */
+public class ReferencedQueue<T> extends ReferenceCollection<T> implements
Queue<T> {
+ private final Queue<Reference<T>> queue;
+ private final Ref type;
+
+ public ReferencedQueue( Queue<Reference<T>> queue, Ref type){
+ super(queue, type);
+ this.queue = queue;
+ this.type = type;
+ }
+ public boolean offer(T e) {
+ processQueue();
+ Reference<T> 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<T> extends SoftReference<T> {
+ 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<? super T> 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<T> extends SoftReference<T> implements
Comparable<SoftReferenceKey<T>>{
+ 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<? super T> 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<T> 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<T> extends WeakReference<T> implements
Comparable<StrongReferenceKey<T>>{
+ 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<? super T> 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<T> 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<T> extends WeakReference<T> {
+ 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<? super T> 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<T> extends WeakReference<T> implements
Comparable<WeakReferenceKey<T>> {
+ 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<? super T> 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<T> 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