[Arakhnę-Dev] [371] * Add classes SoftTreeSet, SoftHashSet, WeakHashSet, WeakTreeSet. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 371
Author: galland
Date: 2012-08-15 22:09:50 +0200 (Wed, 15 Aug 2012)
Log Message:
-----------
* Add classes SoftTreeSet, SoftHashSet, WeakHashSet, WeakTreeSet.
* Add unit tests for ReflectionUtil.
Modified Paths:
--------------
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractPhantomValueMap.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractReferencedValueMap.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractSoftValueMap.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractWeakValueMap.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/PhantomValueHashMap.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/PhantomValueTreeMap.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueHashMap.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueMap.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueTreeMap.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueHashMap.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueMap.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueTreeMap.java
trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/AbstractCollectionTestCase.java
Added Paths:
-----------
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractReferencedSet.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftHashSet.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftTreeSet.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakHashSet.java
trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakTreeSet.java
trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/SoftHashSetTest.java
trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/SoftTreeSetTest.java
trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/WeakHashSetTest.java
trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/WeakTreeSetTest.java
trunk/arakhneVmutils/arakhneVmutils-java/src/test/java/org/arakhne/vmutil/ReflectionUtilTest.java
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractPhantomValueMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractPhantomValueMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractPhantomValueMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -22,11 +22,12 @@
package org.arakhne.util.ref;
+import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;
import java.util.Map;
/**
- * A <tt>Map</tt> implementation with <em>phantom values</em>. An entry in a
+ * A <tt>Map</tt> implementation with {@link PhantomReference phantom values}. An entry in a
* <tt>AbstractPhantomValueMap</tt> will automatically be removed when its value is no
* longer in ordinary use or <code>null</code>.
* <p>
Added: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractReferencedSet.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractReferencedSet.java (rev 0)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractReferencedSet.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -0,0 +1,284 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2005-2009 Stephane GALLAND.
+ * Copyright (C) 2011-12 Stephane GALLAND.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+
+package org.arakhne.util.ref;
+
+import java.lang.ref.Reference;
+import java.lang.ref.ReferenceQueue;
+import java.lang.ref.SoftReference;
+import java.lang.ref.WeakReference;
+import java.util.AbstractSet;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.Set;
+
+/**
+ * A <tt>Set</tt> implementation with {@link SoftReference soft values}
+ * or {@link WeakReference weak values}. An entry in a
+ * <tt>AbstractReferencedSet</tt> will automatically be removed when its value is no
+ * longer in ordinary use or <code>null</code>.
+ * <p>
+ * This abstract implementation does not decide if the map is based on a tree or
+ * on a hashtable.
+ *
+ * @param <E> is the type of the values.
+ * @param <R> is the type of the references.
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ * @since 5.9
+ */
+public abstract class AbstractReferencedSet<E,R extends Reference<E>> extends AbstractSet<E> {
+
+ private boolean autoExpurge = false;
+ private final ReferenceQueue<E> queue = new ReferenceQueue<E>();
+
+ private final Class<? super R> referenceType;
+ private final Set<R> theSet;
+
+ /**
+ * @param theSet is the internal data structure to use.
+ * @param referenceType is the type of the references.
+ */
+ public AbstractReferencedSet(Set<R> theSet, Class<? super R> referenceType) {
+ assert(theSet!=null);
+ assert(referenceType!=null);
+ this.theSet = theSet;
+ this.referenceType = referenceType;
+ }
+
+ /** Create a reference on the given object.
+ *
+ * @param element is the element to wrap into a reference
+ * @return the reference of the given element.
+ */
+ protected abstract R createReference(E element);
+
+ /** Clean the references that was marked as released inside
+ * the queue.
+ */
+ protected final void expurgeNow() {
+ if (this.autoExpurge)
+ expurge();
+ else
+ expurgeQueuedReferences();
+ }
+
+ /** Replies if this map expurge all the released references
+ * even if they are not enqueued by the virtual machine
+ *
+ * @return <code>true</code> is the values are deeply expurged when they
+ * are released from the moemory, otherwise <code>false</code>
+ */
+ public final boolean isDeeplyExpurge() {
+ return this.autoExpurge;
+ }
+
+ /** Set if this map expurge all the released references
+ * even if they are not enqueued by the virtual machine
+ *
+ * @param deeplyExpurge must be <code>true</code> to
+ * expurge all the released values, otherwise <code>false</code>
+ * to expurge only the enqueued values.
+ * @return the old value of this flag
+ */
+ public final boolean setDeeplyExpurge(boolean deeplyExpurge) {
+ boolean old = this.autoExpurge;
+ this.autoExpurge = deeplyExpurge;
+ return old;
+ }
+
+ /** Clean the references that was marked as released inside
+ * the queue.
+ */
+ public final void expurgeQueuedReferences() {
+ Reference<? extends E> o;
+ while((o = this.queue.poll()) != null) {
+ o.clear();
+ if (this.referenceType.isInstance(o)) {
+ this.theSet.remove(this.referenceType.cast(o));
+ }
+ }
+ }
+
+ /** Clean the references that was released.
+ */
+ public final void expurge() {
+ Reference<? extends E> o;
+
+ Iterator<R> iter = this.theSet.iterator();
+ R reference;
+ while (iter.hasNext()) {
+ reference = iter.next();
+ if (reference!=null &&
+ ((reference.isEnqueued())||(reference.get()==null))) {
+ reference.enqueue();
+ reference.clear();
+ }
+ }
+ reference = null;
+
+ while((o = this.queue.poll()) != null) {
+ o.clear();
+ if (this.referenceType.isInstance(o)) {
+ this.theSet.remove(this.referenceType.cast(o));
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public final boolean equals(Object o) {
+ expurgeNow();
+ return super.equals(o);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public final int hashCode() {
+ expurgeNow();
+ return super.hashCode();
+ }
+
+ @Override
+ public Iterator<E> iterator() {
+ expurgeNow();
+ return new InnerIterator();
+ }
+
+ @Override
+ public int size() {
+ expurgeNow();
+ return this.theSet.size();
+ }
+
+ @Override
+ public boolean add(E e) {
+ return this.theSet.add(createReference(e));
+ }
+
+ /**
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ * @since 5.9
+ */
+ private class InnerIterator implements Iterator<E> {
+
+ private final Iterator<R> originalIterator;
+ private E next = null;
+ private boolean nextSearchProceeded = false;
+ private boolean enableRemove = false;
+
+ @SuppressWarnings("synthetic-access")
+ public InnerIterator() {
+ this.originalIterator = AbstractReferencedSet.this.theSet.iterator();
+ }
+
+ private void searchNext() {
+ if (!this.nextSearchProceeded) {
+ this.nextSearchProceeded = true;
+ this.next = null;
+ R originalNext;
+ E wValue;
+ while (this.next==null && this.originalIterator.hasNext()) {
+ originalNext = this.originalIterator.next();
+ if (originalNext!=null) {
+ wValue = originalNext.get();
+ if (wValue!=null) {
+ this.next = wValue;
+ return;
+ }
+ }
+ // Remove the original entry because the pointer was lost.
+ this.originalIterator.remove();
+ }
+ }
+ }
+
+ @Override
+ public boolean hasNext() {
+ searchNext();
+ assert(this.nextSearchProceeded);
+ this.enableRemove = false;
+ return this.next!=null;
+ }
+
+ @Override
+ public E next() {
+ searchNext();
+ assert(this.nextSearchProceeded);
+ E cnext = this.next;
+
+ // Reset the research flags
+ this.next = null;
+ this.nextSearchProceeded = false;
+ this.enableRemove = true;
+
+ return cnext;
+ }
+
+ @Override
+ public void remove() {
+ if (!this.enableRemove)
+ throw new IllegalStateException("you must not invoke the remove function between hasNext and next functions."); //$NON-NLS-1$
+ this.originalIterator.remove();
+ }
+
+ } // class InnerIterator
+
+ /** Comparator wrapper.
+ *
+ * @param <E> is the type of the values.
+ * @param <R> is the type of the references.
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ * @since 5.9
+ */
+ protected static class ReferenceComparator<E,R extends Reference<E>> implements Comparator<R> {
+
+ private final Comparator<? super E> comparator;
+
+ public ReferenceComparator(Comparator<? super E> comparator) {
+ assert(comparator!=null);
+ this.comparator = comparator;
+ }
+
+ @Override
+ public int compare(R o1, R o2) {
+ if (o1==o2) return 0;
+ if (o1==null) return Integer.MIN_VALUE;
+ if (o2==null) return Integer.MAX_VALUE;
+ return this.comparator.compare(o1.get(), o2.get());
+ }
+
+ }
+
+}
\ No newline at end of file
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractReferencedValueMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractReferencedValueMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractReferencedValueMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -507,9 +507,9 @@
originalNext);
return;
}
- // Remove the original entry because the pointer was lost.
- this.originalIterator.remove();
}
+ // Remove the original entry because the pointer was lost.
+ this.originalIterator.remove();
}
}
}
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractSoftValueMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractSoftValueMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractSoftValueMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -23,10 +23,11 @@
package org.arakhne.util.ref;
import java.lang.ref.ReferenceQueue;
+import java.lang.ref.SoftReference;
import java.util.Map;
/**
- * A <tt>Map</tt> implementation with <em>soft values</em>. An entry in a
+ * A <tt>Map</tt> implementation with {@link SoftReference soft values}. An entry in a
* <tt>AbstractSoftValueMap</tt> will automatically be removed when its value is no
* longer in ordinary use or <code>null</code>.
* <p>
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractWeakValueMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractWeakValueMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/AbstractWeakValueMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -23,10 +23,11 @@
package org.arakhne.util.ref;
import java.lang.ref.ReferenceQueue;
+import java.lang.ref.WeakReference;
import java.util.Map;
/**
- * A <tt>Map</tt> implementation with <em>weak values</em>. An entry in a
+ * A <tt>Map</tt> implementation with {@link WeakReference weak values}. An entry in a
* <tt>AbstractWeakValueMap</tt> will automatically be removed when its value is no
* longer in ordinary use or <code>null</code>.
* <p>
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/PhantomValueHashMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/PhantomValueHashMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/PhantomValueHashMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -22,12 +22,13 @@
package org.arakhne.util.ref;
+import java.lang.ref.PhantomReference;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
/**
- * A <tt>Map</tt> implementation with <em>phantom values</em>. An entry in a
+ * A <tt>Map</tt> implementation with {@link PhantomReference phantom values}. An entry in a
* <tt>PhantomValueHashMap</tt> will automatically be removed when its value is no
* longer in ordinary use or null.
* <p>
@@ -64,7 +65,7 @@
* @mavenartifactid $ArtifactId$
* @since 5.8
*/
-public class PhantomValueHashMap<K,V> extends AbstractSoftValueMap<K,V> {
+public class PhantomValueHashMap<K,V> extends AbstractPhantomValueMap<K,V> {
/**
* Constructs an empty <tt>HashMap</tt> with the specified initial
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/PhantomValueTreeMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/PhantomValueTreeMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/PhantomValueTreeMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -22,6 +22,7 @@
package org.arakhne.util.ref;
+import java.lang.ref.PhantomReference;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;
@@ -29,7 +30,7 @@
import java.util.WeakHashMap;
/**
- * A <tt>Map</tt> implementation with <em>phantom values</em>. An entry in a
+ * A <tt>Map</tt> implementation with {@link PhantomReference phantom values}. An entry in a
* <tt>PhantomValueTreeMap</tt> will automatically be removed when its value is no
* longer in ordinary use or null.
* <p>
@@ -66,7 +67,7 @@
* @mavenartifactid $ArtifactId$
* @since 5.8
*/
-public class PhantomValueTreeMap<K,V> extends AbstractSoftValueMap<K,V> {
+public class PhantomValueTreeMap<K,V> extends AbstractPhantomValueMap<K,V> {
/**
* Constructs an empty <tt>TreeMap</tt> with the specified comparator.
Added: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftHashSet.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftHashSet.java (rev 0)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftHashSet.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -0,0 +1,120 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2005-2007 Stephane GALLAND.
+ * Copyright (C) 2011 Stephane GALLAND.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+
+package org.arakhne.util.ref;
+
+import java.lang.ref.SoftReference;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.WeakHashMap;
+
+/**
+ * A <tt>Set</tt> implementation with {@link SoftReference soft values}. An entry in a
+ * <tt>SoftHashSet</tt> will automatically be removed when its value is no
+ * longer in ordinary use or null.
+ * <p>
+ * This class was inspirated from {@link WeakHashMap} and uses a {@link HashSet}
+ * as its internal data structure.
+ * <p>
+ * This class has a special flag which permits to control the
+ * way how the released references are expurged: {@link #isDeeplyExpurge()},
+ * {@link #setDeeplyExpurge(boolean)}. If this flag is <code>true</code>,
+ * all the released references will be immediately removed from the map even
+ * if they are not enqueued by the virtual machine (see {@link #expurge()}.
+ * If this flag is <code>false</code>,
+ * only the enqueued references will be removed from the map
+ * (see {@link #expurgeQueuedReferences()}.
+ * <p>
+ * If this map does not use a "deep expurge" of the released references,
+ * it could contains <code>null</code> values that corresponds to
+ * values that are released by the garbage collector. If a "deep expurge"
+ * is used, all the values released by the garbage collector will be
+ * removed from the map.
+ * <p>
+ * "Deep expurge" consumes much more time that "No deep expurge". This is the
+ * reason why this feature is not activated by default.
+ * <p>
+ * The "deep expurge" feature was added to fix the uncoherent behavior
+ * of the garbage collector which seems to not always enqueued the
+ * released values (sometimes the queue is empty even if a value was released).
+ *
+ * @param <E> is the type of the values.
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ * @since 5.9
+ */
+public class SoftHashSet<E> extends AbstractReferencedSet<E,SoftReference<E>> {
+
+ /**
+ * Constructs an empty <tt>HashSet</tt>.
+ */
+ public SoftHashSet() {
+ super(new HashSet<SoftReference<E>>(), SoftReference.class);
+ }
+
+ /**
+ * Constructs a new, empty set; the backing <tt>HashSet</tt> instance has
+ * the specified initial capacity and default load factor (0.75).
+ *
+ * @param initialCapacity the initial capacity of the hash table
+ * @throws IllegalArgumentException if the initial capacity is less
+ * than zero
+ */
+ public SoftHashSet(int initialCapacity) {
+ super(new HashSet<SoftReference<E>>(initialCapacity), SoftReference.class);
+ }
+
+ /**
+ * Constructs a new, empty set; the backing <tt>HashSet</tt> instance has
+ * the specified initial capacity and the specified load factor.
+ *
+ * @param initialCapacity the initial capacity of the hash map
+ * @param loadFactor the load factor of the hash map
+ * @throws IllegalArgumentException if the initial capacity is less
+ * than zero, or if the load factor is nonpositive
+ */
+ public SoftHashSet(int initialCapacity, float loadFactor) {
+ super(new HashSet<SoftReference<E>>(initialCapacity, loadFactor), SoftReference.class);
+ }
+
+ /**
+ * Constructs a new set containing the elements in the specified
+ * collection. The <tt>HashSet</tt> is created with default load factor
+ * (0.75) and an initial capacity sufficient to contain the elements in
+ * the specified collection.
+ *
+ * @param c the collection whose elements are to be placed into this set
+ * @throws NullPointerException if the specified collection is null
+ */
+ public SoftHashSet(Collection<? extends E> c) {
+ super(new HashSet<SoftReference<E>>(), SoftReference.class);
+ addAll(c);
+ }
+
+ @Override
+ protected final SoftReference<E> createReference(E element) {
+ return new SoftReference<E>(element);
+ }
+
+}
Added: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftTreeSet.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftTreeSet.java (rev 0)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftTreeSet.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -0,0 +1,134 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2005-2007 Stephane GALLAND.
+ * Copyright (C) 2011 Stephane GALLAND.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+
+package org.arakhne.util.ref;
+
+import java.lang.ref.SoftReference;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import java.util.WeakHashMap;
+
+/**
+ * A <tt>Set</tt> implementation with {@link SoftReference soft values}. An entry in a
+ * <tt>SoftTreeSet</tt> will automatically be removed when its value is no
+ * longer in ordinary use or null.
+ * <p>
+ * This class was inspirated from {@link WeakHashMap} and uses a {@link TreeSet}
+ * as its internal data structure.
+ * <p>
+ * This class has a special flag which permits to control the
+ * way how the released references are expurged: {@link #isDeeplyExpurge()},
+ * {@link #setDeeplyExpurge(boolean)}. If this flag is <code>true</code>,
+ * all the released references will be immediately removed from the map even
+ * if they are not enqueued by the virtual machine (see {@link #expurge()}.
+ * If this flag is <code>false</code>,
+ * only the enqueued references will be removed from the map
+ * (see {@link #expurgeQueuedReferences()}.
+ * <p>
+ * If this map does not use a "deep expurge" of the released references,
+ * it could contains <code>null</code> values that corresponds to
+ * values that are released by the garbage collector. If a "deep expurge"
+ * is used, all the values released by the garbage collector will be
+ * removed from the map.
+ * <p>
+ * "Deep expurge" consumes much more time that "No deep expurge". This is the
+ * reason why this feature is not activated by default.
+ * <p>
+ * The "deep expurge" feature was added to fix the uncoherent behavior
+ * of the garbage collector which seems to not always enqueued the
+ * released values (sometimes the queue is empty even if a value was released).
+ *
+ * @param <E> is the type of the values.
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ * @since 5.9
+ */
+public class SoftTreeSet<E> extends AbstractReferencedSet<E,ComparableSoftReference<E>> {
+
+ /**
+ * Constructs an empty <tt>TreeSet</tt>.
+ */
+ public SoftTreeSet() {
+ super(new TreeSet<ComparableSoftReference<E>>(), ComparableSoftReference.class);
+ }
+
+ /**
+ * Constructs a new tree set containing the elements in the specified
+ * collection, sorted according to the <i>natural ordering</i> of its
+ * elements. All elements inserted into the set must implement the
+ * {@link Comparable} interface. Furthermore, all such elements must be
+ * <i>mutually comparable</i>: {@code e1.compareTo(e2)} must not throw a
+ * {@code ClassCastException} for any elements {@code e1} and
+ * {@code e2} in the set.
+ *
+ * @param c collection whose elements will comprise the new set
+ * @throws ClassCastException if the elements in {@code c} are
+ * not {@link Comparable}, or are not mutually comparable
+ * @throws NullPointerException if the specified collection is null
+ */
+ public SoftTreeSet(Collection<? extends E> c) {
+ super(new TreeSet<ComparableSoftReference<E>>(), ComparableSoftReference.class);
+ addAll(c);
+ }
+
+ /**
+ * Constructs a new tree set containing the same elements and
+ * using the same ordering as the specified sorted set.
+ *
+ * @param s sorted set whose elements will comprise the new set
+ * @throws NullPointerException if the specified sorted set is null
+ */
+ @SuppressWarnings("unchecked")
+ public SoftTreeSet(SortedSet<? extends E> s) {
+ this((Comparator<? super E>)s.comparator());
+ addAll(s);
+ }
+
+ /**
+ * Constructs a new, empty tree set, sorted according to the specified
+ * comparator. All elements inserted into the set must be <i>mutually
+ * comparable</i> by the specified comparator: {@code comparator.compare(e1,
+ * e2)} must not throw a {@code ClassCastException} for any elements
+ * {@code e1} and {@code e2} in the set. If the user attempts to add
+ * an element to the set that violates this constraint, the
+ * {@code add} call will throw a {@code ClassCastException}.
+ *
+ * @param comparator the comparator that will be used to order this set.
+ * If {@code null}, the {@linkplain Comparable natural
+ * ordering} of the elements will be used.
+ */
+ public SoftTreeSet(Comparator<? super E> c) {
+ super(new TreeSet<ComparableSoftReference<E>>(
+ new ReferenceComparator<E,ComparableSoftReference<E>>(c)),
+ ComparableSoftReference.class);
+ }
+
+ @Override
+ protected final ComparableSoftReference<E> createReference(E element) {
+ return new ComparableSoftReference<E>(element);
+ }
+
+}
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueHashMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueHashMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueHashMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -22,12 +22,13 @@
package org.arakhne.util.ref;
+import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
/**
- * A <tt>Map</tt> implementation with <em>soft values</em>. An entry in a
+ * A <tt>Map</tt> implementation with {@link SoftReference soft values}. An entry in a
* <tt>SoftValueHashMap</tt> will automatically be removed when its value is no
* longer in ordinary use or null.
* <p>
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -22,11 +22,12 @@
package org.arakhne.util.ref;
+import java.lang.ref.SoftReference;
import java.util.Map;
import java.util.WeakHashMap;
/**
- * A <tt>Map</tt> implementation with <em>soft values</em>. An entry in a
+ * A <tt>Map</tt> implementation with {@link SoftReference soft values}. An entry in a
* <tt>SoftValueMap</tt> will automatically be removed when its value is no
* longer in ordinary use or null.
* <p>
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueTreeMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueTreeMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/SoftValueTreeMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -22,6 +22,7 @@
package org.arakhne.util.ref;
+import java.lang.ref.SoftReference;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;
@@ -29,7 +30,7 @@
import java.util.WeakHashMap;
/**
- * A <tt>Map</tt> implementation with <em>soft values</em>. An entry in a
+ * A <tt>Map</tt> implementation with {@link SoftReference soft values}. An entry in a
* <tt>SoftValueMap</tt> will automatically be removed when its value is no
* longer in ordinary use or null.
* <p>
Added: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakHashSet.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakHashSet.java (rev 0)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakHashSet.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -0,0 +1,120 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2005-2007 Stephane GALLAND.
+ * Copyright (C) 2011 Stephane GALLAND.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+
+package org.arakhne.util.ref;
+
+import java.lang.ref.WeakReference;
+import java.util.Collection;
+import java.util.HashSet;
+import java.util.WeakHashMap;
+
+/**
+ * A <tt>Set</tt> implementation with {@link WeakReference weak values}. An entry in a
+ * <tt>WeakHashSet</tt> will automatically be removed when its value is no
+ * longer in ordinary use or null.
+ * <p>
+ * This class was inspirated from {@link WeakHashMap} and uses a {@link HashSet}
+ * as its internal data structure.
+ * <p>
+ * This class has a special flag which permits to control the
+ * way how the released references are expurged: {@link #isDeeplyExpurge()},
+ * {@link #setDeeplyExpurge(boolean)}. If this flag is <code>true</code>,
+ * all the released references will be immediately removed from the map even
+ * if they are not enqueued by the virtual machine (see {@link #expurge()}.
+ * If this flag is <code>false</code>,
+ * only the enqueued references will be removed from the map
+ * (see {@link #expurgeQueuedReferences()}.
+ * <p>
+ * If this map does not use a "deep expurge" of the released references,
+ * it could contains <code>null</code> values that corresponds to
+ * values that are released by the garbage collector. If a "deep expurge"
+ * is used, all the values released by the garbage collector will be
+ * removed from the map.
+ * <p>
+ * "Deep expurge" consumes much more time that "No deep expurge". This is the
+ * reason why this feature is not activated by default.
+ * <p>
+ * The "deep expurge" feature was added to fix the uncoherent behavior
+ * of the garbage collector which seems to not always enqueued the
+ * released values (sometimes the queue is empty even if a value was released).
+ *
+ * @param <E> is the type of the values.
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ * @since 5.9
+ */
+public class WeakHashSet<E> extends AbstractReferencedSet<E,WeakReference<E>> {
+
+ /**
+ * Constructs an empty <tt>HashSet</tt>.
+ */
+ public WeakHashSet() {
+ super(new HashSet<WeakReference<E>>(), WeakReference.class);
+ }
+
+ /**
+ * Constructs a new, empty set; the backing <tt>HashSet</tt> instance has
+ * the specified initial capacity and default load factor (0.75).
+ *
+ * @param initialCapacity the initial capacity of the hash table
+ * @throws IllegalArgumentException if the initial capacity is less
+ * than zero
+ */
+ public WeakHashSet(int initialCapacity) {
+ super(new HashSet<WeakReference<E>>(initialCapacity), WeakReference.class);
+ }
+
+ /**
+ * Constructs a new, empty set; the backing <tt>HashSet</tt> instance has
+ * the specified initial capacity and the specified load factor.
+ *
+ * @param initialCapacity the initial capacity of the hash map
+ * @param loadFactor the load factor of the hash map
+ * @throws IllegalArgumentException if the initial capacity is less
+ * than zero, or if the load factor is nonpositive
+ */
+ public WeakHashSet(int initialCapacity, float loadFactor) {
+ super(new HashSet<WeakReference<E>>(initialCapacity, loadFactor), WeakReference.class);
+ }
+
+ /**
+ * Constructs a new set containing the elements in the specified
+ * collection. The <tt>HashSet</tt> is created with default load factor
+ * (0.75) and an initial capacity sufficient to contain the elements in
+ * the specified collection.
+ *
+ * @param c the collection whose elements are to be placed into this set
+ * @throws NullPointerException if the specified collection is null
+ */
+ public WeakHashSet(Collection<? extends E> c) {
+ super(new HashSet<WeakReference<E>>(), WeakReference.class);
+ addAll(c);
+ }
+
+ @Override
+ protected final WeakReference<E> createReference(E element) {
+ return new WeakReference<E>(element);
+ }
+
+}
Added: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakTreeSet.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakTreeSet.java (rev 0)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakTreeSet.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -0,0 +1,134 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2005-2007 Stephane GALLAND.
+ * Copyright (C) 2011 Stephane GALLAND.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+
+package org.arakhne.util.ref;
+
+import java.lang.ref.WeakReference;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.SortedSet;
+import java.util.TreeSet;
+import java.util.WeakHashMap;
+
+/**
+ * A <tt>Set</tt> implementation with {@link WeakReference weak values}. An entry in a
+ * <tt>WeakTreeSet</tt> will automatically be removed when its value is no
+ * longer in ordinary use or null.
+ * <p>
+ * This class was inspirated from {@link WeakHashMap} and uses a {@link TreeSet}
+ * as its internal data structure.
+ * <p>
+ * This class has a special flag which permits to control the
+ * way how the released references are expurged: {@link #isDeeplyExpurge()},
+ * {@link #setDeeplyExpurge(boolean)}. If this flag is <code>true</code>,
+ * all the released references will be immediately removed from the map even
+ * if they are not enqueued by the virtual machine (see {@link #expurge()}.
+ * If this flag is <code>false</code>,
+ * only the enqueued references will be removed from the map
+ * (see {@link #expurgeQueuedReferences()}.
+ * <p>
+ * If this map does not use a "deep expurge" of the released references,
+ * it could contains <code>null</code> values that corresponds to
+ * values that are released by the garbage collector. If a "deep expurge"
+ * is used, all the values released by the garbage collector will be
+ * removed from the map.
+ * <p>
+ * "Deep expurge" consumes much more time that "No deep expurge". This is the
+ * reason why this feature is not activated by default.
+ * <p>
+ * The "deep expurge" feature was added to fix the uncoherent behavior
+ * of the garbage collector which seems to not always enqueued the
+ * released values (sometimes the queue is empty even if a value was released).
+ *
+ * @param <E> is the type of the values.
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ * @since 5.9
+ */
+public class WeakTreeSet<E> extends AbstractReferencedSet<E,ComparableWeakReference<E>> {
+
+ /**
+ * Constructs an empty <tt>TreeSet</tt>.
+ */
+ public WeakTreeSet() {
+ super(new TreeSet<ComparableWeakReference<E>>(), ComparableWeakReference.class);
+ }
+
+ /**
+ * Constructs a new tree set containing the elements in the specified
+ * collection, sorted according to the <i>natural ordering</i> of its
+ * elements. All elements inserted into the set must implement the
+ * {@link Comparable} interface. Furthermore, all such elements must be
+ * <i>mutually comparable</i>: {@code e1.compareTo(e2)} must not throw a
+ * {@code ClassCastException} for any elements {@code e1} and
+ * {@code e2} in the set.
+ *
+ * @param c collection whose elements will comprise the new set
+ * @throws ClassCastException if the elements in {@code c} are
+ * not {@link Comparable}, or are not mutually comparable
+ * @throws NullPointerException if the specified collection is null
+ */
+ public WeakTreeSet(Collection<? extends E> c) {
+ super(new TreeSet<ComparableWeakReference<E>>(), ComparableWeakReference.class);
+ addAll(c);
+ }
+
+ /**
+ * Constructs a new tree set containing the same elements and
+ * using the same ordering as the specified sorted set.
+ *
+ * @param s sorted set whose elements will comprise the new set
+ * @throws NullPointerException if the specified sorted set is null
+ */
+ @SuppressWarnings("unchecked")
+ public WeakTreeSet(SortedSet<? extends E> s) {
+ this((Comparator<? super E>)s.comparator());
+ addAll(s);
+ }
+
+ /**
+ * Constructs a new, empty tree set, sorted according to the specified
+ * comparator. All elements inserted into the set must be <i>mutually
+ * comparable</i> by the specified comparator: {@code comparator.compare(e1,
+ * e2)} must not throw a {@code ClassCastException} for any elements
+ * {@code e1} and {@code e2} in the set. If the user attempts to add
+ * an element to the set that violates this constraint, the
+ * {@code add} call will throw a {@code ClassCastException}.
+ *
+ * @param comparator the comparator that will be used to order this set.
+ * If {@code null}, the {@linkplain Comparable natural
+ * ordering} of the elements will be used.
+ */
+ public WeakTreeSet(Comparator<? super E> c) {
+ super(new TreeSet<ComparableWeakReference<E>>(
+ new ReferenceComparator<E,ComparableWeakReference<E>>(c)),
+ ComparableWeakReference.class);
+ }
+
+ @Override
+ protected final ComparableWeakReference<E> createReference(E element) {
+ return new ComparableWeakReference<E>(element);
+ }
+
+}
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueHashMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueHashMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueHashMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -22,12 +22,13 @@
package org.arakhne.util.ref;
+import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.Map;
import java.util.WeakHashMap;
/**
- * A <tt>Map</tt> implementation with <em>weak values</em>. An entry in a
+ * A <tt>Map</tt> implementation with {@link WeakReference weak values}. An entry in a
* <tt>WeakValueHashMap</tt> will automatically be removed when its value is no
* longer in ordinary use or null.
* <p>
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -22,11 +22,12 @@
package org.arakhne.util.ref;
+import java.lang.ref.WeakReference;
import java.util.Map;
import java.util.WeakHashMap;
/**
- * A <tt>Map</tt> implementation with <em>weak values</em>. An entry in a
+ * A <tt>Map</tt> implementation with {@link WeakReference weak values}. An entry in a
* <tt>WeakValueMap</tt> will automatically be removed when its value is no
* longer in ordinary use or null.
* <p>
Modified: trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueTreeMap.java
===================================================================
--- trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueTreeMap.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/main/java/org/arakhne/util/ref/WeakValueTreeMap.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -22,6 +22,7 @@
package org.arakhne.util.ref;
+import java.lang.ref.WeakReference;
import java.util.Comparator;
import java.util.Map;
import java.util.SortedMap;
@@ -29,7 +30,7 @@
import java.util.WeakHashMap;
/**
- * A <tt>Map</tt> implementation with <em>weak values</em>. An entry in a
+ * A <tt>Map</tt> implementation with {@link WeakReference weak values}. An entry in a
* <tt>WeakValueTreeMap</tt> will automatically be removed when its value is no
* longer in ordinary use or null.
* <p>
Modified: trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/AbstractCollectionTestCase.java
===================================================================
--- trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/AbstractCollectionTestCase.java 2012-08-15 18:56:37 UTC (rev 370)
+++ trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/AbstractCollectionTestCase.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -28,12 +28,13 @@
/**
* @param <OBJ>
+ * @param <COL>
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
-public abstract class AbstractCollectionTestCase<OBJ> extends AbstractTestCase {
+public abstract class AbstractCollectionTestCase<OBJ,COL extends Collection<OBJ>> extends AbstractTestCase {
private final Random RANDOM = new Random();
@@ -45,7 +46,7 @@
protected ArrayList<OBJ> unreference;
/**
*/
- protected Collection<OBJ> collection;
+ protected COL collection;
@Override
public void setUp() throws Exception {
@@ -73,7 +74,7 @@
/**
* @return a collection
*/
- protected abstract Collection<OBJ> createCollection();
+ protected abstract COL createCollection();
/**
* @param toAdd
Added: trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/SoftHashSetTest.java
===================================================================
--- trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/SoftHashSetTest.java (rev 0)
+++ trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/SoftHashSetTest.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -0,0 +1,60 @@
+/* $Id$
+ *
+ * Copyright (C) 2011-12 Stephane GALLAND.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.util.ref;
+
+import java.util.Collection;
+import java.util.Set;
+
+/**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class SoftHashSetTest extends AbstractCollectionTestCase<String,Set<String>> {
+
+ /**
+ */
+ public SoftHashSetTest() {
+ super();
+ }
+
+ @Override
+ protected String createContentInstance() {
+ return randomString();
+ }
+
+ @Override
+ protected Set<String> createCollection() {
+ return new SoftHashSet<String>();
+ }
+
+ @Override
+ protected void initCollectionWith(Collection<String> toAdd) {
+ this.collection.clear();
+ this.collection.addAll(toAdd);
+ }
+
+ @Override
+ protected void fillCollectionWith(Collection<String> toAdd) {
+ this.collection.addAll(toAdd);
+ }
+
+}
Added: trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/SoftTreeSetTest.java
===================================================================
--- trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/SoftTreeSetTest.java (rev 0)
+++ trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/SoftTreeSetTest.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -0,0 +1,60 @@
+/* $Id$
+ *
+ * Copyright (C) 2011-12 Stephane GALLAND.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.util.ref;
+
+import java.util.Collection;
+import java.util.Set;
+
+/**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class SoftTreeSetTest extends AbstractCollectionTestCase<String,Set<String>> {
+
+ /**
+ */
+ public SoftTreeSetTest() {
+ super();
+ }
+
+ @Override
+ protected String createContentInstance() {
+ return randomString();
+ }
+
+ @Override
+ protected Set<String> createCollection() {
+ return new SoftTreeSet<String>();
+ }
+
+ @Override
+ protected void initCollectionWith(Collection<String> toAdd) {
+ this.collection.clear();
+ this.collection.addAll(toAdd);
+ }
+
+ @Override
+ protected void fillCollectionWith(Collection<String> toAdd) {
+ this.collection.addAll(toAdd);
+ }
+
+}
Added: trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/WeakHashSetTest.java
===================================================================
--- trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/WeakHashSetTest.java (rev 0)
+++ trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/WeakHashSetTest.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -0,0 +1,60 @@
+/* $Id$
+ *
+ * Copyright (C) 2011-12 Stephane GALLAND.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.util.ref;
+
+import java.util.Collection;
+import java.util.Set;
+
+/**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class WeakHashSetTest extends AbstractCollectionTestCase<String,Set<String>> {
+
+ /**
+ */
+ public WeakHashSetTest() {
+ super();
+ }
+
+ @Override
+ protected String createContentInstance() {
+ return randomString();
+ }
+
+ @Override
+ protected Set<String> createCollection() {
+ return new WeakHashSet<String>();
+ }
+
+ @Override
+ protected void initCollectionWith(Collection<String> toAdd) {
+ this.collection.clear();
+ this.collection.addAll(toAdd);
+ }
+
+ @Override
+ protected void fillCollectionWith(Collection<String> toAdd) {
+ this.collection.addAll(toAdd);
+ }
+
+}
Added: trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/WeakTreeSetTest.java
===================================================================
--- trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/WeakTreeSetTest.java (rev 0)
+++ trunk/arakhneRefs/src/test/java/org/arakhne/util/ref/WeakTreeSetTest.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -0,0 +1,60 @@
+/* $Id$
+ *
+ * Copyright (C) 2011-12 Stephane GALLAND.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.util.ref;
+
+import java.util.Collection;
+import java.util.Set;
+
+/**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class WeakTreeSetTest extends AbstractCollectionTestCase<String,Set<String>> {
+
+ /**
+ */
+ public WeakTreeSetTest() {
+ super();
+ }
+
+ @Override
+ protected String createContentInstance() {
+ return randomString();
+ }
+
+ @Override
+ protected Set<String> createCollection() {
+ return new WeakTreeSet<String>();
+ }
+
+ @Override
+ protected void initCollectionWith(Collection<String> toAdd) {
+ this.collection.clear();
+ this.collection.addAll(toAdd);
+ }
+
+ @Override
+ protected void fillCollectionWith(Collection<String> toAdd) {
+ this.collection.addAll(toAdd);
+ }
+
+}
Added: trunk/arakhneVmutils/arakhneVmutils-java/src/test/java/org/arakhne/vmutil/ReflectionUtilTest.java
===================================================================
--- trunk/arakhneVmutils/arakhneVmutils-java/src/test/java/org/arakhne/vmutil/ReflectionUtilTest.java (rev 0)
+++ trunk/arakhneVmutils/arakhneVmutils-java/src/test/java/org/arakhne/vmutil/ReflectionUtilTest.java 2012-08-15 20:09:50 UTC (rev 371)
@@ -0,0 +1,124 @@
+/* $Id$
+ *
+ * Copyright (C) 2012 Stephane GALLAND.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ * This program is free software; you can redistribute it and/or modify
+ */
+package org.arakhne.vmutil;
+
+import java.lang.reflect.Array;
+
+import junit.framework.TestCase;
+
+/**
+ * @author $Author: galland$
+ * @version $Name$ $Revision$ $Date$
+ * @mavengroupid org.arakhne.afc
+ * @mavenartifactid arakhneVmutils
+ */
+public class ReflectionUtilTest extends TestCase {
+
+ /**
+ */
+ public static void testMatchesParametersClassObjectArray() {
+ assertTrue(ReflectionUtil.matchesParameters(
+ new Class<?>[0],
+ new Object[0]));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[0],
+ new Object[] { 1 }));
+
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class },
+ new Object[0]));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class },
+ new Object[] { 'c' }));
+ assertTrue(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class },
+ new Object[] { 3. }));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class },
+ new Object[] { 4f }));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class },
+ new Object[] { 1 }));
+
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[0]));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 'c' }));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 3. }));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 4. }));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 1 }));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 'c', "a" })); //$NON-NLS-1$
+ assertTrue(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 3., "a" })); //$NON-NLS-1$
+ assertTrue(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 4., "a" })); //$NON-NLS-1$
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 1, "a" })); //$NON-NLS-1$
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 'c', true }));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 3., true }));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 4., true }));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class },
+ new Object[] { 1, true }));
+
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class, Array.class },
+ new Object[0]));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class, Array.class },
+ new Object[] { 1. }));
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class, Array.class },
+ new Object[] { 1., "a" })); //$NON-NLS-1$
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class, Array.class },
+ new Object[] { 1., "a", null })); //$NON-NLS-1$
+ assertFalse(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class, Array.class },
+ new Object[] { 1., "a", new int[0] })); //$NON-NLS-1$
+ assertTrue(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Double.class, String.class, int[].class },
+ new Object[] { 1., "a", new int[0] })); //$NON-NLS-1$
+
+ assertTrue(ReflectionUtil.matchesParameters(
+ new Class<?>[] { Number.class, String.class, int[].class },
+ new Object[] { 1., "a", new int[0] })); //$NON-NLS-1$
+ }
+
+}