@@ -0,0 +1,2544 @@
+//%0A// SortByPriority.java%0A//%0A%0A/*%0AImageJ software for multidimensional image processing and analysis.%0A%0ACopyright (c) 2010, ImageJDev.org.%0AAll rights reserved.%0A%0ARedistribution and use in source and binary forms, with or without%0Amodification, are permitted provided that the following conditions are met:%0A    * Redistributions of source code must retain the above copyright%0A      notice, this list of conditions and the following disclaimer.%0A    * Redistributions in binary form must reproduce the above copyright%0A      notice, this list of conditions and the following disclaimer in the%0A      documentation and/or other materials provided with the distribution.%0A    * Neither the names of the ImageJDev.org developers nor the%0A      names of its contributors may be used to endorse or promote products%0A      derived from this software without specific prior written permission.%0A%0ATHIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS %22AS IS%22%0AAND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE%0AIMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE%0AARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE%0ALIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR%0ACONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF%0ASUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS%0AINTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN%0ACONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)%0AARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE%0APOSSIBILITY OF SUCH DAMAGE.%0A*/%0A%0Apackage imagej.util;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.Collections;%0Aimport java.util.Comparator;%0Aimport java.util.Iterator;%0A%0A/**%0A * Sort values obtained from an Iterator by priority%0A */%0Apublic abstract class SortByPriority%3CT%3E extends ArrayList%3CT%3E %7B%0A%0A%09protected abstract double getPriority(T object);%0A%0A%09public SortByPriority(final Iterable%3CT%3E iterable) %7B%0A%09%09this(iterable.iterator());%0A%09%7D%0A%0A%09public SortByPriority(final Iterator%3CT%3E iterator) %7B%0A%09%09while (iterator.hasNext()) %7B%0A%09%09%09final T item = iterator.next();%0A%09%09%09add(item);%0A%09%09%7D%0A%0A%09%09Collections.sort(this, new Comparator%3CT%3E() %7B%0A%0A%09%09%09@Override%0A%09%09%09public boolean equals(final Object otherComparator) %7B%0A%09%09%09%09return false;%0A%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public int compare(final T a, final T b) %7B%0A%09%09%09%09double priorityA = getPriority(a);%0A%09%09%09%09double priorityB = getPriority(b);%0A%09%09%09%09return priorityA %3C priorityB ? -1 : (priorityA %3E priorityB ? +1 : 0);%0A%09%09%09%7D%0A%09%09%7D);%0A%09%7D%0A%0A%7D%0A

Add a class to iterate through collections in order of priority