Skip to main content
summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to 'common/plugins')
-rw-r--r--common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/InternalJptResourceType.java4
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/SystemTools.java6
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/CollectionTools.java3
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparableComparator.java5
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorAdapter.java8
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorChain.java52
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorTools.java257
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/FalsesFirstBooleanComparator.java49
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/NullsFirstComparator.java45
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/NullsLastComparator.java45
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ReverseComparator.java32
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/StringCollator.java18
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/TransformationComparator.java (renamed from common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/TransformationComparator.java)19
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/TruesFirstBooleanComparator.java49
-rw-r--r--common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/VersionComparator.java76
15 files changed, 518 insertions, 150 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/InternalJptResourceType.java b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/InternalJptResourceType.java
index 5f80f2c109..6ba5d51bf1 100644
--- a/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/InternalJptResourceType.java
+++ b/common/plugins/org.eclipse.jpt.common.core/src/org/eclipse/jpt/common/core/internal/InternalJptResourceType.java
@@ -13,7 +13,7 @@ import java.util.HashSet;
import org.eclipse.core.runtime.content.IContentType;
import org.eclipse.jpt.common.core.JptResourceType;
import org.eclipse.jpt.common.core.JptResourceTypeManager;
-import org.eclipse.jpt.common.utility.internal.comparator.VersionComparator;
+import org.eclipse.jpt.common.utility.internal.comparator.ComparatorTools;
import org.eclipse.jpt.common.utility.internal.iterable.SuperIterableWrapper;
import com.ibm.icu.text.Collator;
@@ -112,7 +112,7 @@ public class InternalJptResourceType
if (v.equals(UNDETERMINED_VERSION)) {
return 1;
}
- return VersionComparator.INTEGER_VERSION_COMPARATOR.compare(this.version, v);
+ return ComparatorTools.integerVersionComparator().compare(this.version, v);
}
@Override
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/SystemTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/SystemTools.java
index 96ab4026a5..4a5d2e91f1 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/SystemTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/SystemTools.java
@@ -11,7 +11,7 @@ package org.eclipse.jpt.common.utility.internal;
import java.io.PrintStream;
import java.util.Map;
-import org.eclipse.jpt.common.utility.internal.comparator.VersionComparator;
+import org.eclipse.jpt.common.utility.internal.comparator.ComparatorTools;
/**
* Various system utility methods.
@@ -75,7 +75,7 @@ public final class SystemTools {
* developers. :-)
*/
public static boolean javaSpecificationVersionIsGreaterThan(String version) {
- return VersionComparator.INTEGER_VERSION_COMPARATOR.compare(javaSpecificationVersion(), version) > 0;
+ return ComparatorTools.integerVersionComparator().compare(javaSpecificationVersion(), version) > 0;
}
/**
@@ -91,7 +91,7 @@ public final class SystemTools {
* is less than the specified version (e.g. <code>"1.5"</code>).
*/
public static boolean javaSpecificationVersionIsLessThan(String version) {
- return VersionComparator.INTEGER_VERSION_COMPARATOR.compare(javaSpecificationVersion(), version) < 0;
+ return ComparatorTools.integerVersionComparator().compare(javaSpecificationVersion(), version) < 0;
}
/**
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/CollectionTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/CollectionTools.java
index 4f5e27eadd..6f3439fc74 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/CollectionTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/CollectionTools.java
@@ -255,6 +255,9 @@ public final class CollectionTools {
* partitions, all the partitions will be one of two sizes; the first
* partions will be of size <code>collection.size()/count+1</code>,
* while the last partions will be of size <code>collection.size()/count</code>.
+ * The partitions will maintain the order of elements returned by the
+ * collection's iterator (i.e. the first elements returned by the iterator
+ * will be in the first partition, first element first).
*/
public static <E> ArrayList<ArrayList<E>> partition(Collection<? extends E> collection, int count) {
if (count <= 0) {
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparableComparator.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparableComparator.java
index be2671c397..da145045ad 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparableComparator.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparableComparator.java
@@ -15,16 +15,17 @@ import java.util.Comparator;
/**
* This comparator compares elements that implement the
* {@link Comparable} interface.
+ *
* @param <E> the type of elements to be compared
*/
-public class ComparableComparator<E extends Comparable<E>>
+public final class ComparableComparator<E extends Comparable<E>>
implements Comparator<E>, Serializable
{
@SuppressWarnings("rawtypes")
public static final Comparator INSTANCE = new ComparableComparator();
@SuppressWarnings("unchecked")
- public static <E> Comparator<E> instance() {
+ public static <E extends Comparable<E>> Comparator<E> instance() {
return INSTANCE;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorAdapter.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorAdapter.java
index 8ef71ad838..137dfc6235 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorAdapter.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorAdapter.java
@@ -15,12 +15,12 @@ import org.eclipse.jpt.common.utility.internal.ObjectTools;
/**
* Convenience comparator that always returns 0;
*
- * @param <T> the type of objects to be compared
+ * @param <E> the type of objects to be compared
*/
-public class ComparatorAdapter<T>
- implements Comparator<T>
+public class ComparatorAdapter<E>
+ implements Comparator<E>
{
- public int compare(T o1, T o2) {
+ public int compare(E o1, E o2) {
return 0;
}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorChain.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorChain.java
new file mode 100644
index 0000000000..c168aa2bb4
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorChain.java
@@ -0,0 +1,52 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0, which accompanies this distribution
+ * and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.comparator;
+
+import java.util.Comparator;
+import java.util.Iterator;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+import org.eclipse.jpt.common.utility.internal.iterable.IterableTools;
+
+/**
+ * This comparator will use a list of comparators to compare two objects.
+ * If the first comparator returns a non-zero value, that will be the
+ * comparator's value; otherwise the next comparator will be called; and so on.
+ *
+ * @param <E> the type of elements to be compared
+ */
+public class ComparatorChain<E>
+ implements Comparator<E>
+{
+ private final Iterable<Comparator<? super E>> comparators;
+
+ public ComparatorChain(Iterable<Comparator<? super E>> comparators) {
+ super();
+ if (IterableTools.isOrContainsNull(comparators)) {
+ throw new NullPointerException();
+ }
+ if (IterableTools.isEmpty(comparators)) {
+ throw new IllegalArgumentException("comparators must not empty"); //$NON-NLS-1$
+ }
+ this.comparators = comparators;
+ }
+
+ public int compare(E o1, E o2) {
+ int result = 0;
+ for (Iterator<Comparator<? super E>> stream = this.comparators.iterator(); stream.hasNext() && (result == 0); ) {
+ result = stream.next().compare(o1, o2);
+ }
+ return result;
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.comparators);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorTools.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorTools.java
index 12881f2b13..ed7142fe92 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorTools.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ComparatorTools.java
@@ -9,8 +9,11 @@
******************************************************************************/
package org.eclipse.jpt.common.utility.internal.comparator;
+import java.text.Collator;
import java.util.Comparator;
-import org.eclipse.jpt.common.utility.internal.transformer.TransformationComparator;
+import java.util.Locale;
+import org.eclipse.jpt.common.utility.internal.ArrayTools;
+import org.eclipse.jpt.common.utility.internal.comparator.VersionComparator.SegmentParser;
import org.eclipse.jpt.common.utility.transformer.Transformer;
/**
@@ -18,21 +21,208 @@ import org.eclipse.jpt.common.utility.transformer.Transformer;
*/
public final class ComparatorTools {
+ /**
+ * Of the two specified objects, return the one that is ordered first.
+ * @param <E> the type of elements to be compared
+ * @see Comparable#compareTo(Object)
+ */
+ public static <E extends Comparable<E>> E min(E o1, E o2) {
+ return (o1.compareTo(o2) < 0) ? o1 : o2;
+ }
+
+ /**
+ * Of the two specified objects, return the one that is ordered first,
+ * as determined by the specified comparator.
+ * @param <E> the type of elements to be compared
+ * @see Comparable#compareTo(Object)
+ */
+ public static <E> E min(E o1, E o2, Comparator<? super E> comparator) {
+ return (comparator.compare(o1, o2) < 0) ? o1 : o2;
+ }
+
+ /**
+ * Of the two specified objects, return the one that is ordered last.
+ * @param <E> the type of elements to be compared
+ * @see Comparable#compareTo(Object)
+ */
+ public static <E extends Comparable<E>> E max(E o1, E o2) {
+ return (o1.compareTo(o2) > 0) ? o1 : o2;
+ }
+
+ /**
+ * Of the two specified objects, return the one that is ordered last,
+ * as determined by the specified comparator.
+ * @param <E> the type of elements to be compared
+ * @see Comparable#compareTo(Object)
+ */
+ public static <E> E max(E o1, E o2, Comparator<? super E> comparator) {
+ return (comparator.compare(o1, o2) > 0) ? o1 : o2;
+ }
+
+
+ // ********** boolean **********
+
+ /**
+ * Return a comparator will compare {@link Boolean}s, depending the
+ * specified "true first" flag.
+ */
+ public static Comparator<Boolean> booleanComparator(boolean trueFirst) {
+ return trueFirst ? truesFirstBooleanComparator() : falsesFirstBooleanComparator();
+ }
+
+ /**
+ * Return a comparator will compare {@link Boolean}s, sorting
+ * <code>false</code>s first.
+ */
+ public static Comparator<Boolean> falsesFirstBooleanComparator() {
+ return FalsesFirstBooleanComparator.instance();
+ }
+
+ /**
+ * Return a comparator will compare {@link Boolean}s, sorting
+ * <code>true</code>s first.
+ */
+ public static Comparator<Boolean> truesFirstBooleanComparator() {
+ return TruesFirstBooleanComparator.instance();
+ }
+
+
+ // ********** chain **********
+
+ /**
+ * @see #chain(Iterable)
+ */
+ public static <E> Comparator<E> chain(Comparator<? super E>... comparators) {
+ return chain(ArrayTools.iterable(comparators));
+ }
+
+ /**
+ * Return a comparator that will use the specified list of comparators to
+ * compare two objects.
+ * If the first comparator returns a non-zero value, that will be the
+ * comparator's value; otherwise the next comparator will be called;
+ * and so on.
+ * @param <E> the type of elements to be compared
+ */
+ public static <E> Comparator<E> chain(Iterable<Comparator<? super E>> comparators) {
+ return new ComparatorChain<E>(comparators);
+ }
+
+
+ // ********** comparable/natural **********
+
+ /**
+ * Return a comparator will compare {@link Comparable}s.
+ * @param <E> the type of elements to be compared
+ */
+ public static <E extends Comparable<E>> Comparator<E> comparableComparator() {
+ return ComparableComparator.instance();
+ }
+
+ /**
+ * Return a comparator will compare {@link Comparable}s.
+ * @param <E> the type of elements to be compared
+ */
+ public static <E extends Comparable<E>> Comparator<E> naturalComparator() {
+ return comparableComparator();
+ }
+
+
+ // ********** nulls **********
+
+ /**
+ * Return a comparator that will sort <code>null</code>s <em>before</em> any
+ * non-<code>null</code> elements. Non-<code>null</code> elements will be
+ * compared by the specified comparator.
+ * @param <E> the type of elements to be compared
+ */
+ public static <E> Comparator<E> nullsFirst(Comparator<? super E> comparator) {
+ return new NullsFirstComparator<E>(comparator);
+ }
+
+ /**
+ * Return a comparator that will sort <code>null</code>s <em>after</em> any
+ * non-<code>null</code> elements. Non-<code>null</code> elements will be
+ * compared by the specified comparator.
+ * @param <E> the type of elements to be compared
+ */
+ public static <E> Comparator<E> nullsLast(Comparator<? super E> comparator) {
+ return new NullsLastComparator<E>(comparator);
+ }
+
+
+ // ********** reverse **********
+
+ /**
+ * Return a comparator that will reverse the order of
+ * {@link Comparable}s.
+ * @param <E> the type of elements to be compared
+ */
+ @SuppressWarnings("unchecked")
+ public static <E extends Comparable<E>> Comparator<E> reverseComparator() {
+ return reverse((Comparator<E>) comparableComparator());
+ }
+
+ /**
+ * Return a comparator that will reverse the order of the specified
+ * comparator.
+ * @param <E> the type of elements to be compared
+ */
+ public static <E> Comparator<E> reverse(Comparator<? super E> comparator) {
+ return new ReverseComparator<E>(comparator);
+ }
+
+
+ // ********** string **********
+
+ /**
+ * Return a collator that wraps the default Java text collator and
+ * implements a {@link String} {@link Comparator} (instead of an
+ * {@link Object} {@link Comparator}, which is what {@link Collator} does,
+ * possibly for backward-compatibility reasons(?)).
+ * @see Collator#getInstance()
+ */
+ public static Comparator<String> stringCollator() {
+ return stringCollator(Collator.getInstance());
+ }
+
+ /**
+ * Return a collator that wraps the Java text collator for the specified
+ * locale and implements a
+ * {@link String} {@link Comparator} (instead of an {@link Object}
+ * {@link Comparator}, which is what {@link Collator} does, possibly for
+ * backward-compatibility reasons(?)).
+ * @see Collator#getInstance(Locale)
+ */
+ public static Comparator<String> stringCollator(Locale locale) {
+ return stringCollator(Collator.getInstance(locale));
+ }
+
+ /**
+ * Return a collator that wraps the specified Java text collator and
+ * implements a {@link String} {@link Comparator} (instead of an
+ * {@link Object} {@link Comparator}, which is what {@link Collator}
+ * does, possibly for backward-compatibility reasons(?)).
+ * @see Collator
+ */
+ public static Comparator<String> stringCollator(Collator collator) {
+ return new StringCollator(collator);
+ }
+
+
// ********** transformation **********
/**
* Return a comparator will transform the elements to be compared and
* compare the resulting outputs (i.e. assume the outputs
* implement the {@link Comparable} interface).
- *
* @param <E> the type of elements to be compared
* @param <O> the type of the result of transforming the elements and the type
* of the elements to be compared by the wrapped comaparator, if present
- *
- * @see TransformationComparator
*/
+ @SuppressWarnings("unchecked")
public static <E, O> Comparator<E> transformationComparator(Transformer<? super E, ? extends O> transformer) {
- return transformationComparator(transformer, null);
+ return transformationComparator(transformer, (Comparator<O>) comparableComparator());
}
/**
@@ -41,18 +231,69 @@ public final class ComparatorTools {
* If the specified comparator is <code>null</code>,
* the natural ordering of the outputs will be used (i.e. assume the outputs
* implement the {@link Comparable} interface).
- *
* @param <E> the type of elements to be compared
* @param <O> the type of the result of transforming the elements and the type
* of the elements to be compared by the wrapped comaparator, if present
- *
- * @see TransformationComparator
*/
public static <E, O> Comparator<E> transformationComparator(Transformer<? super E, ? extends O> transformer, Comparator<O> comparator) {
return new TransformationComparator<E, O>(transformer, comparator);
}
+ // ********** version **********
+
+ /**
+ * Return a version comparator that converts
+ * each version into a series of integers and compares them.
+ * <p>
+ * <strong>NB:</strong> With this comparator
+ * <code>"2.<strong>14</strong>" > "2.<strong>2</strong>"</code>
+ * is <code>true</code>.
+ */
+ public static <T extends Comparable<T>> Comparator<String> integerVersionComparator() {
+ return INTEGER_VERSION_COMPARATOR;
+ }
+
+ /**
+ * @see #integerVersionComparator()
+ */
+ public static final Comparator<String> INTEGER_VERSION_COMPARATOR = versionComparator(VersionComparator.SegmentParser.IntegerSegmentParser.instance());
+
+ /**
+ * The default delimiter is <code>'.'</code>.
+ * @see #versionComparator(String, SegmentParser)
+ */
+ public static <T extends Comparable<T>> Comparator<String> versionComparator(SegmentParser<T> segmentParser) {
+ return versionComparator(".", segmentParser); //$NON-NLS-1$
+ }
+
+ /**
+ * @see #versionComparator(String, SegmentParser)
+ */
+ public static <T extends Comparable<T>> Comparator<String> versionComparator(char delimiter, SegmentParser<T> segmentParser) {
+ return versionComparator(new char[] {delimiter}, segmentParser);
+ }
+
+ /**
+ * @see #versionComparator(String, SegmentParser)
+ */
+ public static <T extends Comparable<T>> Comparator<String> versionComparator(char[] delimiters, SegmentParser<T> segmentParser) {
+ return versionComparator(new String(delimiters), segmentParser);
+ }
+
+ /**
+ * Return a comparator tha can be used to compare version strings
+ * (e.g. <code>"2.2.2"</code> vs. <code>"2.14.3"</code>).
+ * Clients can specify the delimiter(s) that separates a version's
+ * <em>segments</em> as well as a parser to be used for parsing each
+ * <em>segment</em>.
+ * @param <T> the type of comparable returned by the comparator's segment parser
+ */
+ public static <T extends Comparable<T>> Comparator<String> versionComparator(String delimiters, SegmentParser<T> segmentParser) {
+ return new VersionComparator<T>(delimiters, segmentParser);
+ }
+
+
// ********** constructor **********
/**
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/FalsesFirstBooleanComparator.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/FalsesFirstBooleanComparator.java
new file mode 100644
index 0000000000..cbb07d7ce1
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/FalsesFirstBooleanComparator.java
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0, which accompanies this distribution
+ * and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.comparator;
+
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ * This comparator will compare {@link Boolean}s and can be configured to sort
+ * either boolean first.
+ */
+public final class FalsesFirstBooleanComparator
+ implements Comparator<Boolean>, Serializable
+{
+ public static final Comparator<Boolean> INSTANCE = new FalsesFirstBooleanComparator();
+
+ public static Comparator<Boolean> instance() {
+ return INSTANCE;
+ }
+
+ // ensure single instance
+ private FalsesFirstBooleanComparator() {
+ super();
+ }
+
+ public int compare(Boolean b1, Boolean b2) {
+ return b1.booleanValue() ?
+ (b2.booleanValue() ? 0 : 1) :
+ (b2.booleanValue() ? -1 : 0);
+ }
+
+ @Override
+ public String toString() {
+ return this.getClass().getSimpleName();
+ }
+
+ private static final long serialVersionUID = 1L;
+ private Object readResolve() {
+ // replace this object with the singleton
+ return INSTANCE;
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/NullsFirstComparator.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/NullsFirstComparator.java
new file mode 100644
index 0000000000..0ab6ec0bdd
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/NullsFirstComparator.java
@@ -0,0 +1,45 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0, which accompanies this distribution
+ * and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.comparator;
+
+import java.util.Comparator;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * This comparator sort <code>null</code>s <em>before</em> any
+ * non-<code>null</code> elements. Non-<code>null</code> elements will be
+ * compared by the configured comparator.
+ *
+ * @param <E> the type of elements to be compared
+ */
+public class NullsFirstComparator<E>
+ implements Comparator<E>
+{
+ private final Comparator<? super E> comparator;
+
+ public NullsFirstComparator(Comparator<? super E> comparator) {
+ super();
+ if (comparator == null) {
+ throw new NullPointerException();
+ }
+ this.comparator = comparator;
+ }
+
+ public int compare(E e1, E e2) {
+ return (e1 == null) ?
+ ((e2 == null) ? 0 : -1) :
+ ((e2 == null) ? 1 : this.comparator.compare(e1, e2));
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.comparator);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/NullsLastComparator.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/NullsLastComparator.java
new file mode 100644
index 0000000000..c9b4e74868
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/NullsLastComparator.java
@@ -0,0 +1,45 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0, which accompanies this distribution
+ * and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.comparator;
+
+import java.util.Comparator;
+import org.eclipse.jpt.common.utility.internal.ObjectTools;
+
+/**
+ * This comparator sort <code>null</code>s <em>after</em> any
+ * non-<code>null</code> elements. Non-<code>null</code> elements will be
+ * compared by the configured comparator.
+ *
+ * @param <E> the type of elements to be compared
+ */
+public class NullsLastComparator<E>
+ implements Comparator<E>
+{
+ private final Comparator<? super E> comparator;
+
+ public NullsLastComparator(Comparator<? super E> comparator) {
+ super();
+ if (comparator == null) {
+ throw new NullPointerException();
+ }
+ this.comparator = comparator;
+ }
+
+ public int compare(E e1, E e2) {
+ return (e1 == null) ?
+ ((e2 == null) ? 0 : 1) :
+ ((e2 == null) ? -1 : this.comparator.compare(e1, e2));
+ }
+
+ @Override
+ public String toString() {
+ return ObjectTools.toString(this, this.comparator);
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ReverseComparator.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ReverseComparator.java
index deabac843a..db2673a8c2 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ReverseComparator.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/ReverseComparator.java
@@ -9,43 +9,29 @@
******************************************************************************/
package org.eclipse.jpt.common.utility.internal.comparator;
-import java.io.Serializable;
import java.util.Comparator;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
/**
- * This comparator will reverse the order of the specified comparator.
- * If the specified comparator is <code>null</code>,
- * the natural ordering of the objects will be used (i.e. assume the elements
- * implement the {@link Comparable} interface.
+ * This comparator will reverse the order of the configured comparator.
+ *
* @param <E> the type of elements to be compared
*/
public class ReverseComparator<E>
- implements Comparator<E>, Serializable
+ implements Comparator<E>
{
- private final Comparator<E> comparator;
- private static final long serialVersionUID = 1L;
-
- /**
- * Construct a reverse comparator that will reverse the natural order of
- * the compared elements.
- */
- public ReverseComparator() {
- this(null);
- }
+ private final Comparator<? super E> comparator;
- /**
- * Construct a reverse comparator that will reverse the order of
- * the compared elements as calculated by the specified comparator.
- */
- public ReverseComparator(Comparator<E> comparator) {
+ public ReverseComparator(Comparator<? super E> comparator) {
super();
+ if (comparator == null) {
+ throw new NullPointerException();
+ }
this.comparator = comparator;
}
- @SuppressWarnings("unchecked")
public int compare(E e1, E e2) {
- return (this.comparator != null) ? this.comparator.compare(e2, e1) : ((Comparable<E>) e2).compareTo(e1);
+ return this.comparator.compare(e2, e1);
}
@Override
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/StringCollator.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/StringCollator.java
index d2d071a5c3..c8295a183a 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/StringCollator.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/StringCollator.java
@@ -11,7 +11,6 @@ package org.eclipse.jpt.common.utility.internal.comparator;
import java.text.Collator;
import java.util.Comparator;
-import java.util.Locale;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
/**
@@ -27,23 +26,6 @@ public class StringCollator
{
private final Collator collator;
-
- /**
- * Wrap the default collator.
- * @see Collator#getInstance()
- */
- public StringCollator() {
- this(Collator.getInstance());
- }
-
- /**
- * Wrap the collator for the specified locale.
- * @see Collator#getInstance(Locale)
- */
- public StringCollator(Locale locale) {
- this(Collator.getInstance(locale));
- }
-
/**
* Wrap the specified collator.
*/
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/TransformationComparator.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/TransformationComparator.java
index 80c856b115..d3feaec20d 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/transformer/TransformationComparator.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/TransformationComparator.java
@@ -7,7 +7,7 @@
* Contributors:
* Oracle - initial API and implementation
******************************************************************************/
-package org.eclipse.jpt.common.utility.internal.transformer;
+package org.eclipse.jpt.common.utility.internal.comparator;
import java.util.Comparator;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
@@ -16,9 +16,6 @@ import org.eclipse.jpt.common.utility.transformer.Transformer;
/**
* This comparator will transform the elements to be compared and pass the
* resulting outputs to a wrapped transformer.
- * If the wrapped comparator is <code>null</code>,
- * the natural ordering of the outputs will be used (i.e. assume the outputs
- * implement the {@link Comparable} interface).
* @param <E> the type of elements to be compared
* @param <O> the type of the result of transforming the elements and the type
* of the elements to be compared by the wrapped comaparator, if present
@@ -30,27 +27,17 @@ public class TransformationComparator<E, O>
private final Comparator<O> comparator;
- /**
- * Construct a comparator that will use the specified transformer to
- * transform the elements to be compared. The resulting outputs will be
- * passed to the specified comparator and the result returned by the
- * transformation comparator as the result of comparing the original
- * elements.
- */
public TransformationComparator(Transformer<? super E, ? extends O> transformer, Comparator<O> comparator) {
super();
- if (transformer == null) {
+ if ((transformer == null) || (comparator == null)) {
throw new NullPointerException();
}
this.transformer = transformer;
this.comparator = comparator;
}
- @SuppressWarnings("unchecked")
public int compare(E e1, E e2) {
- O o1 = this.transformer.transform(e1);
- O o2 = this.transformer.transform(e2);
- return (this.comparator != null) ? this.comparator.compare(o1, o2) : ((Comparable<O>) o1).compareTo(o2);
+ return this.comparator.compare(this.transformer.transform(e1), this.transformer.transform(e2));
}
@Override
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/TruesFirstBooleanComparator.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/TruesFirstBooleanComparator.java
new file mode 100644
index 0000000000..d0e060f28c
--- /dev/null
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/TruesFirstBooleanComparator.java
@@ -0,0 +1,49 @@
+/*******************************************************************************
+ * Copyright (c) 2013 Oracle. All rights reserved.
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0, which accompanies this distribution
+ * and is available at http://www.eclipse.org/legal/epl-v10.html.
+ *
+ * Contributors:
+ * Oracle - initial API and implementation
+ ******************************************************************************/
+package org.eclipse.jpt.common.utility.internal.comparator;
+
+import java.io.Serializable;
+import java.util.Comparator;
+
+/**
+ * This comparator will compare {@link Boolean}s and can be configured to sort
+ * either boolean first.
+ */
+public final class TruesFirstBooleanComparator
+ implements Comparator<Boolean>, Serializable
+{
+ public static final Comparator<Boolean> INSTANCE = new TruesFirstBooleanComparator();
+
+ public static Comparator<Boolean> instance() {
+ return INSTANCE;
+ }
+
+ // ensure single instance
+ private TruesFirstBooleanComparator() {
+ super();
+ }
+
+ public int compare(Boolean b1, Boolean b2) {
+ return b1.booleanValue() ?
+ (b2.booleanValue() ? 0 : -1) :
+ (b2.booleanValue() ? 1 : 0);
+ }
+
+ @Override
+ public String toString() {
+ return this.getClass().getSimpleName();
+ }
+
+ private static final long serialVersionUID = 1L;
+ private Object readResolve() {
+ // replace this object with the singleton
+ return INSTANCE;
+ }
+}
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/VersionComparator.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/VersionComparator.java
index ff54f51c1c..a74e47934c 100644
--- a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/VersionComparator.java
+++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/comparator/VersionComparator.java
@@ -22,7 +22,7 @@ import org.eclipse.jpt.common.utility.internal.ObjectTools;
* <em>segments</em> as well as a parser to be used for parsing each
* <em>segment</em>.
*
- * @see #INTEGER_VERSION_COMPARATOR
+ * @param <T> the type of comparable returned by the comparator's segment parser
*/
public class VersionComparator<T extends Comparable<T>>
implements Comparator<String>
@@ -31,42 +31,6 @@ public class VersionComparator<T extends Comparable<T>>
private final SegmentParser<T> segmentParser;
- /**
- * Static implementation of the version comparator interface that converts
- * each version into a series of integers and compares them.
- * <p>
- * <strong>NB:</strong> With this comparator
- * <code>"2.<strong>14</strong>" > "2.<strong>2</strong>"</code>
- * is <code>true</code>.
- */
- public static final Comparator<String> INTEGER_VERSION_COMPARATOR = new VersionComparator<Integer>(SegmentParser.IntegerSegmentParser.instance());
-
-
- /**
- * Use the specified segment parser.
- * The default delimiter is <code>'.'</code>.
- */
- public VersionComparator(SegmentParser<T> segmentParser) {
- this(".", segmentParser); //$NON-NLS-1$
- }
-
- /**
- * Use the specified delimiters and segment parser.
- */
- public VersionComparator(char delimiter, SegmentParser<T> segmentParser) {
- this(new char[] {delimiter}, segmentParser);
- }
-
- /**
- * Use the specified delimiters and segment parser.
- */
- public VersionComparator(char[] delimiters, SegmentParser<T> segmentParser) {
- this(new String(delimiters), segmentParser);
- }
-
- /**
- * Use the specified delimiters and segment parser.
- */
public VersionComparator(String delimiters, SegmentParser<T> segmentParser) {
super();
if ((delimiters == null) || (segmentParser == null)) {
@@ -76,11 +40,10 @@ public class VersionComparator<T extends Comparable<T>>
this.segmentParser = segmentParser;
}
-
/**
* <strong>NB:</strong> Callers must handle any runtime exceptions thrown by the
* segment parser supplied to the comparator. In particular, the pre-built
- * integer segment parser {@link #INTEGER_VERSION_COMPARATOR} can throw a
+ * integer segment parser {@link ComparatorTools#INTEGER_VERSION_COMPARATOR} can throw a
* {@link NumberFormatException} if any segement string contains non-numeric
* characters.
*/
@@ -194,40 +157,5 @@ public class VersionComparator<T extends Comparable<T>>
return INSTANCE;
}
}
-
- /**
- * Singleton implementation of the segment parser interface that throws
- * an exception if called.
- */
- final class Disabled<S extends Comparable<S>>
- implements SegmentParser<S>, Serializable
- {
- @SuppressWarnings("rawtypes")
- public static final SegmentParser INSTANCE = new Disabled();
- @SuppressWarnings("unchecked")
- public static <R extends Comparable<R>> SegmentParser<R> instance() {
- return INSTANCE;
- }
- // ensure single instance
- private Disabled() {
- super();
- }
- // throw an exception
- public S parse(int segmentIndex, String segment) {
- throw new UnsupportedOperationException();
- }
- public S getZero() {
- throw new UnsupportedOperationException();
- }
- @Override
- public String toString() {
- return ObjectTools.singletonToString(this);
- }
- private static final long serialVersionUID = 1L;
- private Object readResolve() {
- // replace this object with the singleton
- return INSTANCE;
- }
- }
}
}

Back to the top