[Arakhnę-Dev] [396] * Move utilities from SFC to AFC. |
[ Thread Index |
Date Index
| More arakhne.org/dev Archives
]
Revision: 396
Author: galland
Date: 2013-03-21 17:23:02 +0100 (Thu, 21 Mar 2013)
Log Message:
-----------
* Move utilities from SFC to AFC.
Modified Paths:
--------------
trunk/util/pom.xml
trunk/util/src/main/java/org/arakhne/afc/sizediterator/EmptyIterator.java
trunk/util/src/main/java/org/arakhne/afc/sizediterator/ModifiableCollectionSizedIterator.java
trunk/util/src/main/java/org/arakhne/afc/sizediterator/MultiSizedIterator.java
trunk/util/src/main/java/org/arakhne/afc/sizediterator/SizedIterator.java
trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableCollectionSizedIterator.java
trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableMapKeySizedIterator.java
trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableMapValueSizedIterator.java
trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableSizedIterator.java
trunk/util/src/main/java/org/arakhne/afc/util/ListUtil.java
Added Paths:
-----------
trunk/util/src/main/java/org/arakhne/afc/sizediterator/ArraySizedIterator.java
trunk/util/src/main/java/org/arakhne/afc/sizediterator/CollectionSizedIterator.java
trunk/util/src/main/java/org/arakhne/afc/sizediterator/SingleIterator.java
trunk/util/src/main/java/org/arakhne/afc/util/ArrayUtil.java
trunk/util/src/main/java/org/arakhne/afc/util/NaturalOrderComparator.java
trunk/util/src/main/java/org/arakhne/afc/util/OutputParameter.java
trunk/util/src/main/java/org/arakhne/afc/util/UnsupportedNaturalOrderException.java
trunk/util/src/test/java/org/
trunk/util/src/test/java/org/arakhne/
trunk/util/src/test/java/org/arakhne/afc/
trunk/util/src/test/java/org/arakhne/afc/sizediterator/
trunk/util/src/test/java/org/arakhne/afc/sizediterator/CollectionSizedIteratorTest.java
trunk/util/src/test/java/org/arakhne/afc/sizediterator/SingleIteratorTest.java
trunk/util/src/test/java/org/arakhne/afc/util/
trunk/util/src/test/java/org/arakhne/afc/util/ListUtilTest.java
trunk/util/src/test/java/org/arakhne/afc/util/NaturalOrderComparatorTest.java
Modified: trunk/util/pom.xml
===================================================================
--- trunk/util/pom.xml 2013-03-21 14:28:10 UTC (rev 395)
+++ trunk/util/pom.xml 2013-03-21 16:23:02 UTC (rev 396)
@@ -22,6 +22,11 @@
<groupId>org.arakhne.afc</groupId>
<artifactId>arakhneVmutils</artifactId>
</dependency>
+ <dependency>
+ <groupId>junit</groupId>
+ <artifactId>junit</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
Added: trunk/util/src/main/java/org/arakhne/afc/sizediterator/ArraySizedIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/ArraySizedIterator.java (rev 0)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/ArraySizedIterator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -0,0 +1,114 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2010-2011 Janus Core Developers
+ * Copyright (C) 2012-13 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.afc.sizediterator;
+
+import java.util.NoSuchElementException;
+
+/** Iterator which is disabling the {@code remove()} function.
+ *
+ * @param <OBJ> is the type of the objects to iterator on.
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class ArraySizedIterator<OBJ> implements SizedIterator<OBJ> {
+
+ private final OBJ[] array;
+ private final int length;
+ private int index = 0;
+ private OBJ next;
+ private int nextIndex;
+
+ /**
+ * @param array
+ */
+ public ArraySizedIterator(OBJ[] array) {
+ this.array = array;
+ this.length = (array!=null) ? array.length : 0;
+ searchNext();
+ }
+
+ private void searchNext() {
+ this.next = null;
+ OBJ o;
+ while (this.index>=0 && this.index<this.length) {
+ o = this.array[this.index++];
+ if (o!=null) {
+ this.next = o;
+ return;
+ }
+ }
+ this.index = -1;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return this.next!=null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public OBJ next() {
+ if (this.next==null) throw new NoSuchElementException();
+ OBJ o = this.next;
+ this.nextIndex = this.index - 1;
+ searchNext();
+ return o;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ if (this.nextIndex<0) throw new NoSuchElementException();
+ return this.nextIndex;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int totalSize() {
+ return this.length;
+ }
+
+ @Override
+ public int rest() {
+ return this.length - this.nextIndex;
+ }
+
+}
Added: trunk/util/src/main/java/org/arakhne/afc/sizediterator/CollectionSizedIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/CollectionSizedIterator.java (rev 0)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/CollectionSizedIterator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -0,0 +1,101 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2010-2011 Janus Core Developers
+ * Copyright (C) 2012-13 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.afc.sizediterator;
+
+import java.util.Collection;
+import java.util.Iterator;
+
+/** Iterator on collection.
+ *
+ * @param <OBJ> is the type of the objects to iterator on.
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class CollectionSizedIterator<OBJ> implements SizedIterator<OBJ> {
+
+ private final Iterator<OBJ> iterator;
+ private int length, index;
+
+ /**
+ * @param collection
+ */
+ public CollectionSizedIterator(Collection<OBJ> collection) {
+ this.iterator = collection.iterator();
+ this.length = collection.size();
+ this.index = -1;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return this.iterator.hasNext();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public OBJ next() {
+ OBJ n = this.iterator.next();
+ this.index ++;
+ return n;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void remove() {
+ this.iterator.remove();
+ this.length --;
+ this.index --;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ return this.index - 1;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int totalSize() {
+ return this.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int rest() {
+ return this.length - (this.index+1);
+ }
+
+}
Modified: trunk/util/src/main/java/org/arakhne/afc/sizediterator/EmptyIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/EmptyIterator.java 2013-03-21 14:28:10 UTC (rev 395)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/EmptyIterator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -56,7 +56,7 @@
/**
*/
- private EmptyIterator() {
+ protected EmptyIterator() {
//
}
@@ -96,6 +96,14 @@
* {@inheritDoc}
*/
@Override
+ public final int index() {
+ return -1;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
public final int totalSize() {
return 0;
}
Modified: trunk/util/src/main/java/org/arakhne/afc/sizediterator/ModifiableCollectionSizedIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/ModifiableCollectionSizedIterator.java 2013-03-21 14:28:10 UTC (rev 395)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/ModifiableCollectionSizedIterator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -83,6 +83,14 @@
* {@inheritDoc}
*/
@Override
+ public int index() {
+ return this.total - this.rest;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
public int totalSize() {
return this.total;
}
Modified: trunk/util/src/main/java/org/arakhne/afc/sizediterator/MultiSizedIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/MultiSizedIterator.java 2013-03-21 14:28:10 UTC (rev 395)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/MultiSizedIterator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -117,4 +117,12 @@
return this.total - this.returned;
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ return totalSize() - rest();
+ }
+
}
Added: trunk/util/src/main/java/org/arakhne/afc/sizediterator/SingleIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/SingleIterator.java (rev 0)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/SingleIterator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -0,0 +1,98 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2010-2011 Janus Core Developers
+ * Copyright (C) 2012-13 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.afc.sizediterator;
+
+import java.util.NoSuchElementException;
+
+/** Single iterator.
+ *
+ * @param <OBJ> is the type of the objects to iterator on.
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class SingleIterator<OBJ> implements SizedIterator<OBJ> {
+
+ private OBJ object;
+
+ /**
+ * @param obj
+ */
+ public SingleIterator(OBJ obj) {
+ this.object = obj;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return this.object!=null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public OBJ next() {
+ if (this.object!=null) {
+ OBJ obj = this.object;
+ this.object = null;
+ return obj;
+ }
+ throw new NoSuchElementException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ if (this.object==null)
+ return 0;
+ return -1;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int totalSize() {
+ return 1;
+ }
+
+ @Override
+ public int rest() {
+ if (this.object==null) return 0;
+ return 1;
+ }
+
+}
Modified: trunk/util/src/main/java/org/arakhne/afc/sizediterator/SizedIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/SizedIterator.java 2013-03-21 14:28:10 UTC (rev 395)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/SizedIterator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -49,4 +49,10 @@
*/
public int rest();
+ /** Replies the position of the last replied element in the iterated collection.
+ *
+ * @return the index of the last element replied by <code>next()</code>.
+ */
+ public int index();
+
}
Modified: trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableCollectionSizedIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableCollectionSizedIterator.java 2013-03-21 14:28:10 UTC (rev 395)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableCollectionSizedIterator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -72,6 +72,14 @@
* {@inheritDoc}
*/
@Override
+ public int index() {
+ return this.total - this.rest;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
public int totalSize() {
return this.total;
}
Modified: trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableMapKeySizedIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableMapKeySizedIterator.java 2013-03-21 14:28:10 UTC (rev 395)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableMapKeySizedIterator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -75,6 +75,14 @@
* {@inheritDoc}
*/
@Override
+ public int index() {
+ return this.total - this.rest;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
public int totalSize() {
return this.total;
}
Modified: trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableMapValueSizedIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableMapValueSizedIterator.java 2013-03-21 14:28:10 UTC (rev 395)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableMapValueSizedIterator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -75,6 +75,14 @@
* {@inheritDoc}
*/
@Override
+ public int index() {
+ return this.total - this.rest;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
public int totalSize() {
return this.total;
}
Modified: trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableSizedIterator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableSizedIterator.java 2013-03-21 14:28:10 UTC (rev 395)
+++ trunk/util/src/main/java/org/arakhne/afc/sizediterator/UnmodifiableSizedIterator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -96,6 +96,14 @@
* {@inheritDoc}
*/
@Override
+ public int index() {
+ return this.original.index();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
public int totalSize() {
return this.original.totalSize();
}
Added: trunk/util/src/main/java/org/arakhne/afc/util/ArrayUtil.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/util/ArrayUtil.java (rev 0)
+++ trunk/util/src/main/java/org/arakhne/afc/util/ArrayUtil.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -0,0 +1,1517 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2010-2011 Janus Core Developers
+ * Copyright (C) 2012-13 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.afc.util;
+
+import java.lang.reflect.Array;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Iterator;
+import java.util.NoSuchElementException;
+import java.util.Random;
+
+import org.arakhne.afc.sizediterator.ArraySizedIterator;
+import org.arakhne.afc.sizediterator.SizedIterator;
+
+/**
+ * Some utilities functions for arrays.
+ *
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class ArrayUtil {
+
+ /** Reverse the specified array.
+ *
+ * @param <T>
+ * @param tab
+ */
+ public static <T> void reverse(T[] tab) {
+ T tmp;
+ for(int i=0; i<tab.length/2; ++i) {
+ tmp = tab[i];
+ tab[i] = tab[tab.length-i-1];
+ tab[tab.length-i-1] = tmp;
+ }
+ }
+
+ /** Replies an array that corresponds to the given collection.
+ *
+ * @param <T> is the type of the elements.
+ * @param collection is the collection to translate
+ * @param clazz is the type of the elements.
+ * @return the array.
+ */
+ public static <T> T[] toArray(Collection<? extends T> collection, Class<T> clazz) {
+ int size = (collection==null) ? 0 : collection.size();
+ T[] tab = newInstance(clazz, size);
+ if ((collection!=null)&&(size>0)) collection.toArray(tab);
+ return tab;
+ }
+
+ /** Replies an array that corresponds to the given collection.
+ * <p>
+ * This function clear the content of the given collection.
+ *
+ * @param <T> is the type of the elements.
+ * @param collection is the collection to translate
+ * @param clazz is the type of the elements.
+ * @return the array.
+ */
+ public static <T> T[] toArrayAndClear(Collection<? extends T> collection, Class<T> clazz) {
+ T[] tab = toArray(collection,clazz);
+ collection.clear();
+ return tab;
+ }
+
+ /** Merge the arrays.
+ * <p>
+ * This function does not remove the <code>null</code> values.
+ *
+ * @param <T> is the type of the elements.
+ * @param clazz is the type of the elements.
+ * @param arrays are the arrays to merge.
+ * @return the array.
+ */
+ public static <T> T[] merge(Class<T> clazz, T[]... arrays) {
+ int length = 0;
+ for (T[] tab : arrays) {
+ if (tab!=null)
+ length += tab.length;
+ }
+ T[] result = newInstance(clazz,length);
+ int i=0;
+ for (T[] tab : arrays) {
+ if (tab!=null) {
+ System.arraycopy(tab,0,result,i,tab.length);
+ i += tab.length;
+ }
+ }
+ return result;
+ }
+
+ /** Merge the arrays.
+ * <p>
+ * This function removes the <code>null</code> values.
+ *
+ * @param <T> is the type of the elements.
+ * @param clazz is the type of the elements.
+ * @param arrays are the arrays to merge.
+ * @return the array.
+ */
+ public static <T> T[] merge_without_null(Class<T> clazz, T[]... arrays) {
+ int length = 0;
+ for (T[] tab : arrays) {
+ if (tab!=null) {
+ for (T t : tab) {
+ if (t!=null) ++length;
+ }
+ }
+ }
+ T[] result = newInstance(clazz,length);
+ int i=0;
+ for (T[] tab : arrays) {
+ if (tab!=null) {
+ for (T t : tab) {
+ if (t!=null) {
+ result[i] = t;
+ ++i;
+ }
+ }
+ }
+ }
+ return result;
+ }
+
+ /** Merge the elements to make an array.
+ * <p>
+ * This function does not remove the <code>null</code> values.
+ *
+ * @param <T> is the type of the elements.
+ * @param clazz is the type of the elements.
+ * @param elements are the elements to merge.
+ * @return the array.
+ */
+ public static <T> T[] merge(Class<T> clazz, T... elements) {
+ T[] result = newInstance(clazz,elements.length);
+ System.arraycopy(elements,0,result,0,elements.length);
+ return result;
+ }
+
+ /** Merge the elements to make an array.
+ * <p>
+ * This function removes the <code>null</code> values.
+ *
+ * @param <T> is the type of the elements.
+ * @param clazz is the type of the elements.
+ * @param elements are the elements to merge.
+ * @return the array.
+ */
+ public static <T> T[] merge_without_null(Class<T> clazz, T... elements) {
+ int l = 0;
+ for (T t : elements) {
+ if (t!=null) ++l;
+ }
+ T[] result = newInstance(clazz,l);
+ int i=0;
+ for (T t : elements) {
+ if (t!=null) result[i++] = t;
+ }
+ return result;
+ }
+
+ /** Merge the elements to make an array.
+ * <p>
+ * This function does not remove the <code>null</code> values.
+ *
+ * @param <T> is the type of the elements.
+ * @param source is the first array to merge.
+ * @param clazz is the type of the elements.
+ * @param elements are the elements to merge.
+ * @return the array.
+ */
+ public static <T> T[] merge(Class<T> clazz, T[] source, T... elements) {
+ T[] result = newInstance(clazz,source.length+elements.length);
+ System.arraycopy(source,0,result,0,source.length);
+ System.arraycopy(elements,0,result,source.length,elements.length);
+ return result;
+ }
+
+ /** Merge the elements to make an array.
+ * <p>
+ * This function removes the <code>null</code> values.
+ *
+ * @param <T> is the type of the elements.
+ * @param source is the first array to merge.
+ * @param clazz is the type of the elements.
+ * @param elements are the elements to merge.
+ * @return the array.
+ */
+ public static <T> T[] merge_without_null(Class<T> clazz, T[] source, T... elements) {
+ int l=0;
+ for (T t : source) {
+ if (t!=null) ++l;
+ }
+ for (T t : elements) {
+ if (t!=null) ++l;
+ }
+ T[] result = newInstance(clazz,l);
+ int i=0;
+ for (T t : source) {
+ if (t!=null) result[i++] = t;
+ }
+ for (T t : elements) {
+ if (t!=null) result[i++] = t;
+ }
+ return result;
+ }
+
+ /** Remove the given elements from the array.
+ *
+ * @param <T> is the type of the elements.
+ * @param clazz is the type of the elements.
+ * @param source is the array to scan
+ * @param to_remove are the elements to remove.
+ * @return the array without the removed elements.
+ */
+ public static <T> T[] removeElements(Class<T> clazz, T[] source, T... to_remove) {
+ ArrayList<T> list = new ArrayList<T>();
+ list.addAll(Arrays.asList(source));
+ for (T t : to_remove) {
+ list.remove(t);
+ }
+ return toArrayAndClear(list,clazz);
+ }
+
+ /** Cast the specified array and put <code>null</code> is the array when
+ * the element could not be casted.
+ *
+ * @param <TO> is the type of the elements before the cast.
+ * @param <TT> is the type of the elements after the cast.
+ * @param original_array is the array to cast
+ * @param clazz is the casting type
+ * @return the array in which each element was casted according to the given type.
+ * @see #castRestrictedArray(Object[], Class)
+ */
+ public static <TO,TT> TT[] castArray(TO[] original_array, Class<TT> clazz) {
+ int l = (original_array==null) ? 0 : original_array.length;
+ TT[] result = newInstance(clazz,l);
+ if ((original_array!=null)&&(l>0)) {
+ int index = 0;
+ for (TO to : original_array) {
+ result[index] = (clazz.isInstance(to)) ? clazz.cast(to) : null;
+ }
+ }
+ return result;
+ }
+
+ /** Cast the specified array and put <code>null</code> is the array when
+ * the element could not be casted.
+ *
+ * @param <TO> is the type of the elements before the cast.
+ * @param <TT> is the type of the elements after the cast.
+ * @param original_array is the array to cast
+ * @param clazz is the casting type
+ * @return the array in which each element was casted according to the given type.
+ * @see #castRestrictedArray(Object[], Class)
+ */
+ public static <TO,TT> TT[] castArray(Collection<TO> original_array, Class<TT> clazz) {
+ int l = (original_array==null) ? 0 : original_array.size();
+ TT[] result = newInstance(clazz,l);
+ if ((original_array!=null)&&(l>0)) {
+ int index = 0;
+ for (TO to : original_array) {
+ result[index] = (clazz.isInstance(to)) ? clazz.cast(to) : null;
+ }
+ }
+ return result;
+ }
+
+ /** Replies an array in which all the elements must
+ * respect the given comparator.
+ * <p>
+ * The respect of the comparator is done when the
+ * comparator replies equals.
+ *
+ * @param <T> is the type of the elements
+ * @param original_array is the array to cast
+ * @param clazz is the casting type
+ * @param comparator is filtering the elements.
+ * @return the array in which each element was casted according to the given type.
+ */
+ public static <T> T[] restrictArray(T[] original_array, Class<T> clazz, Filter<T> comparator) {
+ int l = (original_array==null) ? 0 : original_array.length;
+ final ArrayList<T> result = new ArrayList<T>();
+ if ((original_array!=null)&&(l>0)) {
+ for (T to : original_array) {
+ if (comparator.filter(to)) {
+ result.add(to);
+ }
+ }
+ }
+ return toArrayAndClear(result, clazz);
+ }
+
+ /** Cast the specified array and remove the elements that could not be casted.
+ *
+ * @param <TO> is the type of the elements before the cast.
+ * @param <TT> is the type of the elements after the cast.
+ * @param original_array is the array to cast
+ * @param clazz is the casting type
+ * @return the array in which each element was casted according to the given type.
+ * @see #castArray(Object[], Class)
+ */
+ @SuppressWarnings("unchecked")
+ public static <TO,TT> TT[] castRestrictedArray(TO[] original_array, Class<TT> clazz) {
+ int l = (original_array==null) ? 0 : original_array.length;
+ final ArrayList<TT> result = new ArrayList<TT>();
+ if ((original_array!=null)&&(l>0)) {
+ for (TO to : original_array) {
+ if (clazz.isInstance(to)) {
+ result.add((TT)to);
+ }
+ }
+ }
+ return toArrayAndClear(result, clazz);
+ }
+
+ /** Cast the specified array and remove the elements that could not be casted.
+ *
+ * @param <TO> is the type of the elements before the cast.
+ * @param <TT> is the type of the elements after the cast.
+ * @param original_array is the array to cast
+ * @param clazz is the casting type
+ * @return the array in which each element was casted according to the given type.
+ * @see #castArray(Object[], Class)
+ */
+ @SuppressWarnings("unchecked")
+ public static <TO,TT> TT[] castRestrictedArray(Collection<TO> original_array, Class<TT> clazz) {
+ int l = (original_array==null) ? 0 : original_array.size();
+ final ArrayList<TT> result = new ArrayList<TT>();
+ if ((original_array!=null)&&(l>0)) {
+ for (TO to : original_array) {
+ if (clazz.isInstance(to)) {
+ result.add((TT)to);
+ }
+ }
+ }
+ return toArrayAndClear(result, clazz);
+ }
+
+ /** Replies of the given element is in the array.
+ * <p>
+ * This function does not call {@link Object#equals(java.lang.Object)}.
+ * It tests the equality on the object references.
+ *
+ * @param <T> is the type of the elements.
+ * @param elt is the element to search for.
+ * @param array is the array inside which the search must be done.
+ * @return <code>true</code> if the element is inside the array, otherwise <code>false</code>
+ */
+ public static <T> boolean containsObject(T elt, T[] array) {
+ for (T t : array) {
+ if (t==elt) return true;
+ }
+ return false;
+ }
+
+ /** Replies of the given element is in the array.
+ * <p>
+ * This function does not call {@link Object#equals(java.lang.Object)}.
+ * It tests the equality on the object references.
+ *
+ * @param <T> is the type of the elements.
+ * @param elts are the elements to search for.
+ * @param array is the array inside which the search must be done.
+ * @return <code>true</code> if the elements are inside the array, otherwise <code>false</code>
+ */
+ public static <T> boolean containsAllObjects(T[] elts, T[] array) {
+ boolean found;
+ for (T elt : elts) {
+ found = false;
+ for (T t : array) {
+ if (t==elt) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) return false;
+ }
+ return true;
+ }
+
+ /** Replies of the given element is in the array.
+ * <p>
+ * This function is based on {@link Object#equals(java.lang.Object)}.
+ *
+ * @param <T> is the type of the elements.
+ * @param elt is the element to search for.
+ * @param array is the array inside which the search must be done.
+ * @return <code>true</code> if the element is inside the array, otherwise <code>false</code>
+ */
+ public static <T> boolean contains(T elt, T... array) {
+ for (T t : array) {
+ if ((t==elt)||
+ ((t!=null)&&
+ (t.equals(elt)))) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /** Replies of the given element is in the sorted array.
+ * <p>
+ * This function assumes that the given array is sorted
+ * according to the given comparator.
+ * A dichotomic algorithm is used.
+ *
+ * @param <T> is the type of the elements.
+ * @param comparator is the comparator used to sort the array.
+ * @param elt is the element to search for.
+ * @param array is the array inside which the search must be done.
+ * @return <code>true</code> if the element is inside the array, otherwise <code>false</code>
+ * @since 4.0
+ */
+ public static <T> boolean contains(Comparator<T> comparator, T elt, T... array) {
+ assert(comparator!=null);
+ assert(elt!=null);
+ assert(array!=null);
+
+ int f = 0;
+ int l = array.length-1;
+ int c, cmp;
+ T indata;
+ while (l>=f) {
+ c = (f+l)/2;
+ indata = array[c];
+ cmp = comparator.compare(elt, indata);
+ if (cmp==0) {
+ return true;
+ }
+ else if (cmp<0) {
+ l = c-1;
+ }
+ else {
+ f = c+1;
+ }
+ }
+ return false;
+ }
+
+ /** Replies if the given elements is in the array.
+ * <p>
+ * This function is based on {@link Object#equals(java.lang.Object)}.
+ *
+ * @param <T> is the type of the elements.
+ * @param elts are the elements to search for.
+ * @param array is the array inside which the search must be done.
+ * @return <code>true</code> if the elements are inside the array, otherwise <code>false</code>
+ */
+ public static <T> boolean containsAll(T[] elts, T[] array) {
+ boolean found;
+ for (T elt : elts) {
+ found = false;
+ for (T t : array) {
+ if ((t==elt)||
+ ((t!=null)&&(t.equals(elt)))) {
+ found = true;
+ break;
+ }
+ }
+ if (!found) return false;
+ }
+ return true;
+ }
+
+ /** Replies of one of the given elements is in the array.
+ * <p>
+ * This function is based on {@link Object#equals(java.lang.Object)}.
+ *
+ * @param <T> is the type of the elements.
+ * @param elts is the first array.
+ * @param array is the second array.
+ * @return <code>true</code> if an intersection is existing, otherwise <code>false</code>
+ */
+ public static <T> boolean intersects(T[] elts, T[] array) {
+ for (T t : array) {
+ for (T elt : elts) {
+ if ((elt==t)||
+ ((t!=null)&&
+ (t.equals(elt)))) {
+ return true;
+ }
+ }
+ }
+ return false;
+ }
+
+ /** Create an instance of array.
+ *
+ * @param <T> is the type of the elements.
+ * @param clazz is the type of the elements.
+ * @param size is the size of the new array.
+ * @return the new array.
+ */
+ @SuppressWarnings("unchecked")
+ public static <T> T[] newInstance(Class<T> clazz, int size) {
+ if (size<0) throw new IndexOutOfBoundsException(size+"<0"); //$NON-NLS-1$
+ return (T[])Array.newInstance(clazz, size);
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param <T> is the type of the elements.
+ * @param array is the array to shuffle.
+ */
+ public static <T> void shuffle(T[] array) {
+ shuffle(array,new Random());
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param <T> is the type of the elements.
+ * @param array is the array to shuffle.
+ * @param rnd is the random number generator to use.
+ */
+ public static <T> void shuffle(T[] array, Random rnd) {
+ int ir;
+ T tmp;
+ for (int i=array.length; i>1; --i) {
+ ir = rnd.nextInt(i);
+ tmp = array[i-1];
+ array[i-1] = array[ir];
+ array[ir] = tmp;
+ }
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ */
+ public static void shuffle(boolean[] array) {
+ shuffle(array, new Random());
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ * @param rnd is the random number generator to use.
+ */
+ public static void shuffle(boolean[] array, Random rnd) {
+ int ir;
+ boolean tmp;
+ for (int i=array.length; i>1; --i) {
+ ir = rnd.nextInt(i);
+ tmp = array[i-1];
+ array[i-1] = array[ir];
+ array[ir] = tmp;
+ }
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ */
+ public static void shuffle(byte[] array) {
+ shuffle(array,new Random());
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ * @param rnd is the random number generator to use.
+ */
+ public static void shuffle(byte[] array, Random rnd) {
+ int ir;
+ byte tmp;
+ for (int i=array.length; i>1; --i) {
+ ir = rnd.nextInt(i);
+ tmp = array[i-1];
+ array[i-1] = array[ir];
+ array[ir] = tmp;
+ }
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ */
+ public static void shuffle(char[] array) {
+ shuffle(array,new Random());
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ * @param rnd is the random number generator to use.
+ */
+ public static void shuffle(char[] array, Random rnd) {
+ int ir;
+ char tmp;
+ for (int i=array.length; i>1; --i) {
+ ir = rnd.nextInt(i);
+ tmp = array[i-1];
+ array[i-1] = array[ir];
+ array[ir] = tmp;
+ }
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ */
+ public static void shuffle(int[] array) {
+ shuffle(array,new Random());
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ * @param rnd is the random number generator to use.
+ */
+ public static void shuffle(int[] array, Random rnd) {
+ int ir;
+ int tmp;
+ for (int i=array.length; i>1; --i) {
+ ir = rnd.nextInt(i);
+ tmp = array[i-1];
+ array[i-1] = array[ir];
+ array[ir] = tmp;
+ }
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ */
+ public static void shuffle(long[] array) {
+ shuffle(array,new Random());
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ * @param rnd is the random number generator to use.
+ */
+ public static void shuffle(long[] array, Random rnd) {
+ int ir;
+ long tmp;
+ for (int i=array.length; i>1; --i) {
+ ir = rnd.nextInt(i);
+ tmp = array[i-1];
+ array[i-1] = array[ir];
+ array[ir] = tmp;
+ }
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ */
+ public static void shuffle(float[] array) {
+ shuffle(array,new Random());
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ * @param rnd is the random number generator to use.
+ */
+ public static void shuffle(float[] array, Random rnd) {
+ int ir;
+ float tmp;
+ for (int i=array.length; i>1; --i) {
+ ir = rnd.nextInt(i);
+ tmp = array[i-1];
+ array[i-1] = array[ir];
+ array[ir] = tmp;
+ }
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ */
+ public static void shuffle(double[] array) {
+ shuffle(array,new Random());
+ }
+
+ /** Shuffle the specified array.
+ *
+ * @param array is the array to shuffle.
+ * @param rnd is the random number generator to use.
+ */
+ public static void shuffle(double[] array, Random rnd) {
+ int ir;
+ double tmp;
+ for (int i=array.length; i>1; --i) {
+ ir = rnd.nextInt(i);
+ tmp = array[i-1];
+ array[i-1] = array[ir];
+ array[ir] = tmp;
+ }
+ }
+
+ /**
+ * Replies a string representation of the given object.
+ * <p>
+ * This function supports the base type's arrays.
+ *
+ * @param o is the object to translate.
+ * @return a string representation of the given object.
+ */
+ public static String toString(Object o) {
+ if (o==null) return null;
+ if (o instanceof boolean[])
+ return Arrays.toString((boolean[])o);
+ if (o instanceof byte[])
+ return Arrays.toString((byte[])o);
+ if (o instanceof char[])
+ return Arrays.toString((char[])o);
+ if (o instanceof short[])
+ return Arrays.toString((short[])o);
+ if (o instanceof int[])
+ return Arrays.toString((int[])o);
+ if (o instanceof long[])
+ return Arrays.toString((long[])o);
+ if (o instanceof float[])
+ return Arrays.toString((float[])o);
+ if (o instanceof double[])
+ return Arrays.toString((double[])o);
+ if (o instanceof Object[])
+ return Arrays.toString((Object[])o);
+ return o.toString();
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param <T> is the type of the object to iterate on.
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ */
+ public static <T> SizedIterator<T> sizedIterator(T[] array) {
+ return new ArraySizedIterator<T>(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param <T> is the type of the object to iterate on.
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static <T> Iterator<T> iterator(T[] array) {
+ return new ArraySizedIterator<T>(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static SizedIterator<Boolean> sizedIterator(boolean[] array) {
+ return new NativeBooleanToObjectBooleanIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static Iterator<Boolean> iterator(boolean[] array) {
+ return new NativeBooleanToObjectBooleanIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static SizedIterator<Character> sizedIterator(char[] array) {
+ return new NativeCharacterToObjectCharacterIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static Iterator<Character> iterator(char[] array) {
+ return new NativeCharacterToObjectCharacterIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static SizedIterator<Byte> sizedIterator(byte[] array) {
+ return new NativeByteToObjectByteIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static Iterator<Byte> iterator(byte[] array) {
+ return new NativeByteToObjectByteIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static SizedIterator<Short> sizedIterator(short[] array) {
+ return new NativeShortToObjectShortIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static Iterator<Short> iterator(short[] array) {
+ return new NativeShortToObjectShortIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static SizedIterator<Integer> sizedIterator(int[] array) {
+ return new NativeIntegerToObjectIntegerIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static Iterator<Integer> iterator(int[] array) {
+ return new NativeIntegerToObjectIntegerIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static SizedIterator<Long> sizedIterator(long[] array) {
+ return new NativeLongToObjectLongIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static Iterator<Long> iterator(long[] array) {
+ return new NativeLongToObjectLongIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static SizedIterator<Float> sizedIterator(float[] array) {
+ return new NativeFloatToObjectFloatIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static Iterator<Float> iterator(float[] array) {
+ return new NativeFloatToObjectFloatIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static SizedIterator<Double> sizedIterator(double[] array) {
+ return new NativeDoubleToObjectDoubleIterator(array);
+ }
+
+ /** Replies a sized iterator on the objects.
+ *
+ * @param array are the objects to iterate on.
+ * @return an iterator
+ * @since 4.1
+ */
+ public static Iterator<Double> iterator(double[] array) {
+ return new NativeDoubleToObjectDoubleIterator(array);
+ }
+
+ /**
+ * Some utilities functions for arrays.
+ *
+ * @param <T> is the type of the value to pass to the filter.
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+ public static interface Filter<T> {
+
+ /**
+ * @param o is the value to filter
+ * @return <code>true</code> if the given value is acceptable, otherwise <code>false</code>
+ */
+ public boolean filter(T o);
+
+ }
+
+ /**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavenartifactid $ArtifactId$
+ * @mavengroupid $GroupId$
+ * @since SFC 4.1
+ */
+ private static class NativeBooleanToObjectBooleanIterator implements SizedIterator<Boolean> {
+
+ private final boolean[] array;
+ private int idx = 0;
+
+ /**
+ * @param data
+ */
+ public NativeBooleanToObjectBooleanIterator(boolean[] data) {
+ this.array = data;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return this.array!=null && this.idx>=0 && this.idx<this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Boolean next() {
+ if (this.array!=null && this.idx>=0 && this.idx<this.array.length) {
+ Boolean b = Boolean.valueOf(this.array[this.idx]);
+ ++this.idx;
+ return b;
+ }
+ throw new NoSuchElementException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int totalSize() {
+ return this.array==null ? 0 : this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ return this.idx - 1;
+ }
+
+ @Override
+ public int rest() {
+ return totalSize() - (index()+1);
+ }
+
+ } // class NativeBooleanToObjectBooleanIterator
+
+ /**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavenartifactid $ArtifactId$
+ * @mavengroupid $GroupId$
+ * @since SFC 4.1
+ */
+ private static class NativeCharacterToObjectCharacterIterator implements SizedIterator<Character> {
+
+ private final char[] array;
+ private int idx = 0;
+
+ /**
+ * @param data
+ */
+ public NativeCharacterToObjectCharacterIterator(char[] data) {
+ this.array = data;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return this.array!=null && this.idx>=0 && this.idx<this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Character next() {
+ if (this.array!=null && this.idx>=0 && this.idx<this.array.length) {
+ Character c = Character.valueOf(this.array[this.idx]);
+ ++this.idx;
+ return c;
+ }
+ throw new NoSuchElementException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int totalSize() {
+ return this.array==null ? 0 : this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ return this.idx - 1;
+ }
+
+ @Override
+ public int rest() {
+ return totalSize() - (index()+1);
+ }
+
+ } // class NativeCharacterToObjectCharacterIterator
+
+ /**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavenartifactid $ArtifactId$
+ * @mavengroupid $GroupId$
+ * @since SFC 4.1
+ */
+ private static class NativeByteToObjectByteIterator implements SizedIterator<Byte> {
+
+ private final byte[] array;
+ private int idx = 0;
+
+ /**
+ * @param data
+ */
+ public NativeByteToObjectByteIterator(byte[] data) {
+ this.array = data;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return this.array!=null && this.idx>=0 && this.idx<this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Byte next() {
+ if (this.array!=null && this.idx>=0 && this.idx<this.array.length) {
+ Byte b = Byte.valueOf(this.array[this.idx]);
+ ++this.idx;
+ return b;
+ }
+ throw new NoSuchElementException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int totalSize() {
+ return this.array==null ? 0 : this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ return this.idx - 1;
+ }
+
+ @Override
+ public int rest() {
+ return totalSize() - (index()+1);
+ }
+
+ } // class NativeByteToObjectByteIterator
+
+ /**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavenartifactid $ArtifactId$
+ * @mavengroupid $GroupId$
+ * @since SFC 4.1
+ */
+ private static class NativeShortToObjectShortIterator implements SizedIterator<Short> {
+
+ private final short[] array;
+ private int idx = 0;
+
+ /**
+ * @param data
+ */
+ public NativeShortToObjectShortIterator(short[] data) {
+ this.array = data;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return this.array!=null && this.idx>=0 && this.idx<this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Short next() {
+ if (this.array!=null && this.idx>=0 && this.idx<this.array.length) {
+ Short s = Short.valueOf(this.array[this.idx]);
+ ++this.idx;
+ return s;
+ }
+ throw new NoSuchElementException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int totalSize() {
+ return this.array==null ? 0 : this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ return this.idx - 1;
+ }
+
+ @Override
+ public int rest() {
+ return totalSize() - (index()+1);
+ }
+
+ } // class NativeShortToObjectShortIterator
+
+ /**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavenartifactid $ArtifactId$
+ * @mavengroupid $GroupId$
+ * @since SFC 4.1
+ */
+ private static class NativeIntegerToObjectIntegerIterator implements SizedIterator<Integer> {
+
+ private final int[] array;
+ private int idx = 0;
+
+ /**
+ * @param data
+ */
+ public NativeIntegerToObjectIntegerIterator(int[] data) {
+ this.array = data;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return this.array!=null && this.idx>=0 && this.idx<this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Integer next() {
+ if (this.array!=null && this.idx>=0 && this.idx<this.array.length) {
+ Integer f = Integer.valueOf(this.array[this.idx]);
+ ++this.idx;
+ return f;
+ }
+ throw new NoSuchElementException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int totalSize() {
+ return this.array==null ? 0 : this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ return this.idx - 1;
+ }
+
+ @Override
+ public int rest() {
+ return totalSize() - (index()+1);
+ }
+
+ } // class NativeIntegerToObjectIntegerIterator
+
+ /**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavenartifactid $ArtifactId$
+ * @mavengroupid $GroupId$
+ * @since SFC 4.1
+ */
+ private static class NativeLongToObjectLongIterator implements SizedIterator<Long> {
+
+ private final long[] array;
+ private int idx = 0;
+
+ /**
+ * @param data
+ */
+ public NativeLongToObjectLongIterator(long[] data) {
+ this.array = data;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return this.array!=null && this.idx>=0 && this.idx<this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Long next() {
+ if (this.array!=null && this.idx>=0 && this.idx<this.array.length) {
+ Long l = Long.valueOf(this.array[this.idx]);
+ ++this.idx;
+ return l;
+ }
+ throw new NoSuchElementException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int totalSize() {
+ return this.array==null ? 0 : this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ return this.idx - 1;
+ }
+
+ @Override
+ public int rest() {
+ return totalSize() - (index()+1);
+ }
+
+ } // class NativeLongToObjectLongIterator
+
+ /**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavenartifactid $ArtifactId$
+ * @mavengroupid $GroupId$
+ * @since SFC 4.1
+ */
+ private static class NativeFloatToObjectFloatIterator implements SizedIterator<Float> {
+
+ private final float[] array;
+ private int idx = 0;
+
+ /**
+ * @param data
+ */
+ public NativeFloatToObjectFloatIterator(float[] data) {
+ this.array = data;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return this.array!=null && this.idx>=0 && this.idx<this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Float next() {
+ if (this.array!=null && this.idx>=0 && this.idx<this.array.length) {
+ Float f = Float.valueOf(this.array[this.idx]);
+ ++this.idx;
+ return f;
+ }
+ throw new NoSuchElementException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int totalSize() {
+ return this.array==null ? 0 : this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ return this.idx - 1;
+ }
+
+ @Override
+ public int rest() {
+ return totalSize() - (index()+1);
+ }
+
+ } // class NativeFloatToObjectFloatIterator
+
+ /**
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavenartifactid $ArtifactId$
+ * @mavengroupid $GroupId$
+ * @since SFC 4.1
+ */
+ private static class NativeDoubleToObjectDoubleIterator implements SizedIterator<Double> {
+
+ private final double[] array;
+ private int idx = 0;
+
+ /**
+ * @param data
+ */
+ public NativeDoubleToObjectDoubleIterator(double[] data) {
+ this.array = data;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean hasNext() {
+ return this.array!=null && this.idx>=0 && this.idx<this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public Double next() {
+ if (this.array!=null && this.idx>=0 && this.idx<this.array.length) {
+ Double d = Double.valueOf(this.array[this.idx]);
+ ++this.idx;
+ return d;
+ }
+ throw new NoSuchElementException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void remove() {
+ throw new UnsupportedOperationException();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int totalSize() {
+ return this.array==null ? 0 : this.array.length;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int index() {
+ return this.idx - 1;
+ }
+
+ @Override
+ public int rest() {
+ return totalSize() - (index()+1);
+ }
+
+ } // class NativeDoubleToObjectDoubleIterator
+
+}
Modified: trunk/util/src/main/java/org/arakhne/afc/util/ListUtil.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/util/ListUtil.java 2013-03-21 14:28:10 UTC (rev 395)
+++ trunk/util/src/main/java/org/arakhne/afc/util/ListUtil.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -2,7 +2,7 @@
* $Id$
*
* Copyright (C) 2010-2011 Janus Core Developers
- * Copyright (C) 2012 Stephane GALLAND.
+ * Copyright (C) 2012-13 Stephane GALLAND.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@@ -44,8 +44,24 @@
* @param comparator is the comparator of elements.
* @param data is the data to remove.
* @return <code>true</code> if the data was removed, otherwise <code>false</code>
+ * @deprecated see {@link #remove(List, Comparator, Object)}
*/
+ @Deprecated
public static <E> boolean dichotomicRemove(List<E> list, Comparator<? super E> comparator, E data) {
+ return remove(list, comparator, data);
+ }
+
+ /** Remove the given element from the list using a dichotomic algorithm.
+ * <p>
+ * This function ensure that the comparator is invoked as: <code>comparator(data, dataAlreadyInList)</code>.
+ *
+ * @param <E> is the type of the elements in the list.
+ * @param list is the list to change.
+ * @param comparator is the comparator of elements.
+ * @param data is the data to remove.
+ * @return <code>true</code> if the data was removed, otherwise <code>false</code>
+ */
+ public static <E> boolean remove(List<E> list, Comparator<? super E> comparator, E data) {
assert(list!=null);
assert(comparator!=null);
assert(data!=null);
@@ -85,8 +101,30 @@
* @param allowMultipleOccurencesOfSameValue indicates if multiple
* occurrences of the same value are allowed in the list.
* @return <code>true</code> if the data was added, otherwise <code>false</code>
+ * @deprecated see {@link #add(List, Comparator, Object, boolean)}
*/
+ @Deprecated
public static <E> boolean dichotomicAdd(List<E> list, Comparator<? super E> comparator, E data, boolean allowMultipleOccurencesOfSameValue) {
+ return add(list, comparator, data, allowMultipleOccurencesOfSameValue, false);
+ }
+
+ /** Add the given element in the main list using a dichotomic algorithm.
+ * <p>
+ * This function ensure that the comparator is invoked as: <code>comparator(data, dataAlreadyInList)</code>.
+ * <p>
+ * If the data is al
+ *
+ * @param <E> is the type of the elements in the list.
+ * @param list is the list to change.
+ * @param comparator is the comparator of elements.
+ * @param data is the data to insert.
+ * @param allowMultipleOccurencesOfSameValue indicates if multiple
+ * occurrences of the same value are allowed in the list.
+ * @param allowReplacement indicates if the given <vaz>elt</var> may replace
+ * the found element.
+ * @return <code>true</code> if the data was added, otherwise <code>false</code>
+ */
+ public static <E> boolean add(List<E> list, Comparator<? super E> comparator, E data, boolean allowMultipleOccurencesOfSameValue, boolean allowReplacement) {
assert(list!=null);
assert(comparator!=null);
assert(data!=null);
@@ -120,8 +158,24 @@
* @param comparator is the comparator of elements.
* @param data is the data to search for.
* @return <code>true</code> if the data is inside the list, otherwise <code>false</code>
+ * @deprecated see {@link #contains(List, Comparator, Object)}
*/
+ @Deprecated
public static <E> boolean dichotomicContains(List<E> list, Comparator<? super E> comparator, E data) {
+ return contains(list, comparator, data);
+ }
+
+ /** Replies if the given element is inside the list, using a dichotomic algorithm.
+ * <p>
+ * This function ensure that the comparator is invoked as: <code>comparator(data, dataAlreadyInList)</code>.
+ *
+ * @param <E> is the type of the elements in the list.
+ * @param list is the list to explore.
+ * @param comparator is the comparator of elements.
+ * @param data is the data to search for.
+ * @return <code>true</code> if the data is inside the list, otherwise <code>false</code>
+ */
+ public static <E> boolean contains(List<E> list, Comparator<? super E> comparator, E data) {
assert(list!=null);
assert(comparator!=null);
assert(data!=null);
@@ -147,4 +201,387 @@
return false;
}
+ /** Replies the index of the given data in the given list according to a
+ * dichotomic search algorithm. Order between objects
+ * is given by <var>comparator</var>.
+ * <p>
+ * This function assumes that the given list is sorted
+ * according to the given comparator.
+ * A dichotomic algorithm is used.
+ *
+ * @param <T> is the type of the data to search for.
+ * @param list is the list inside which the element should be searched.
+ * @param comparator is the comparator used to sort the list.
+ * @param elt is the element to search for.
+ * @return the index at which the element is, or <code>-1</code> if
+ * the element was not found.
+ */
+ public static <T> int indexOf(List<T> list, Comparator<? super T> comparator, T elt) {
+ try {
+ assert(comparator!=null);
+ assert(list!=null);
+ if (elt==null) return -1;
+
+ int f = 0;
+ int l = list.size()-1;
+ int c, cmp;
+ T indata;
+
+ while (l>=f) {
+ c = (f+l)/2;
+ indata = list.get(c);
+ cmp = comparator.compare(elt, indata);
+ if (cmp==0) {
+ do {
+ --c;
+ }
+ while (c>=0 && comparator.compare(elt, list.get(c))==0);
+ return c+1;
+ }
+ else if (cmp<0) {
+ l = c-1;
+ }
+ else {
+ f = c+1;
+ }
+ }
+ }
+ catch(AssertionError e) {
+ throw e;
+ }
+ catch(Throwable _) {
+ //
+ }
+ return -1;
+ }
+
+ /** Replies the last index of the given data in the given list according to a
+ * dichotomic search algorithm. Order between objects
+ * is given by <var>comparator</var>.
+ * <p>
+ * This function assumes that the given list is sorted
+ * according to the given comparator.
+ * A dichotomic algorithm is used.
+ *
+ * @param <T> is the type of the data to search for.
+ * @param list is the list inside which the element should be searched.
+ * @param comparator is the comparator used to sort the list.
+ * @param elt is the element to search for.
+ * @return the last index at which the element is, or <code>-1</code> if
+ * the element was not found.
+ */
+ public static <T> int lastIndexOf(List<T> list, Comparator<? super T> comparator, T elt) {
+ try {
+ assert(comparator!=null);
+ assert(list!=null);
+ if (elt==null) return -1;
+
+ int f = 0;
+ int l = list.size()-1;
+ int c, cmp;
+ T indata;
+
+ while (l>=f) {
+ c = (f+l)/2;
+ indata = list.get(c);
+ cmp = comparator.compare(elt, indata);
+ if (cmp==0) {
+ do {
+ ++c;
+ }
+ while (c<list.size() && comparator.compare(elt, list.get(c))==0);
+ return c-1;
+ }
+ else if (cmp<0) {
+ l = c-1;
+ }
+ else {
+ f = c+1;
+ }
+ }
+ }
+ catch(AssertionError e) {
+ throw e;
+ }
+ catch(Throwable _) {
+ //
+ }
+ return -1;
+ }
+
+ /** Replies the index at which the given element may
+ * be added in a sorted list.
+ * <p>
+ * This function assumes that the given list is sorted
+ * according to the given comparator.
+ * A dichotomic algorithm is used.
+ * <p>
+ * This function assumes that the given <var>elt</var>
+ * may appear many times in the list.
+ *
+ * @param <T> is the type of the elements.
+ * @param comparator is the comparator used to sort the list.
+ * @param elt is the element to add in.
+ * @param list is the list inside which the element should be added.
+ * @return the index at which the element may be added.
+ */
+ public static <T> int getInsertionIndex(List<T> list, Comparator<? super T> comparator, T elt) {
+ return getInsertionIndex(list, comparator, elt, true);
+ }
+
+ /** Replies the index at which the given element may
+ * be added in a sorted list.
+ * <p>
+ * This function assumes that the given list is sorted
+ * according to the given comparator.
+ * A dichotomic algorithm is used.
+ * <p>
+ * This function assumes that the given <var>elt</var>
+ * may appear many times in the list.
+ *
+ * @param <T> is the type of the elements.
+ * @param comparator is the comparator used to sort the list.
+ * @param elt is the element to add in.
+ * @param list is the list inside which the element should be added.
+ * @param allowMultiple indicates if the given <var>elt</var> may appear
+ * many times in the list, or not.
+ * @return the index at which the element may be added.
+ */
+ public static <T> int getInsertionIndex(List<T> list, Comparator<? super T> comparator, T elt, boolean allowMultiple) {
+ try {
+ assert(comparator!=null);
+ assert(list!=null);
+ if (elt==null) return -1;
+
+ int f = 0;
+ int l = list.size()-1;
+ int c, comparison;
+ T indata;
+ while (l>=f) {
+ c = (f+l)/2;
+ indata = list.get(c);
+ comparison = comparator.compare(elt, indata);
+ if (!allowMultiple && comparison==0) return -1;
+ if (comparison<0) {
+ l = c-1;
+ }
+ else {
+ f = c+1;
+ }
+ }
+ return f;
+ }
+ catch(AssertionError e) {
+ throw e;
+ }
+ catch(Throwable _) {
+ return -1;
+ }
+ }
+
+ /**
+ * Returns the index of the leastest element in this list greater than or equal to
+ * the given element, or <code>-1</code> if there is no such element.
+ * <p>
+ * This function assumes that the given list is sorted
+ * according to the given comparator.
+ * A dichotomic algorithm is used.
+ *
+ * @param <T> is the type of the data to search for.
+ * @param list is the list inside which the element should be searched.
+ * @param comparator is the comparator used to sort the list.
+ * @param elt is the value to match.
+ * @return the index of leastest element greater than or equal to <var>elt</var>, or
+ * <code>-1</code> if there is no such element.
+ * @see NavigableSet#ceiling(Object)
+ */
+ public static <T> int ceilingIndex(List<T> list, Comparator<? super T> comparator, T elt) {
+ try {
+ assert(comparator!=null);
+ assert(list!=null);
+ if (elt==null) return -1;
+ int f = 0;
+ int l = list.size()-1;
+ int c, cmp;
+ T indata;
+
+ while (l>=f) {
+ c = (f+l)/2;
+ indata = list.get(c);
+ cmp = comparator.compare(elt, indata);
+ if (cmp==0) {
+ do {
+ --c;
+ }
+ while (c>=0 && comparator.compare(elt, list.get(c))==0);
+ return c+1;
+ }
+ else if (cmp<0) {
+ l = c-1;
+ }
+ else {
+ f = c+1;
+ }
+ }
+ if (f>=list.size()) f = -1;
+ return f;
+ }
+ catch(AssertionError e) {
+ throw e;
+ }
+ catch(Throwable _) {
+ return -1;
+ }
+ }
+
+ /**
+ * Returns the index of the greatest element in this list less than or equal to
+ * the given element, or <code>-1</code> if there is no such element.
+ * <p>
+ * This function assumes that the given list is sorted
+ * according to the given comparator.
+ * A dichotomic algorithm is used.
+ *
+ * @param <T> is the type of the data to search for.
+ * @param list is the list inside which the element should be searched.
+ * @param comparator is the comparator used to sort the list.
+ * @param elt is the value to match.
+ * @return the index of greatest element less than or equal to <var>elt</var>, or
+ * <code>-1</code> if there is no such element.
+ * @see NavigableSet#floor(Object)
+ */
+ public static <T> int floorIndex(List<T> list, Comparator<? super T> comparator, T elt) {
+ try {
+ assert(comparator!=null);
+ assert(list!=null);
+ if (elt==null) return -1;
+ int f = 0;
+ int l = list.size()-1;
+ int c, cmp;
+ T indata;
+
+ while (l>=f) {
+ c = (f+l)/2;
+ indata = list.get(c);
+ cmp = comparator.compare(elt, indata);
+ if (cmp==0) {
+ do {
+ ++c;
+ }
+ while (c<list.size() && comparator.compare(elt, list.get(c))==0);
+ return c-1;
+ }
+ else if (cmp<0) {
+ l = c-1;
+ }
+ else {
+ f = c+1;
+ }
+ }
+ return l;
+ }
+ catch(AssertionError e) {
+ throw e;
+ }
+ catch(Throwable _) {
+ return -1;
+ }
+ }
+
+ /**
+ * Returns the index of the least element in this list strictly greater
+ * than the given element, or <code>-1</code> if there is no such element.
+ * <p>
+ * This function assumes that the given list is sorted
+ * according to the given comparator.
+ * A dichotomic algorithm is used.
+ *
+ * @param <T> is the type of the data to search for.
+ * @param list is the list inside which the element should be searched.
+ * @param comparator is the comparator used to sort the list.
+ * @param elt is the value to match.
+ * @return the index of least element strictly greater than to <var>elt</var>, or
+ * <code>-1</code> if there is no such element.
+ * @see NavigableSet#higher(Object)
+ */
+ public static <T> int higherIndex(List<T> list, Comparator<? super T> comparator, T elt) {
+ try {
+ assert(comparator!=null);
+ assert(list!=null);
+ if (elt==null) return -1;
+ int f = 0;
+ int l = list.size()-1;
+ int c, cmp;
+ T indata;
+
+ while (l>=f) {
+ c = (f+l)/2;
+ indata = list.get(c);
+ cmp = comparator.compare(elt, indata);
+ if (cmp<0) {
+ l = c-1;
+ }
+ else {
+ f = c+1;
+ }
+ }
+ ++l;
+ if (l>=list.size()) l = -1;
+ return l;
+ }
+ catch(AssertionError e) {
+ throw e;
+ }
+ catch(Throwable _) {
+ return -1;
+ }
+ }
+
+ /**
+ * Returns the index of the greatest element in this list strictly lower
+ * than the given element, or <code>-1</code> if there is no such element.
+ * <p>
+ * This function assumes that the given list is sorted
+ * according to the given comparator.
+ * A dichotomic algorithm is used.
+ *
+ * @param <T> is the type of the data to search for.
+ * @param list is the list inside which the element should be searched.
+ * @param comparator is the comparator used to sort the list.
+ * @param elt is the value to match.
+ * @return the index of greater element strictly lower than to <var>elt</var>, or
+ * <code>-1</code> if there is no such element.
+ * @see NavigableSet#lower(Object)
+ */
+ public static <T> int lowerIndex(List<T> list, Comparator<? super T> comparator, T elt) {
+ try {
+ assert(comparator!=null);
+ assert(list!=null);
+ if (elt==null) return -1;
+ int f = 0;
+ int l = list.size()-1;
+ int c, cmp;
+ T indata;
+
+ while (l>=f) {
+ c = (f+l)/2;
+ indata = list.get(c);
+ cmp = comparator.compare(elt, indata);
+ if (cmp<=0) {
+ l = c-1;
+ }
+ else {
+ f = c+1;
+ }
+ }
+ return l;
+ }
+ catch(AssertionError e) {
+ throw e;
+ }
+ catch(Throwable _) {
+ return -1;
+ }
+ }
+
}
Added: trunk/util/src/main/java/org/arakhne/afc/util/NaturalOrderComparator.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/util/NaturalOrderComparator.java (rev 0)
+++ trunk/util/src/main/java/org/arakhne/afc/util/NaturalOrderComparator.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -0,0 +1,61 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2010-2011 Janus Core Developers
+ * Copyright (C) 2012-13 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.afc.util;
+
+import java.util.Comparator;
+
+/** Comparator which is using the natural order of the elements, i.e.
+ * the elements must implements {@link Comparable}
+ *
+ * @param <OBJ> is the type of the objects to compare to.
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class NaturalOrderComparator<OBJ> implements Comparator<OBJ> {
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ @SuppressWarnings({ "unchecked", "rawtypes" })
+ public int compare(OBJ o1, OBJ o2) {
+ if (o1==o2) return 0;
+ if (o1==null) return Integer.MIN_VALUE;
+ if (o2==null) return Integer.MAX_VALUE;
+ try {
+ return ((Comparable)o1).compareTo(o2);
+ }
+ catch(Throwable _) {
+ //
+ }
+ try {
+ return -((Comparable)o2).compareTo(o1);
+ }
+ catch(Throwable _) {
+ //
+ }
+ throw new UnsupportedNaturalOrderException(o1);
+ }
+
+}
Added: trunk/util/src/main/java/org/arakhne/afc/util/OutputParameter.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/util/OutputParameter.java (rev 0)
+++ trunk/util/src/main/java/org/arakhne/afc/util/OutputParameter.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -0,0 +1,154 @@
+/*
+ * $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.afc.util;
+
+/** Utilities class that permits to obtain output parameters for Java functions.
+ * <p>
+ * The generic type describes the type of the parameter.
+ * <p>
+ * Example of the classic swap function:
+ * <pre><code>
+ * class SwapTest {
+ * public static void swap(OutputParameter<Long> a, OutputParameter<Long> b) {
+ * Long tmp = a.get();
+ * a.set(b.get();
+ * b.set(tmp);
+ * }
+ *
+ * public static void main(String[]) {
+ * OutputParameter<Long> opx = new OuputParameter(24);
+ * OutputParameter<Long> opy = new OuputParameter(36);
+ * System.out.println("before, x="+opx.get());
+ * System.out.println("before, y="+opy.get());
+ * swap(opx,opy);
+ * System.out.println("after, x="+opx.get());
+ * System.out.println("after, y="+opy.get());
+ * }
+ * }
+ * </code></pre>
+ * <p>
+ * The example outputs the text:
+ * <pre><code>
+ * before, x=24
+ * before, y=36
+ * after, x=36
+ * after, y=24
+ * </code></pre>
+ *
+ * @param <T> is the type of the value to output with this object.
+ * @author $Author: sgalland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class OutputParameter<T> {
+
+ private T object;
+
+ /**
+ * @param initialValue is the initial value of the output parameter.
+ */
+ public OutputParameter(T initialValue) {
+ this.object = initialValue;
+ }
+
+ /**
+ * Create empty output parameter.
+ */
+ public OutputParameter() {
+ this.object = null;
+ }
+
+ /**
+ * {@inheritDoc}
+ *
+ * @return {@inheritDoc}
+ */
+ @Override
+ public String toString() {
+ StringBuilder str = new StringBuilder();
+ str.append("output:"); //$NON-NLS-1$
+ str.append((this.object==null) ? this.object : this.object.toString());
+ return str.toString();
+ }
+
+ /** Replies the parameter value.
+ *
+ * @return the value embedded inside this output parameter.
+ */
+ public T get() {
+ return this.object;
+ }
+
+ /** Set the parameter.
+ *
+ * @param newValue is the value of the parameter.
+ * @return the old value of the parameter.
+ */
+ public T set(T newValue) {
+ T obj = this.object;
+ this.object = newValue;
+ return obj;
+ }
+
+ /** Clear this parameter.
+ */
+ public void clear() {
+ this.object = null;
+ }
+
+ /** Replies if the value was set, i.e. not <code>null</code>.
+ *
+ * @return <code>true</code> is the parameter vlaue was set, otherwise <code>false</code>
+ * @see #isEmpty()
+ */
+ public boolean isSet() {
+ return this.object != null;
+ }
+
+ /** Replies if the value was not set, i.e. <code>null</code>.
+ *
+ * @return <code>false</code> is the parameter vlaue was set, otherwise <code>true</code>
+ * @see #isSet()
+ */
+ public boolean isEmpty() {
+ return this.object == null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public boolean equals(Object o) {
+ return (this.object==o)
+ ||
+ (this.object!=null && this.object.equals(o));
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public int hashCode() {
+ return this.object==null ? 0 : HashCodeUtil.hash(this.object);
+ }
+
+}
Added: trunk/util/src/main/java/org/arakhne/afc/util/UnsupportedNaturalOrderException.java
===================================================================
--- trunk/util/src/main/java/org/arakhne/afc/util/UnsupportedNaturalOrderException.java (rev 0)
+++ trunk/util/src/main/java/org/arakhne/afc/util/UnsupportedNaturalOrderException.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -0,0 +1,54 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2010-2011 Janus Core Developers
+ * Copyright (C) 2012-13 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.afc.util;
+
+/** Exception thrown when an object does not support any
+ * natural order.
+ *
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class UnsupportedNaturalOrderException extends RuntimeException {
+
+ private static final long serialVersionUID = 6506767532507115987L;
+
+ private final Object element;
+
+ /**
+ * @param element is the element which does not provide a natural order.
+ */
+ public UnsupportedNaturalOrderException(Object element) {
+ super();
+ this.element = element;
+ }
+
+ /** Replies the element which does not provide a natural order.
+ *
+ * @return the element which does not provide a natural order.
+ */
+ public Object getElement() {
+ return this.element;
+ }
+
+}
Added: trunk/util/src/test/java/org/arakhne/afc/sizediterator/CollectionSizedIteratorTest.java
===================================================================
--- trunk/util/src/test/java/org/arakhne/afc/sizediterator/CollectionSizedIteratorTest.java (rev 0)
+++ trunk/util/src/test/java/org/arakhne/afc/sizediterator/CollectionSizedIteratorTest.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -0,0 +1,166 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2010-2011 Janus Core Developers
+ * Copyright (C) 2012-13 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.afc.sizediterator;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.NoSuchElementException;
+
+import junit.framework.TestCase;
+
+/**
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class CollectionSizedIteratorTest extends TestCase {
+
+ private String s1, s2, s3, s4, s5;
+ private Collection<String> collection;
+ private CollectionSizedIterator<String> iterator;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ this.collection = new ArrayList<String>();
+ this.collection.add(this.s1 = "s1"); //$NON-NLS-1$
+ this.collection.add(this.s2 = "s2"); //$NON-NLS-1$
+ this.collection.add(this.s3 = "s3"); //$NON-NLS-1$
+ this.collection.add(this.s4 = "s4"); //$NON-NLS-1$
+ this.collection.add(this.s5 = "s5"); //$NON-NLS-1$
+ this.iterator = new CollectionSizedIterator<String>(this.collection);
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ this.iterator = null;
+ this.collection = null;
+ this.s1 = this.s2 = this.s3 = this.s4 = this.s5 = null;
+ super.tearDown();
+ }
+
+ /**
+ */
+ public void testHasNext() {
+ assertTrue(this.iterator.hasNext());
+ this.iterator.next();
+ assertTrue(this.iterator.hasNext());
+ this.iterator.next();
+ assertTrue(this.iterator.hasNext());
+ this.iterator.next();
+ assertTrue(this.iterator.hasNext());
+ this.iterator.next();
+ assertTrue(this.iterator.hasNext());
+ this.iterator.next();
+ assertFalse(this.iterator.hasNext());
+ }
+
+ /**
+ */
+ public void testNext() {
+ assertSame(this.s1, this.iterator.next());
+ assertSame(this.s2, this.iterator.next());
+ assertSame(this.s3, this.iterator.next());
+ assertSame(this.s4, this.iterator.next());
+ assertSame(this.s5, this.iterator.next());
+ try {
+ this.iterator.next();
+ fail("expecting NoSuchElementException"); //$NON-NLS-1$
+ }
+ catch(NoSuchElementException _) {
+ // Expected exception
+ }
+ }
+
+ /**
+ */
+ public void remove() {
+ assertSame(this.s1, this.iterator.next());
+ assertSame(this.s2, this.iterator.next());
+ this.iterator.remove();
+ assertSame(this.s3, this.iterator.next());
+ assertSame(this.s4, this.iterator.next());
+ this.iterator.remove();
+ assertSame(this.s5, this.iterator.next());
+
+ assertEquals(3, this.collection.size());
+ assertTrue(this.collection.contains(this.s1));
+ assertFalse(this.collection.contains(this.s2));
+ assertTrue(this.collection.contains(this.s3));
+ assertFalse(this.collection.contains(this.s4));
+ assertTrue(this.collection.contains(this.s5));
+ }
+
+ /**
+ */
+ public void testIndex() {
+ assertEquals(-1, this.iterator.index());
+ assertSame(this.s1, this.iterator.next());
+ assertEquals(0, this.iterator.index());
+ assertSame(this.s2, this.iterator.next());
+ assertEquals(1, this.iterator.index());
+ assertSame(this.s3, this.iterator.next());
+ assertEquals(2, this.iterator.index());
+ assertSame(this.s4, this.iterator.next());
+ assertEquals(3, this.iterator.index());
+ assertSame(this.s5, this.iterator.next());
+ assertEquals(4, this.iterator.index());
+ }
+
+ /**
+ */
+ public void testRest() {
+ assertEquals(5, this.iterator.rest());
+ assertSame(this.s1, this.iterator.next());
+ assertEquals(4, this.iterator.rest());
+ assertSame(this.s2, this.iterator.next());
+ assertEquals(3, this.iterator.rest());
+ assertSame(this.s3, this.iterator.next());
+ assertEquals(2, this.iterator.rest());
+ assertSame(this.s4, this.iterator.next());
+ assertEquals(1, this.iterator.rest());
+ assertSame(this.s5, this.iterator.next());
+ assertEquals(0, this.iterator.rest());
+ }
+
+ /**
+ */
+ public void testTotalSize() {
+ assertEquals(5, this.iterator.totalSize());
+ assertSame(this.s1, this.iterator.next());
+ assertEquals(5, this.iterator.totalSize());
+ assertSame(this.s2, this.iterator.next());
+ assertEquals(5, this.iterator.totalSize());
+ this.iterator.remove();
+ assertEquals(4, this.iterator.totalSize());
+ assertSame(this.s3, this.iterator.next());
+ assertEquals(4, this.iterator.totalSize());
+ assertSame(this.s4, this.iterator.next());
+ assertEquals(4, this.iterator.totalSize());
+ this.iterator.remove();
+ assertEquals(3, this.iterator.totalSize());
+ assertSame(this.s5, this.iterator.next());
+ assertEquals(3, this.iterator.totalSize());
+ }
+
+}
Added: trunk/util/src/test/java/org/arakhne/afc/sizediterator/SingleIteratorTest.java
===================================================================
--- trunk/util/src/test/java/org/arakhne/afc/sizediterator/SingleIteratorTest.java (rev 0)
+++ trunk/util/src/test/java/org/arakhne/afc/sizediterator/SingleIteratorTest.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -0,0 +1,111 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2010-2011 Janus Core Developers
+ * Copyright (C) 2012-13 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.afc.sizediterator;
+
+import java.util.NoSuchElementException;
+import java.util.UUID;
+
+import junit.framework.TestCase;
+
+/**
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class SingleIteratorTest extends TestCase {
+
+ private String s1;
+ private SingleIterator<String> iterator;
+
+ @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ this.s1 = UUID.randomUUID().toString();
+ this.iterator = new SingleIterator<String>(this.s1);
+ }
+
+ @Override
+ public void tearDown() throws Exception {
+ this.iterator = null;
+ this.s1 = null;
+ super.tearDown();
+ }
+
+ /**
+ */
+ public void testHasNext() {
+ assertTrue(this.iterator.hasNext());
+ this.iterator.next();
+ assertFalse(this.iterator.hasNext());
+ }
+
+ /**
+ */
+ public void testNext() {
+ assertSame(this.s1, this.iterator.next());
+ try {
+ this.iterator.next();
+ fail("expecting NoSuchElementException"); //$NON-NLS-1$
+ }
+ catch(NoSuchElementException _) {
+ // expected exception
+ }
+ }
+
+ /**
+ */
+ public void testRemove() {
+ try {
+ this.iterator.remove();
+ fail("expecting UnsupportedOperationException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedOperationException _) {
+ // exepcted exception
+ }
+ }
+
+ /**
+ */
+ public void testIndex() {
+ assertEquals(-1, this.iterator.index());
+ this.iterator.next();
+ assertEquals(0, this.iterator.index());
+ }
+
+ /**
+ */
+ public void testRest() {
+ assertEquals(1, this.iterator.rest());
+ this.iterator.next();
+ assertEquals(0, this.iterator.rest());
+ }
+
+ /**
+ */
+ public void testTotalSize() {
+ assertEquals(1, this.iterator.totalSize());
+ this.iterator.next();
+ assertEquals(1, this.iterator.totalSize());
+ }
+
+}
Added: trunk/util/src/test/java/org/arakhne/afc/util/ListUtilTest.java
===================================================================
--- trunk/util/src/test/java/org/arakhne/afc/util/ListUtilTest.java (rev 0)
+++ trunk/util/src/test/java/org/arakhne/afc/util/ListUtilTest.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -0,0 +1,350 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2010-2011 Janus Core Developers
+ * Copyright (C) 2012-13 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.afc.util;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+
+import junit.framework.TestCase;
+
+/**
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class ListUtilTest extends TestCase {
+
+ /**
+ */
+ public static void testContainsComparatorTList() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 12, 30));
+
+ assertFalse(ListUtil.contains(list, new NaturalOrderComparator<Integer>(), 0));
+ assertTrue(ListUtil.contains(list, new NaturalOrderComparator<Integer>(), 1));
+ assertFalse(ListUtil.contains(list, new NaturalOrderComparator<Integer>(), 2));
+ assertFalse(ListUtil.contains(list, new NaturalOrderComparator<Integer>(), 3));
+ assertTrue(ListUtil.contains(list, new NaturalOrderComparator<Integer>(), 4));
+ assertFalse(ListUtil.contains(list, new NaturalOrderComparator<Integer>(), 5));
+ assertTrue(ListUtil.contains(list, new NaturalOrderComparator<Integer>(), 6));
+ }
+
+ /**
+ */
+ public static void testAddComparatorTList() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 12, 30));
+
+ assertEquals(0, ListUtil.add(list, new NaturalOrderComparator<Integer>(), -2, false, false));
+ assertEquals(3, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 4, false, false));
+ assertEquals(6, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 16, false, false));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(-2), iterator.next());
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(16), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testAddComparatorTListBoolean_true() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 12, 30));
+
+ assertEquals(0, ListUtil.add(list, new NaturalOrderComparator<Integer>(), -2, true, false));
+ assertEquals(3, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 4, true, false));
+ assertEquals(6, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 16, true, false));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(-2), iterator.next());
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(16), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testAddComparatorTListBoolean_false() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 12, 30));
+
+ assertEquals(0, ListUtil.add(list, new NaturalOrderComparator<Integer>(), -2, false, false));
+ assertEquals(-1, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 4, false, false));
+ assertEquals(5, ListUtil.add(list, new NaturalOrderComparator<Integer>(), 16, false, false));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(-2), iterator.next());
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(16), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testGetInsertionIndexComparatorTList() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 12, 30));
+
+ assertEquals(0, ListUtil.getInsertionIndex(list, new NaturalOrderComparator<Integer>(), -2));
+ assertEquals(2, ListUtil.getInsertionIndex(list, new NaturalOrderComparator<Integer>(), 4));
+ assertEquals(4, ListUtil.getInsertionIndex(list, new NaturalOrderComparator<Integer>(), 16));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testGetInsertionIndexComparatorTListBoolean_true() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 12, 30));
+
+ assertEquals(0, ListUtil.getInsertionIndex(list, new NaturalOrderComparator<Integer>(), -2, true));
+ assertEquals(2, ListUtil.getInsertionIndex(list, new NaturalOrderComparator<Integer>(), 4, true));
+ assertEquals(4, ListUtil.getInsertionIndex(list, new NaturalOrderComparator<Integer>(), 16, true));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testGetInsertionIndexComparatorTListBoolean_false() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 12, 30));
+
+ assertEquals(0, ListUtil.getInsertionIndex(list, new NaturalOrderComparator<Integer>(), -2, false));
+ assertEquals(-1, ListUtil.getInsertionIndex(list, new NaturalOrderComparator<Integer>(), 4, false));
+ assertEquals(4, ListUtil.getInsertionIndex(list, new NaturalOrderComparator<Integer>(), 16, false));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testRemoveComparatorTList() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 12, 30));
+
+ assertEquals(-1, ListUtil.remove(list, new NaturalOrderComparator<Integer>(), -2));
+ assertEquals(-1, ListUtil.remove(list, new NaturalOrderComparator<Integer>(), 34));
+ assertEquals(2, ListUtil.remove(list, new NaturalOrderComparator<Integer>(), 6));
+ assertEquals(1, ListUtil.remove(list, new NaturalOrderComparator<Integer>(), 4));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testIndexOfComparatorTList() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 6, 6, 12, 30));
+
+ assertEquals(-1, ListUtil.indexOf(list, new NaturalOrderComparator<Integer>(), -2));
+ assertEquals(-1, ListUtil.indexOf(list, new NaturalOrderComparator<Integer>(), 34));
+ assertEquals(-1, ListUtil.indexOf(list, new NaturalOrderComparator<Integer>(), 7));
+ assertEquals(0, ListUtil.indexOf(list, new NaturalOrderComparator<Integer>(), 1));
+ assertEquals(1, ListUtil.indexOf(list, new NaturalOrderComparator<Integer>(), 4));
+ assertEquals(2, ListUtil.indexOf(list, new NaturalOrderComparator<Integer>(), 6));
+ assertEquals(5, ListUtil.indexOf(list, new NaturalOrderComparator<Integer>(), 12));
+ assertEquals(6, ListUtil.indexOf(list, new NaturalOrderComparator<Integer>(), 30));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testLastIndexOfComparatorTList() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 6, 6, 12, 30));
+
+ assertEquals(-1, ListUtil.lastIndexOf(list, new NaturalOrderComparator<Integer>(), -2));
+ assertEquals(-1, ListUtil.lastIndexOf(list, new NaturalOrderComparator<Integer>(), 34));
+ assertEquals(-1, ListUtil.lastIndexOf(list, new NaturalOrderComparator<Integer>(), 7));
+ assertEquals(0, ListUtil.lastIndexOf(list, new NaturalOrderComparator<Integer>(), 1));
+ assertEquals(1, ListUtil.lastIndexOf(list, new NaturalOrderComparator<Integer>(), 4));
+ assertEquals(4, ListUtil.lastIndexOf(list, new NaturalOrderComparator<Integer>(), 6));
+ assertEquals(5, ListUtil.lastIndexOf(list, new NaturalOrderComparator<Integer>(), 12));
+ assertEquals(6, ListUtil.lastIndexOf(list, new NaturalOrderComparator<Integer>(), 30));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testFloorIndexComparatorTList() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 6, 6, 12, 30));
+
+ assertEquals(-1, ListUtil.floorIndex(list, new NaturalOrderComparator<Integer>(), -2));
+ assertEquals(4, ListUtil.floorIndex(list, new NaturalOrderComparator<Integer>(), 7));
+ assertEquals(6, ListUtil.floorIndex(list, new NaturalOrderComparator<Integer>(), 34));
+ assertEquals(0, ListUtil.floorIndex(list, new NaturalOrderComparator<Integer>(), 1));
+ assertEquals(1, ListUtil.floorIndex(list, new NaturalOrderComparator<Integer>(), 4));
+ assertEquals(4, ListUtil.floorIndex(list, new NaturalOrderComparator<Integer>(), 6));
+ assertEquals(5, ListUtil.floorIndex(list, new NaturalOrderComparator<Integer>(), 12));
+ assertEquals(6, ListUtil.floorIndex(list, new NaturalOrderComparator<Integer>(), 30));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testHigherIndexComparatorTList() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 6, 6, 12, 30));
+
+ assertEquals(0, ListUtil.higherIndex(list, new NaturalOrderComparator<Integer>(), -2));
+ assertEquals(5, ListUtil.higherIndex(list, new NaturalOrderComparator<Integer>(), 7));
+ assertEquals(-1, ListUtil.higherIndex(list, new NaturalOrderComparator<Integer>(), 34));
+ assertEquals(1, ListUtil.higherIndex(list, new NaturalOrderComparator<Integer>(), 1));
+ assertEquals(2, ListUtil.higherIndex(list, new NaturalOrderComparator<Integer>(), 4));
+ assertEquals(5, ListUtil.higherIndex(list, new NaturalOrderComparator<Integer>(), 6));
+ assertEquals(6, ListUtil.higherIndex(list, new NaturalOrderComparator<Integer>(), 12));
+ assertEquals(-1, ListUtil.higherIndex(list, new NaturalOrderComparator<Integer>(), 30));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testLowerIndexComparatorTList() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 6, 6, 12, 30));
+
+ assertEquals(-1, ListUtil.lowerIndex(list, new NaturalOrderComparator<Integer>(), -2));
+ assertEquals(4, ListUtil.lowerIndex(list, new NaturalOrderComparator<Integer>(), 7));
+ assertEquals(6, ListUtil.lowerIndex(list, new NaturalOrderComparator<Integer>(), 34));
+ assertEquals(-1, ListUtil.lowerIndex(list, new NaturalOrderComparator<Integer>(), 1));
+ assertEquals(0, ListUtil.lowerIndex(list, new NaturalOrderComparator<Integer>(), 4));
+ assertEquals(1, ListUtil.lowerIndex(list, new NaturalOrderComparator<Integer>(), 6));
+ assertEquals(4, ListUtil.lowerIndex(list, new NaturalOrderComparator<Integer>(), 12));
+ assertEquals(5, ListUtil.lowerIndex(list, new NaturalOrderComparator<Integer>(), 30));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+ /**
+ */
+ public static void testCeilingIndexComparatorTList() {
+ List<Integer> list = new ArrayList<Integer>();
+ list.addAll(Arrays.asList(1, 4, 6, 6, 6, 12, 30));
+
+ assertEquals(0, ListUtil.ceilingIndex(list, new NaturalOrderComparator<Integer>(), -2));
+ assertEquals(5, ListUtil.ceilingIndex(list, new NaturalOrderComparator<Integer>(), 7));
+ assertEquals(-1, ListUtil.ceilingIndex(list, new NaturalOrderComparator<Integer>(), 34));
+ assertEquals(0, ListUtil.ceilingIndex(list, new NaturalOrderComparator<Integer>(), 1));
+ assertEquals(1, ListUtil.ceilingIndex(list, new NaturalOrderComparator<Integer>(), 4));
+ assertEquals(2, ListUtil.ceilingIndex(list, new NaturalOrderComparator<Integer>(), 6));
+ assertEquals(5, ListUtil.ceilingIndex(list, new NaturalOrderComparator<Integer>(), 12));
+ assertEquals(6, ListUtil.ceilingIndex(list, new NaturalOrderComparator<Integer>(), 30));
+
+ Iterator<Integer> iterator = list.iterator();
+ assertEquals(Integer.valueOf(1), iterator.next());
+ assertEquals(Integer.valueOf(4), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(6), iterator.next());
+ assertEquals(Integer.valueOf(12), iterator.next());
+ assertEquals(Integer.valueOf(30), iterator.next());
+ assertFalse(iterator.hasNext());
+ }
+
+}
Added: trunk/util/src/test/java/org/arakhne/afc/util/NaturalOrderComparatorTest.java
===================================================================
--- trunk/util/src/test/java/org/arakhne/afc/util/NaturalOrderComparatorTest.java (rev 0)
+++ trunk/util/src/test/java/org/arakhne/afc/util/NaturalOrderComparatorTest.java 2013-03-21 16:23:02 UTC (rev 396)
@@ -0,0 +1,184 @@
+/*
+ * $Id$
+ *
+ * Copyright (C) 2010-2011 Janus Core Developers
+ * Copyright (C) 2012-13 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.afc.util;
+
+import junit.framework.TestCase;
+
+/**
+ * @author $Author: galland$
+ * @version $FullVersion$
+ * @mavengroupid $GroupId$
+ * @mavenartifactid $ArtifactId$
+ */
+public class NaturalOrderComparatorTest extends TestCase {
+
+
+ /**
+ */
+ public static void testCompare() {
+ NaturalOrderComparator<Object> comparator = new NaturalOrderComparator<Object>();
+
+ Integer o1 = 1;
+ Integer o2 = 2;
+ Integer o3 = 3;
+ Integer o4 = 10;
+ Float v1 = 3f;
+ Double v2 = 3.;
+ String v3 = "3"; //$NON-NLS-1$
+ Object v4 = new Object();
+
+ assertEquals(0, comparator.compare(o1, o1));
+ assertEquals(-1, comparator.compare(o1, o2));
+ assertEquals(-1, comparator.compare(o1, o3));
+ assertEquals(-1, comparator.compare(o1, o4));
+ try {
+ comparator.compare(o1, v1);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o1, v2);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o1, v3);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o1, v4);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+
+ assertEquals(1, comparator.compare(o2, o1));
+ assertEquals(0, comparator.compare(o2, o2));
+ assertEquals(-1, comparator.compare(o2, o3));
+ assertEquals(-1, comparator.compare(o2, o4));
+ try {
+ comparator.compare(o2, v1);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o2, v2);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o2, v3);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o2, v4);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+
+
+ assertEquals(1, comparator.compare(o3, o1));
+ assertEquals(1, comparator.compare(o3, o2));
+ assertEquals(0, comparator.compare(o3, o3));
+ assertEquals(-1, comparator.compare(o3, o4));
+ try {
+ comparator.compare(o3, v1);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o3, v2);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o3, v3);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o3, v4);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+
+
+ assertEquals(1, comparator.compare(o4, o1));
+ assertEquals(1, comparator.compare(o4, o2));
+ assertEquals(1, comparator.compare(o4, o3));
+ assertEquals(0, comparator.compare(o4, o4));
+ try {
+ comparator.compare(o4, v1);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o4, v2);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o4, v3);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ try {
+ comparator.compare(o4, v4);
+ fail("Expecting UnsupportedNaturalOrderException"); //$NON-NLS-1$
+ }
+ catch(UnsupportedNaturalOrderException _) {
+ // expected exception
+ }
+ }
+
+}