001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.runtime.type.comparator;
020
021import lucee.runtime.exp.PageException;
022import lucee.runtime.op.Caster;
023import lucee.runtime.op.Operator;
024
025
026/**
027 * Implementation of a Comparator, compares to Softregister Objects
028 */
029public final class NumberSortRegisterComparator implements ExceptionComparator {
030        
031        private boolean isAsc;
032        private PageException pageException=null;
033
034        /**
035         * constructor of the class 
036         * @param isAsc is ascendinf or descending
037         */
038        public NumberSortRegisterComparator(boolean isAsc) {
039        
040                this.isAsc=isAsc;
041        }
042        
043        /**
044         * @return Returns the expressionException.
045         */
046        public PageException getPageException() {
047                return pageException;
048        }
049        
050        @Override
051        public int compare(Object oLeft, Object oRight) {
052                try {
053                        if(pageException!=null) return 0;
054                        else if(isAsc) return compareObjects(oLeft, oRight);
055                        else return compareObjects(oRight, oLeft);
056                } catch (PageException e) {
057                        pageException=e;
058                        return 0;
059                }
060        }
061        
062        private int compareObjects(Object oLeft, Object oRight) throws PageException {
063        /*return Operator.compare(
064                ((SortRegister)oLeft).getValue(),
065                ((SortRegister)oRight).getValue()
066        );
067        */
068        return Operator.compare(
069                Caster.toDoubleValue(((SortRegister)oLeft).getValue())
070                ,
071                Caster.toDoubleValue(((SortRegister)oRight).getValue())
072        );
073        
074        }
075
076}