You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2021/09/08 16:39:57 UTC

[sis] 09/09: Update for change in GeoAPI: parameterize `SortBy` and `SortProperty`. Use `SortBy` instead of `SortProperty[]` where appropriate.

This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit bc2b5511300043ad73d565e4d9e00e238c2549ec
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Wed Sep 8 18:16:54 2021 +0200

    Update for change in GeoAPI: parameterize `SortBy` and `SortProperty`.
    Use `SortBy` instead of `SortProperty[]` where appropriate.
---
 .../apache/sis/filter/DefaultFilterFactory.java    |   6 +-
 .../org/apache/sis/filter/DefaultSortProperty.java |  95 ++++++++++++++---
 .../sis/internal/sql/feature/FeatureIterator.java  |   7 +-
 .../sis/internal/sql/feature/FeatureStream.java    |  21 ++--
 .../sis/internal/storage/query/FeatureQuery.java   |  67 ++++++------
 .../sis/internal/storage/query/FeatureSubset.java  |   8 +-
 .../internal/storage/query/SortByComparator.java   | 118 ++++++++-------------
 .../internal/storage/query/SimpleQueryTest.java    |   2 +-
 8 files changed, 182 insertions(+), 142 deletions(-)

diff --git a/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java b/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java
index ea9a6ff..029eb4e 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultFilterFactory.java
@@ -995,13 +995,15 @@ public abstract class DefaultFilterFactory<R,G,T> extends AbstractFactory implem
 
     /**
      * Indicates an property by which contents should be sorted, along with intended order.
+     * The given expression should evaluate to {@link Comparable} objects,
+     * but {@link Iterable} objects are accepted as well.
      *
      * @param  property  the property to sort by.
      * @param  order     the sorting order, ascending or descending.
      * @return definition of sort order of a property.
      */
     @Override
-    public SortProperty sort(final ValueReference<? super R, ?> property, final SortOrder order) {
-        return new DefaultSortProperty(property, order);
+    public SortProperty<R> sort(final ValueReference<? super R, ?> property, final SortOrder order) {
+        return new DefaultSortProperty<>(property, order);
     }
 }
diff --git a/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultSortProperty.java b/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultSortProperty.java
index af2bae3..3c80d20 100644
--- a/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultSortProperty.java
+++ b/core/sis-feature/src/main/java/org/apache/sis/filter/DefaultSortProperty.java
@@ -16,8 +16,12 @@
  */
 package org.apache.sis.filter;
 
+import java.util.Iterator;
+import java.util.Collections;
 import java.io.Serializable;
 import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.collection.Containers;
+import org.apache.sis.util.resources.Errors;
 
 // Branch-dependent imports
 import org.opengis.filter.SortOrder;
@@ -29,45 +33,49 @@ import org.opengis.filter.ValueReference;
  * Defines a sort order based on a property and ascending/descending order.
  *
  * @author  Johann Sorel (Geomatys)
+ * @author  Martin Desruisseaux (Geomatys)
  * @version 1.1
- * @since   1.1
+ *
+ * @param  <R>  the type of resources (typically {@code Feature}) to sort.
+ *
+ * @since 1.1
  * @module
  */
-final class DefaultSortProperty implements SortProperty, Serializable {
+final class DefaultSortProperty<R> implements SortProperty<R>, Serializable {
     /**
      * For cross-version compatibility.
      */
-    private static final long serialVersionUID = 5434026034835575812L;
+    private static final long serialVersionUID = 6065805794498223206L;
 
     /**
      * The property on which to apply sorting.
      */
-    private final ValueReference<?,?> property;
+    private final ValueReference<? super R, ?> property;
 
     /**
-     * The desired order: {@code ASCENDING} or {@code DESCENDING}.
+     * Whether the sorting order is {@code ASCENDING} or {@code DESCENDING}.
      */
-    private final SortOrder order;
+    private final boolean descending;
 
     /**
-     * Creates a new {@code SortBy} filter.
+     * Creates a new {@code SortProperty} instance.
      * It is caller responsibility to ensure that no argument is null.
      *
      * @param property  property on which to apply sorting.
      * @param order     the desired order: {@code ASCENDING} or {@code DESCENDING}.
      */
-    DefaultSortProperty(final ValueReference<?,?> property, final SortOrder order) {
-        this.property = property;
-        this.order    = order;
+    DefaultSortProperty(final ValueReference<? super R, ?> property, final SortOrder order) {
         ArgumentChecks.ensureNonNull("property", property);
         ArgumentChecks.ensureNonNull("order",    order);
+        this.property = property;
+        descending = SortOrder.DESCENDING.equals(order);
     }
 
     /**
      * Returns the property to sort by.
      */
     @Override
-    public ValueReference<?,?> getValueReference() {
+    public ValueReference<? super R, ?> getValueReference() {
         return property;
     }
 
@@ -76,7 +84,61 @@ final class DefaultSortProperty implements SortProperty, Serializable {
      */
     @Override
     public SortOrder getSortOrder() {
-        return order;
+        return descending ? SortOrder.DESCENDING : SortOrder.ASCENDING;
+    }
+
+    /**
+     * Compares two resources for order. Returns a negative number if {@code r1} should be sorted before {@code r2},
+     * a positive number if {@code r2} should be after {@code r1}, or 0 if both resources are equal.
+     * The ordering of null resources or null property values is unspecified and may change in any future version.
+     *
+     * @param  r1  the first resource to compare.
+     * @param  r2  the second resource to compare.
+     * @return negative if the first resource is before the second, positive for the converse, or 0 if equal.
+     * @throws ClassCastException if the types of {@linkplain ValueReference#apply(Object) property values}
+     *         prevent them from being compared by this comparator.
+     */
+    @Override
+    @SuppressWarnings({"rawtypes","unchecked"})
+    public int compare(R r1, R r2) {
+        if (descending) {
+            final R rt = r1;
+            r1 = r2;
+            r2 = rt;
+        }
+        if (r1 == null) return +1;
+        if (r2 == null) return -1;
+        final Object o1 = property.apply(r1);
+        final Object o2 = property.apply(r2);
+        if (o1 == null) return +1;
+        if (o2 == null) return -1;
+        /*
+         * Following code uses raw types and unsafe casts, but actually it should be okay.
+         * If a cast is invalid, a `ClassCastException` will be thrown almost immediately,
+         * either in this method or in `Containers.compare(…)` loop. Because this exception
+         * is part of the `Comparator.compare(…)` method contract, we are compliant.
+         */
+        if (o1 instanceof Comparable<?> && o2 instanceof Comparable<?>) {
+            return ((Comparable) o1).compareTo(o2);
+        }
+        if (o1 instanceof Iterable<?>) {
+            return Containers.compare(((Iterable) o1).iterator(), iterator(o2));
+        }
+        if (o2 instanceof Iterable<?>) {
+            return Containers.compare(iterator(o1), ((Iterable) o2).iterator());
+        }
+        throw new ClassCastException(Errors.format(Errors.Keys.IllegalPropertyValueClass_3,
+                property.getXPath(), Comparable.class, (o1 instanceof Comparable<?> ? o2 : o1).getClass()));
+    }
+
+    /**
+     * Returns an iterator for the given object. Intentionally raw return type for allowing unchecked casts
+     * in {@link #compare(Object, Object)}. It is not as unsafe as it looks since {@link ClassCastException}
+     * should happen soon if the type is incorrect and that exception is part of method contract.
+     */
+    @SuppressWarnings("rawtypes")
+    private static Iterator iterator(final Object o) {
+        return (o instanceof Iterable<?>) ? ((Iterable<?>) o).iterator() : Collections.singleton(o).iterator();
     }
 
     /**
@@ -84,7 +146,7 @@ final class DefaultSortProperty implements SortProperty, Serializable {
      */
     @Override
     public int hashCode() {
-        return property.hashCode() + 41 * order.hashCode();
+        return property.hashCode() + Boolean.hashCode(descending);
     }
 
     /**
@@ -95,10 +157,9 @@ final class DefaultSortProperty implements SortProperty, Serializable {
         if (this == obj) {
             return true;
         }
-        if (obj instanceof DefaultSortProperty) {
-            final DefaultSortProperty other = (DefaultSortProperty) obj;
-            return property.equals(other.property)
-                   && order.equals(other.order);
+        if (obj instanceof DefaultSortProperty<?>) {
+            final DefaultSortProperty<?> other = (DefaultSortProperty<?>) obj;
+            return descending == other.descending && property.equals(other.property);
         }
         return false;
     }
diff --git a/storage/sis-sqlstore/src/main/java/org/apache/sis/internal/sql/feature/FeatureIterator.java b/storage/sis-sqlstore/src/main/java/org/apache/sis/internal/sql/feature/FeatureIterator.java
index 3f780df..af78ad2 100644
--- a/storage/sis-sqlstore/src/main/java/org/apache/sis/internal/sql/feature/FeatureIterator.java
+++ b/storage/sis-sqlstore/src/main/java/org/apache/sis/internal/sql/feature/FeatureIterator.java
@@ -34,6 +34,7 @@ import org.apache.sis.util.collection.WeakValueHashMap;
 import org.opengis.feature.Feature;
 import org.opengis.filter.SortOrder;
 import org.opengis.filter.SortProperty;
+import org.opengis.filter.SortBy;
 
 
 /**
@@ -99,12 +100,12 @@ final class FeatureIterator implements Spliterator<Feature>, AutoCloseable {
      * @param connection  connection to the database, used for creating the statement.
      * @param distinct    whether the set should contain distinct feature instances.
      * @param filter      condition to append, not including the {@code WHERE} keyword.
-     * @param sort        the {@code ORDER BY} clauses, or {@code null} or empty if none.
+     * @param sort        the {@code ORDER BY} clauses, or {@code null} if none.
      * @param offset      number of rows to skip in underlying SQL query, or ≤ 0 for none.
      * @param count       maximum number of rows to return, or ≤ 0 for no limit.
      */
     FeatureIterator(final Table table, final Connection connection,
-             final boolean distinct, final String filter, final SortProperty[] sort,
+             final boolean distinct, final String filter, final SortBy<? super Feature> sort,
              final long offset, final long count)
             throws SQLException, InternalDataStoreException
     {
@@ -120,7 +121,7 @@ final class FeatureIterator implements Spliterator<Feature>, AutoCloseable {
             }
             if (sort != null) {
                 String separator = " ORDER BY ";
-                for (final SortProperty s : sort) {
+                for (final SortProperty<? super Feature> s : sort.getSortProperties()) {
                     builder.append(separator).appendIdentifier(s.getValueReference().getXPath());
                     final SortOrder order = s.getSortOrder();
                     if (order != null) {
diff --git a/storage/sis-sqlstore/src/main/java/org/apache/sis/internal/sql/feature/FeatureStream.java b/storage/sis-sqlstore/src/main/java/org/apache/sis/internal/sql/feature/FeatureStream.java
index cb76791..f7f734d 100644
--- a/storage/sis-sqlstore/src/main/java/org/apache/sis/internal/sql/feature/FeatureStream.java
+++ b/storage/sis-sqlstore/src/main/java/org/apache/sis/internal/sql/feature/FeatureStream.java
@@ -35,12 +35,11 @@ import org.apache.sis.internal.storage.query.SortByComparator;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.util.collection.BackingStoreException;
 import org.apache.sis.util.ArgumentChecks;
-import org.apache.sis.util.ArraysExt;
 
 // Branch-dependent imports
 import org.opengis.feature.Feature;
 import org.opengis.filter.Filter;
-import org.opengis.filter.SortProperty;
+import org.opengis.filter.SortBy;
 
 
 /**
@@ -98,9 +97,9 @@ final class FeatureStream extends DeferredStream<Feature> {
     private boolean distinct;
 
     /**
-     * The {@code ORDER BY} clauses, or {@code null} or empty if none.
+     * The {@code ORDER BY} clauses, or {@code null} if none.
      */
-    private SortProperty[] sort;
+    private SortBy<? super Feature> sort;
 
     /**
      * Number of rows to skip in underlying SQL query, or 0 for none.
@@ -243,13 +242,17 @@ final class FeatureStream extends DeferredStream<Feature> {
     public Stream<Feature> sorted(final Comparator<? super Feature> comparator) {
         if (isPagined() || hasComparator) {
             return delegate().sorted(comparator);
-        } else if (comparator instanceof SortByComparator) {
-            sort = ArraysExt.concatenate(sort, ((SortByComparator) comparator).orders);
+        }
+        if (sort == null && comparator instanceof SortBy<?>) {
+            sort = (SortBy<? super Feature>) comparator;
+            return this;
+        }
+        if (sort instanceof SortByComparator && comparator instanceof SortByComparator) {
+            sort = new SortByComparator((SortByComparator) sort, (SortByComparator) comparator);
             return this;
-        } else {
-            hasComparator = true;
-            return super.sorted(comparator);
         }
+        hasComparator = true;
+        return super.sorted(comparator);
     }
 
     /**
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/FeatureQuery.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/FeatureQuery.java
index d09efba..32926a5 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/FeatureQuery.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/FeatureQuery.java
@@ -43,6 +43,7 @@ import org.opengis.filter.Filter;
 import org.opengis.filter.Expression;
 import org.opengis.filter.Literal;
 import org.opengis.filter.ValueReference;
+import org.opengis.filter.SortBy;
 import org.opengis.filter.SortProperty;
 import org.opengis.filter.InvalidFilterValueException;
 
@@ -65,11 +66,6 @@ public class FeatureQuery extends Query implements Cloneable {
     public static final long UNLIMITED = -1;
 
     /**
-     * The value of {@link #sortBy} when no sorting is applied.
-     */
-    private static final SortProperty[] UNSORTED = new SortProperty[0];
-
-    /**
      * The properties to retrieve, or {@code null} if all properties shall be included in the query.
      * In a database, "properties" are table columns.
      * Subset of columns is called <cite>projection</cite> in relational database terminology.
@@ -111,11 +107,9 @@ public class FeatureQuery extends Query implements Cloneable {
      * The expressions to use for sorting the feature instances.
      *
      * @see #getSortBy()
-     * @see #setSortBy(SortProperty...)
-     *
-     * @todo Should be an instance of {@link org.opengis.filter.SortBy}.
+     * @see #setSortBy(SortBy)
      */
-    private SortProperty[] sortBy;
+    private SortBy<Feature> sortBy;
 
     /**
      * Hint used by resources to optimize returned features.
@@ -132,8 +126,7 @@ public class FeatureQuery extends Query implements Cloneable {
      */
     public FeatureQuery() {
         selection = Filter.include();
-        sortBy = UNSORTED;
-        limit  = UNLIMITED;
+        limit = UNLIMITED;
     }
 
     /**
@@ -255,29 +248,37 @@ public class FeatureQuery extends Query implements Cloneable {
      * returned by the {@link org.apache.sis.storage.FeatureSet}.
      * {@code SortBy} clauses are applied in declaration order, like SQL.
      *
-     * @param  sortBy  expressions to use for sorting the feature instances.
+     * @param  properties  expressions to use for sorting the feature instances,
+     *                     or {@code null} or an empty array if none.
      */
-    @SuppressWarnings("AssignmentToCollectionOrArrayFieldFromParameter")
-    public void setSortBy(SortProperty... sortBy) {
-        if (sortBy == null || sortBy.length == 0) {
-            sortBy = UNSORTED;
-        } else {
-            sortBy = sortBy.clone();
-            for (int i=0; i < sortBy.length; i++) {
-                ArgumentChecks.ensureNonNullElement("sortBy", i, sortBy[i]);
-            }
+    @SafeVarargs
+    public final void setSortBy(final SortProperty<Feature>... properties) {
+        SortBy<Feature> sortBy = null;
+        if (properties != null && properties.length != 0) {
+            sortBy = new SortByComparator(properties);
         }
+        setSortBy(sortBy);
+    }
+
+    /**
+     * Sets the expressions to use for sorting the feature instances.
+     * {@code SortBy} objects are used to order the {@link org.opengis.feature.Feature} instances
+     * returned by the {@link org.apache.sis.storage.FeatureSet}.
+     *
+     * @param  sortBy  expressions to use for sorting the feature instances, or {@code null} if none.
+     */
+    public void setSortBy(final SortBy<Feature> sortBy) {
         this.sortBy = sortBy;
     }
 
     /**
      * Returns the expressions to use for sorting the feature instances.
-     * They are the values specified in the last call to {@link #setSortBy(SortBy...)}.
+     * They are the values specified in the last call to {@link #setSortBy(SortBy)}.
      *
-     * @return expressions to use for sorting the feature instances, or an empty array if none.
+     * @return expressions to use for sorting the feature instances, or {@code null} if none.
      */
-    public SortProperty[] getSortBy() {
-        return (sortBy.length == 0) ? UNSORTED : sortBy.clone();
+    public SortBy<Feature> getSortBy() {
+        return sortBy;
     }
 
     /**
@@ -498,7 +499,7 @@ public class FeatureQuery extends Query implements Cloneable {
     @Override
     public int hashCode() {
         return 97 * Arrays.hashCode(projection) + 31 * selection.hashCode()
-              + 7 * Arrays.hashCode(sortBy) + Long.hashCode(limit ^ skip)
+              + 7 * Objects.hashCode(sortBy) + Long.hashCode(limit ^ skip)
               + 3 * Objects.hashCode(linearResolution);
     }
 
@@ -518,8 +519,8 @@ public class FeatureQuery extends Query implements Cloneable {
             return skip  == other.skip &&
                    limit == other.limit &&
                    selection.equals(other.selection) &&
-                   Arrays.equals(projection, other.projection) &&
-                   Arrays.equals(sortBy,  other.sortBy)  &&
+                   Arrays .equals(projection,       other.projection) &&
+                   Objects.equals(sortBy,           other.sortBy) &&
                    Objects.equals(linearResolution, other.linearResolution);
         }
         return false;
@@ -545,11 +546,11 @@ public class FeatureQuery extends Query implements Cloneable {
         if (selection != Filter.include()) {
             sb.append(" WHERE ").append(selection);
         }
-        if (sortBy != UNSORTED) {
-            sb.append(" ORDER BY ");
-            for (int i=0; i<sortBy.length; i++) {
-                if (i != 0) sb.append(", ");
-                final SortProperty p = sortBy[i];
+        if (sortBy != null) {
+            String separator = " ORDER BY ";
+            for (final SortProperty<Feature> p : sortBy.getSortProperties()) {
+                sb.append(separator);
+                separator = ", ";
                 sb.append(p.getValueReference().getXPath()).append(' ').append(p.getSortOrder());
             }
         }
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/FeatureSubset.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/FeatureSubset.java
index de2b664..2452752 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/FeatureSubset.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/FeatureSubset.java
@@ -31,7 +31,7 @@ import org.opengis.feature.Feature;
 import org.opengis.feature.FeatureType;
 import org.opengis.filter.Filter;
 import org.opengis.filter.Expression;
-import org.opengis.filter.SortProperty;
+import org.opengis.filter.SortBy;
 
 
 /**
@@ -105,9 +105,9 @@ final class FeatureSubset extends AbstractFeatureSet {
         /*
          * Apply sorting.
          */
-        final SortProperty[] sortBy = query.getSortBy();
-        if (sortBy.length > 0) {
-            stream = stream.sorted(new SortByComparator(sortBy));
+        final SortBy<Feature> sortBy = query.getSortBy();
+        if (sortBy != null) {
+            stream = stream.sorted(sortBy);
         }
         /*
          * Apply offset.
diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/SortByComparator.java b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/SortByComparator.java
index d97d7ea..6d4a010 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/SortByComparator.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/query/SortByComparator.java
@@ -16,113 +16,85 @@
  */
 package org.apache.sis.internal.storage.query;
 
-import java.util.Collections;
-import java.util.Comparator;
-import java.util.Iterator;
-import org.apache.sis.util.collection.Containers;
+import java.util.List;
+import java.io.Serializable;
+import org.apache.sis.util.ArraysExt;
+import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.internal.util.UnmodifiableArrayList;
 
 // Branch-dependent imports
 import org.opengis.feature.Feature;
-import org.opengis.filter.SortOrder;
-import org.opengis.filter.Expression;
+import org.opengis.filter.SortBy;
 import org.opengis.filter.SortProperty;
 
 
 /**
- * Comparator sorting features by an array of {@code SortBy} expressions, applied in order.
+ * Comparator sorting features using an array of {@link SortProperty} elements applied in order.
+ * This is restricted to comparator of {@link Feature} instances for now because this is the only
+ * comparator that we currently need, and it makes {@linkplain #SortByComparator(SortByComparator,
+ * SortByComparator) concatenations} type-safe. We may generalize in the future if needed.
  *
  * @author  Johann Sorel (Geomatys)
  * @author  Martin Desruisseaux (Geomatys)
  * @version 1.1
  * @since   1.0
  * @module
- *
- * @todo Current implementation has unchecked casts. Fixing that may require a revision of filter interfaces.
- *       See <a href="https://github.com/opengeospatial/geoapi/issues/32">GeoAPI issue #32</a>.
  */
-public final class SortByComparator implements Comparator<Feature> {
-    /**
-     * The sort order specified to the constructor.
-     * Not used by this class, but provided for allowing optimizer
-     * to decompose this comparator into its component properties.
-     */
-    public final SortProperty[] orders;
-
+public final class SortByComparator implements SortBy<Feature>, Serializable {
     /**
-     * The expression to evaluate for getting the values to sort.
+     * For cross-version compatibility.
      */
-    private final Expression<?,?>[] properties;
+    private static final long serialVersionUID = -7964849249532212389L;
 
     /**
-     * {@code false} for ascending order, {@code true} for descending order.
-     * If unspecified or unknown, we assume ascending order.
+     * The sort order specified to the constructor.
+     *
+     * @see #getSortProperties()
      */
-    private final boolean[] descending;
+    private final SortProperty<Feature>[] properties;
 
     /**
      * Creates a new comparator for the given sort expressions.
      * It is caller responsibility to ensure that the given array is non-empty.
      */
-    SortByComparator(final SortProperty[] orders) {
-        this.orders = orders;
-        properties  = new Expression[orders.length];
-        descending  = new boolean   [orders.length];
-        for (int i=0; i<orders.length; i++) {
-            final SortProperty order = orders[i];
-            properties[i] = order.getValueReference();
-            descending[i] = SortOrder.DESCENDING.equals(order.getSortOrder());
+    SortByComparator(SortProperty<Feature>[] properties) {
+        properties = properties.clone();
+        this.properties = properties;
+        for (int i=0; i < properties.length; i++) {
+            ArgumentChecks.ensureNonNullElement("properties", i, properties[i]);
         }
     }
 
     /**
-     * Compares two features for order. Returns -1 if {@code f1} should be sorted before {@code f2},
-     * +1 if {@code f2} should be after {@code f1}, or 0 if both are equal. Null features are sorted
-     * after all non-null features, regardless sorting order.
+     * Creates a new comparator as the concatenation of the two given comparators.
      *
-     * @param  f1  the first feature to compare.
-     * @param  f2  the second feature to compare.
-     * @return -1 if the first feature is before the second, +1 for the converse, or 0 if equal.
+     * @param  s1  the first "sort by" to concatenate.
+     * @param  s2  the second "sort by" to concatenate.
+     */
+    public SortByComparator(final SortByComparator s1, final SortByComparator s2) {
+        properties = ArraysExt.concatenate(s1.properties, s2.properties);
+    }
+
+    /**
+     * Returns the properties whose values are used for sorting.
+     * The list shall have a minimum of one element.
      */
     @Override
-    public int compare(final Feature f1, final Feature f2) {
-        if (f1 != f2) {
-            if (f1 == null) return +1;
-            if (f2 == null) return -1;
-            for (int i=0; i<properties.length; i++) {
-                final Expression property = properties[i];
-                Object o1 = property.apply(f1);
-                Object o2 = property.apply(f2);
-                if (o1 != o2) {
-                    if (o1 == null) return +1;
-                    if (o2 == null) return -1;
-                    final int result;
-                    /*
-                     * No @SuppressWarnings("unchecked") below: those casts are really unsafe;
-                     * we can not make them safe with current Filter API. See GeoAPI issue #32.
-                     */
-                    if (o1 instanceof Iterable<?>) {
-                        result = Containers.compare(((Iterable) o1).iterator(), iterator(o2));
-                    } else if (o2 instanceof Iterable<?>) {
-                        result = Containers.compare(iterator(o1), ((Iterable) o2).iterator());
-                    } else {
-                        result = ((Comparable) o1).compareTo(o2);
-                    }
-                    if (result != 0) {
-                        return descending[i] ? -result : result;
-                    }
-                }
-            }
-        }
-        return 0;
+    public List<SortProperty<Feature>> getSortProperties() {
+        return UnmodifiableArrayList.wrap(properties);
     }
 
     /**
-     * Returns an iterator for the given object.
-     *
-     * @todo Intentionally raw return type, but no {@literal @SuppressWarning} annotation because this
-     *       is a real problem with current Filter API which needs to be fixed. See GeoAPI issue #32.
+     * Compares two resources for order. Returns a negative number if {@code r1} should be sorted before {@code r2},
+     * a positive number if {@code r2} should be after {@code r1}, or 0 if both resources are equal.
+     * The ordering of null resources or null property values is unspecified.
      */
-    private static Iterator iterator(final Object o) {
-        return (o instanceof Iterable<?>) ? ((Iterable<?>) o).iterator() : Collections.singleton(o).iterator();
+    @Override
+    public int compare(final Feature r1, final Feature r2) {
+        for (final SortProperty<Feature> p : properties) {
+            final int c = p.compare(r1, r2);
+            if (c != 0) return c;
+        }
+        return 0;
     }
 }
diff --git a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/query/SimpleQueryTest.java b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/query/SimpleQueryTest.java
index c243236..a66c759 100644
--- a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/query/SimpleQueryTest.java
+++ b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/query/SimpleQueryTest.java
@@ -136,7 +136,7 @@ public final strictfp class SimpleQueryTest extends TestCase {
     }
 
     /**
-     * Verifies the effect of {@link FeatureQuery#setSortBy(SortBy[])}.
+     * Verifies the effect of {@link FeatureQuery#setSortBy(SortProperty[])}.
      *
      * @throws DataStoreException if an error occurred while executing the query.
      */