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, compare to numbers
009     */
010    public final class NumberComparator implements ExceptionComparator {
011    
012            private boolean isAsc;
013            private PageException pageException=null;
014    
015            /**
016             * constructor of the class 
017             * @param isAsc is ascendinf or descending
018             */
019            public NumberComparator(boolean isAsc) {
020                    this.isAsc=isAsc;
021            }
022            
023            /**
024             * @return Returns the pageException.
025             */
026            public PageException getPageException() {
027                    return pageException;
028            }
029            
030            /**
031             * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
032             */
033            public int compare(Object oLeft, Object oRight) {
034                    try {
035                            if(pageException!=null) return 0;
036                            else if(isAsc) return compareObjects(oLeft, oRight);
037                            else return compareObjects(oRight, oLeft);
038                    } catch (PageException e) {
039                            pageException=e;
040                            return 0;
041                    }
042            }
043            
044            private int compareObjects(Object oLeft, Object oRight) throws PageException {
045                    double left=Caster.toDoubleValue(oLeft);
046                    double right=Caster.toDoubleValue(oRight);
047                    if(left < right)return -1;
048                    return left > right ? 1 : 0;
049                    
050            }
051    }