Class Ordering

java.lang.Object
ch.tocco.nice2.persist.core.api.query.Ordering

public class Ordering extends Object
This class specifies the ordering policy of a query. It may be bound to an entity model, but it doesn't have to. If it is bound to a model, it will check that the specified fields exist. Ordering objects obtained by Query.getOrdering() are always bound. You may also order by fields of a related entity by using the dot-notation known from the query language: myRelation.myField. Note that fields of entities of a one-to-many or many-to-many relation may not be used for ordering.

This class is designed to reflect the behaviour of table ordering in GUIs.

Example: Ordering o = new Ordering(); o.append("myField", Direction.ASCENDING); o.append("myRel.otherField", Direction.valueOf("DESC"+"ENDING")); o.apply(myQeury);

  • Field Details

  • Constructor Details

    • Ordering

      public Ordering()
      Create an unbound ordering object.
    • Ordering

      public Ordering(EntityModel model)
      Create an ordering object bound to the specified entity model.
      Parameters:
      model - The model.
  • Method Details

    • of

      public static Ordering of(String... paths)
      Create a new Ordering instance with ordering rules.
      Parameters:
      paths - The fields/paths to append. All paths are appended with Ordering.Direction.ASCENDING, unless they're prefixed with a dash ('-').
      Returns:
      the new Ordering instance.
    • apply

      public void apply(Query query) throws PersistException
      Apply this ordering policy to a query in strict mode.
      Parameters:
      query - The query to apply this ordering policy to.
      Throws:
      PersistException - If an error occurs.
      See Also:
    • apply

      public void apply(Query query, boolean lenient) throws PersistException
      Apply this ordering policy to a query. The lenient flag specifies, how unknown or invalid fields will be handled: If lenient is set to true, unknown or invalid fields will simply be discarded, if it's set to false, an exception will be thrown.
      Parameters:
      query - The query to apply this ordering policy to.
      lenient - true, for applying the ordering policy leniently, false for applying it strictly.
      Throws:
      PersistException - If an error occurs or, if applying strictly, an unknown or invalid field was encountered.
    • clear

      public Ordering clear()
      Clear the ordering policy.
    • isEmpty

      public boolean isEmpty()
      Check whether the ordering policy is empty.
      Returns:
      true, if the ordering policy is empty.
    • size

      public int size()
      Get the size (field count) of this ordering policy.
      Returns:
      The field count.
    • truncate

      public boolean truncate(int maxSize)
      Truncate the ordering policy to the specified size.
      Parameters:
      maxSize - The maximum size.
      Returns:
      true, if the ordering policy was truncated, false, if the policy remained unchanged.
    • append

      public Ordering append(Path path, Ordering.Direction direction) throws PersistException
      Append an ordering rule to the policy. If the policy already contains a ordering rule for the specified field/path, it will be removed before appending.
      Parameters:
      path - The field/path to append.
      direction - The ordering direction.
      Throws:
      PersistException - If an error occurred.
    • append

      public Ordering append(String path, Ordering.Direction direction) throws PersistException
      Append an ordering rule to the policy. If the policy already contains a ordering rule for the specified field/path, it will be removed before appending.
      Parameters:
      path - The field/path to append.
      direction - The ordering direction.
      Throws:
      PersistException - If an error occurred.
    • append

      public Ordering append(String... paths) throws PersistException
      Append ordering rules to the policy. If the policy already contains a ordering rule for the specified field/path, it will be removed before appending. All paths are appended with Ordering.Direction.ASCENDING, unless they're prefixed with a dash ('-').
      Parameters:
      paths - The fields/paths to append. All paths are appended with Ordering.Direction.ASCENDING, unless they're prefixed with a dash ('-').
      Throws:
      PersistException - If an error occurred.
    • prepend

      public Ordering prepend(Path path, Ordering.Direction direction) throws PersistException
      Prepend an ordering rule to the policy. If the policy already contains a ordering rule for the specified field/path, the existing will be removed before prepending.
      Parameters:
      path - The field/path to append.
      direction - The ordering direction.
      Throws:
      PersistException - If an error occurred.
    • prepend

      public Ordering prepend(String path, Ordering.Direction direction) throws PersistException
      Prepend an ordering rule to the policy. If the policy already contains a ordering rule for the specified field/path, the existing will be removed before prepending.
      Parameters:
      path - The field/path to append.
      direction - The ordering direction.
      Throws:
      PersistException - If an error occurred.
    • prepend

      public Ordering prepend(String... paths)
      Prepend ordering rules to the policy. If the policy already contains an ordering rule for the specified field/path, the existing will be removed before prepending. All paths are prepended with Ordering.Direction.ASCENDING, unless they're prefixed with a dash ('-').
      Parameters:
      paths - The fields/paths to append. All paths are appended with Ordering.Direction.ASCENDING, unless they're prefixed with a dash ('-').
      Throws:
      PersistException - If an error occurred.
    • remove

      public boolean remove(Path path)
      Remove a field/path from this ordering policy. If the policy contains no rule for the specified field/path, no action will be taken.
      Parameters:
      path - The field/path.
      Returns:
      true, if the field/path has been removed.
    • remove

      public boolean remove(String path)
      Remove a field/path from this ordering policy. If the policy contains no rule for the specified field/path, no action will be taken.
      Parameters:
      path - The field/path.
      Returns:
      true, if the field/path has been removed.
    • setDirection

      public boolean setDirection(Path path, Ordering.Direction direction)
      Set the direction of the ordering rule for the specified field/path. If the policy contains no rule for that field/path, no action will be taken.
      Parameters:
      path - The path.
      direction - The new direction.
      Returns:
      true, if the direction has been set.
    • setDirection

      public boolean setDirection(String path, Ordering.Direction direction)
      Set the direction of the ordering rule for the specified field/path. If the policy contains no rule for that field/path, no action will be taken.
      Parameters:
      path - The path.
      direction - The new direction.
      Returns:
      true, if the direction has been set.
    • getRules

      public Ordering.Rule[] getRules()
      Get an array of the current ordering rules.
      Returns:
      An array of the current ordering rules.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object