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 java.util.Comparator; 022 023import lucee.commons.lang.ComparatorUtil; 024import lucee.runtime.PageContext; 025import lucee.runtime.engine.ThreadLocalPageContext; 026import lucee.runtime.exp.PageException; 027import lucee.runtime.op.Caster; 028 029 030/** 031 * Implementation of a Comparator, compares to Softregister Objects 032 */ 033public final class SortRegisterComparator implements ExceptionComparator { 034 035 private boolean isAsc; 036 private PageException pageException=null; 037 private boolean ignoreCase; 038 private final Comparator comparator; 039 040 041 /** 042 * constructor of the class 043 * @param isAsc is ascending or descending 044 * @param ignoreCase do ignore case 045 */ 046 public SortRegisterComparator(PageContext pc,boolean isAsc, boolean ignoreCase, boolean localeSensitive) { 047 this.isAsc=isAsc; 048 this.ignoreCase=ignoreCase; 049 050 comparator = ComparatorUtil.toComparator( 051 ignoreCase?ComparatorUtil.SORT_TYPE_TEXT_NO_CASE:ComparatorUtil.SORT_TYPE_TEXT 052 , isAsc, localeSensitive?ThreadLocalPageContext.getLocale(pc):null, null); 053 054 } 055 056 /** 057 * @return Returns the expressionException. 058 */ 059 public PageException getPageException() { 060 return pageException; 061 } 062 063 @Override 064 public int compare(Object oLeft, Object oRight) { 065 try { 066 if(pageException!=null) return 0; 067 else if(isAsc) return compareObjects(oLeft, oRight); 068 else return compareObjects(oRight, oLeft); 069 } catch (PageException e) { 070 pageException=e; 071 return 0; 072 } 073 } 074 075 private int compareObjects(Object oLeft, Object oRight) throws PageException { 076 String strLeft=Caster.toString(((SortRegister)oLeft).getValue()); 077 String strRight=Caster.toString(((SortRegister)oRight).getValue()); 078 return comparator.compare(strLeft, strRight); 079 } 080 081}