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.commons.lang; 020 021import java.text.Collator; 022import java.util.Comparator; 023import java.util.Locale; 024 025import lucee.runtime.type.comparator.NumberComparator; 026import lucee.runtime.type.comparator.TextComparator; 027 028public class ComparatorUtil { 029 030 public static final int SORT_TYPE_TEXT=1; 031 public static final int SORT_TYPE_TEXT_NO_CASE=2; 032 public static final int SORT_TYPE_NUMBER=3; 033 034 public static Comparator toComparator(int sortType, boolean orderAsc, Locale l,Comparator defaultValue) { 035 // text 036 if(sortType==SORT_TYPE_TEXT) { 037 if(l!=null)return toCollator(l,Collator.IDENTICAL); 038 return new TextComparator(orderAsc,false); 039 } 040 // text no case 041 else if(sortType==SORT_TYPE_TEXT_NO_CASE) { 042 if(l!=null)return toCollator(l,Collator.TERTIARY); 043 return new TextComparator(orderAsc,true); 044 } 045 // numeric 046 else if(sortType==SORT_TYPE_NUMBER) { 047 return new NumberComparator(orderAsc); 048 } 049 else { 050 return defaultValue; 051 } 052 } 053 054 055 private static Comparator toCollator(Locale l, int strength) { 056 Collator c=Collator.getInstance(l); 057 c.setStrength(strength); 058 c.setDecomposition(Collator.CANONICAL_DECOMPOSITION); 059 return c; 060 } 061 062}