001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.workplace.list; 029 030import java.text.Collator; 031import java.util.Comparator; 032import java.util.Locale; 033 034/** 035 * Default comparator for case sensitive column sorting with string localization.<p> 036 * 037 * If both list items column values are Strings then a localized collector is used for sorting; 038 * if not, the <code>{@link Comparable}</code> interface is used.<p> 039 * 040 * @since 6.0.0 041 * 042 * @see org.opencms.workplace.list.CmsListColumnDefinition 043 */ 044public class CmsListItemNumericalComparator implements I_CmsListItemComparator { 045 046 /** 047 * Default Constructor.<p> 048 */ 049 public CmsListItemNumericalComparator() { 050 051 // no-op 052 } 053 054 /** 055 * @see org.opencms.workplace.list.I_CmsListItemComparator#getComparator(java.lang.String, java.util.Locale) 056 */ 057 public Comparator<CmsListItem> getComparator(final String columnId, final Locale locale) { 058 059 final Collator collator = Collator.getInstance(locale); 060 return new Comparator<CmsListItem>() { 061 062 /** 063 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) 064 */ 065 @SuppressWarnings({"unchecked", "rawtypes"}) 066 public int compare(CmsListItem o1, CmsListItem o2) { 067 068 Comparable c1 = (Comparable)o1.get(columnId); 069 Comparable c2 = (Comparable)o2.get(columnId); 070 if ((c1 instanceof String) && (c2 instanceof String)) { 071 return collator.compare(c1, c2); 072 } else if (c1 != null) { 073 if (c2 == null) { 074 return 1; 075 } 076 return c1.compareTo(c2); 077 } else if (c2 != null) { 078 return -1; 079 } 080 return 0; 081 } 082 }; 083 } 084}