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 CmsListItemDefaultComparator implements I_CmsListItemComparator {
045
046    /**
047     * Default Constructor.<p>
048     */
049    public CmsListItemDefaultComparator() {
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({"rawtypes", "unchecked"})
066            public int compare(CmsListItem o1, CmsListItem o2) {
067
068                if (o1 == o2) {
069                    return 0;
070                }
071                Comparable c1 = (Comparable)o1.get(columnId);
072                Comparable c2 = (Comparable)o2.get(columnId);
073                if ((c1 instanceof String) && (c2 instanceof String)) {
074                    return collator.compare(c1, c2);
075                } else if (c1 != null) {
076                    if (c2 == null) {
077                        return 1;
078                    }
079                    return c1.compareTo(c2);
080                } else if (c2 != null) {
081                    return -1;
082                }
083                return 0;
084            }
085        };
086    }
087}