Package com.dooapp.gaedo.finders

Source Code of com.dooapp.gaedo.finders.SortingExpression

package com.dooapp.gaedo.finders;

import java.util.Map.Entry;

import com.dooapp.gaedo.exceptions.UncomparableObjectsInSortingException;
import com.dooapp.gaedo.finders.sort.SortingExpressionImpl;
import com.dooapp.gaedo.finders.sort.SortingExpressionVisitor;
import com.dooapp.gaedo.patterns.Visitable;

/**
* Define a sorting mode
* @author ndx
*
*/
public interface SortingExpression extends Iterable<Entry<FieldInformer, SortingExpression.Direction>>, Visitable<SortingExpressionVisitor>{
  /**
   * An easy builder pattern for sorting expressions
   * @author ndx
   *
   */
  public static class Build {
    public static SortingExpression sort() {
      return new SortingExpressionImpl();
    }
  }
  /**
   * The sorting direction for a given field
   * @author ndx
   *
   */
  public static enum Direction {
    Ascending("Ascending"),
    Descending("Descending");
   
    /**
     * Text associated to direction. This text can be used in dynamic finders code
     */
    private final String text;

    public String getText() {
      return text;
    }

    private Direction(String name) {
      this.text = name;
    }

    /**
     * Compare objects and return a result according to direction
     * @param firstValue first compared value
     * @param secondValue second compared value
     * @return comparison result according to this ordering, or an exception if anything failed (a null somewhere, or a non comparable object)
     */
    public int compareTo(Object firstValue, Object secondValue) {
      switch(this) {
      case Ascending:
        if(firstValue instanceof Comparable)
          return ((Comparable) firstValue).compareTo(secondValue);
        else if(secondValue instanceof Comparable)
          return -1*((Comparable) secondValue).compareTo(firstValue);
        throw new UncomparableObjectsInSortingException("nor firstValue "+firstValue+" neither secondValue "+secondValue+" is Comparable !");
      case Descending:
        if(firstValue instanceof Comparable)
          return -1*((Comparable) firstValue).compareTo(secondValue);
        else if(secondValue instanceof Comparable)
          return ((Comparable) secondValue).compareTo(firstValue);
        throw new UncomparableObjectsInSortingException("nor firstValue "+firstValue+" neither secondValue "+secondValue+" is Comparable !");
      }
      throw new UnsupportedOperationException("this cas is unknown to Direction ("+this+")");
    }
  }

  /**
   * Adds a sorting directive for a given field
   * @param informer informer for field
   * @param direction direction for field
   */
  public SortingExpression add(FieldInformer informer, Direction direction);
 
  /**
   * Adds an ascending sorting directive for the given informer
   * @param informer
   * @return
   */
  public SortingExpression withAscending(FieldInformer informer);
 
  /**
   * Adds a descending sorting directive for the given informer
   * @param informer
   * @return
   */
  public SortingExpression withDescending(FieldInformer informer);
}
TOP

Related Classes of com.dooapp.gaedo.finders.SortingExpression

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.
ateElement(o), m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m) })(window,document,'script','//www.google-analytics.com/analytics.js','ga'); ga('create', 'UA-20639858-1', 'auto'); ga('send', 'pageview');