001    package railo.runtime.type.comparator;
002    
003    import railo.runtime.exp.PageException;
004    import railo.runtime.op.Caster;
005    
006    
007    /**
008     * comparator implementation to compare textes
009     */
010    public final class TextComparator implements ExceptionComparator {
011            
012            private boolean isAsc;
013            private boolean ignoreCase;
014            private PageException pageException=null;
015    
016            /**
017             * constructor of the class
018             * @param isAsc ascending or desending
019             * @param ignoreCase ignore case or not
020             */
021            public TextComparator(boolean isAsc, boolean ignoreCase) {
022                    this.isAsc=isAsc;
023                    this.ignoreCase=ignoreCase;
024            }
025            
026            /**
027             * @return Returns the expressionException.
028             */
029            public PageException getPageException() {
030                    return pageException;
031            }
032            
033            /**
034             * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
035             */
036            public int compare(Object oLeft, Object oRight) {
037                    try {
038                            if(pageException!=null) return 0;
039                            else if(isAsc) return compareObjects(oLeft, oRight);
040                            else return compareObjects(oRight, oLeft);
041                    } catch (PageException e) {
042                            pageException=e;
043                            return 0;
044                    }
045            }
046            
047            private int compareObjects(Object oLeft, Object oRight) throws PageException {
048                    if(ignoreCase)return Caster.toString(oLeft).compareToIgnoreCase(Caster.toString(oRight));
049                    return Caster.toString(oLeft).compareTo(Caster.toString(oRight));
050            }
051    
052    }