Class PropertyComparator

java.lang.Object
org.apache.sis.metadata.PropertyComparator
All Implemented Interfaces:
Comparator<Method>

final class PropertyComparator extends Object implements Comparator<Method>
The comparator for sorting the properties in a metadata object. Since the comparator uses (among other criteria) the property names, this class incidentally defines static methods for inferring those names from the methods.

This comparator uses the following criteria, in priority order:

  1. Deprecated properties are last.
  2. If the property order is specified by a XmlType annotation, then this comparator complies to that order.
  3. Otherwise this comparator sorts mandatory methods first, followed by conditional methods, then optional ones.
  4. If the order cannot be inferred from the above, then the comparator fallbacks on alphabetical order.
Since:
0.3
Version:
1.0
  • Field Details

    • IS

      private static final String IS
      The prefix for getters on boolean values.
      See Also:
    • GET

      private static final String GET
      The prefix for getters (general case).
      See Also:
    • SET

      static final String SET
      The prefix for setters.
      See Also:
    • order

      private final Map<Object,Integer> order
      Methods and property names specified in the XmlType annotation. Entries description:
      • Keys in this map are either String or Method instances:
        • String keys property names as given by XmlType.propOrder(). They are computed at construction time and do not change after construction.
        • Method keys will be added after construction, only as needed.
      • Key is associated to an index that specify its position in descending order. For example, the property associated to integer 0 shall be sorted last. This descending order is only an implementation convenience.
    • implementation

      private final Class<?> implementation
      The implementation class, or the interface is the implementation class is unknown.
  • Constructor Details

    • PropertyComparator

      PropertyComparator(Class<?> implementation, Class<?> standardImpl)
      Creates a new comparator for the given implementation class.
      Parameters:
      implementation - the implementation class, or the interface if the implementation class is unknown.
      standardImpl - the implementation specified by the MetadataStandard, or null if none. This is the same than implementation unless a custom implementation is used.
  • Method Details

    • defineOrder

      private static void defineOrder(Class<?> implementation, Map<Object,Integer> order)
      Uses the XmlType annotation for defining the property order.
      Parameters:
      implementation - the implementation class where to search for XmlType annotation.
      order - the order map where to store the properties order.
    • isDeprecated

      static boolean isDeprecated(Class<?> implementation, Method method)
      Returns true if the given method is deprecated, either in the interface that declare the method or in the implementation class. A method may be deprecated in the implementation but not in the interface when the implementation has been updated for a new standard, while the interface is still reflecting the old standard.
      Parameters:
      implementation - the implementation class, or the interface is the implementation class is unknown.
      method - the method to check for deprecation.
      Returns:
      true if the method is deprecated.
    • compare

      public int compare(Method m1, Method m2)
      Compares the given methods for order.
      Specified by:
      compare in interface Comparator<Method>
    • order

      private static int order(org.opengis.annotation.UML uml)
      Returns a higher number for obligation which should be first.
    • indexOf

      private int indexOf(Method method)
      Returns the index of the given method, or -1 if the method is not found. If positive, the index returned by this method correspond to a sorting in descending order.
    • prefix

      static String prefix(String name)
      Returns the prefix of the specified method name. If the method name doesn't starts with a prefix (for example ConformanceResult.pass()), then this method returns an empty string.
    • isAcronym

      private static boolean isAcronym(String name, int offset)
      Returns true if the specified string starting at the specified index contains no lower case characters. The characters don't have to be in upper case however (e.g. non-alphabetic characters)
    • toPropertyName

      static String toPropertyName(String name, int base)
      Removes the "get" or "is" prefix and turn the first character after the prefix into lower case. For example, the method name "getTitle" will be replaced by the property name "title". We will perform this operation only if there is at least 1 character after the prefix.
      Parameters:
      name - the method name (cannot be null).
      base - must be the result of prefix(name).length().
      Returns:
      the property name (never null).