001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util.comparator;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.model.PortletCategory;
019    
020    import java.io.Serializable;
021    
022    import java.text.Collator;
023    
024    import java.util.Comparator;
025    import java.util.Locale;
026    
027    /**
028     * @author Brian Wing Shun Chan
029     */
030    public class PortletCategoryComparator
031            implements Comparator<PortletCategory>, Serializable {
032    
033            public PortletCategoryComparator(Locale locale) {
034                    _locale = locale;
035            }
036    
037            @Override
038            public int compare(
039                    PortletCategory portletCategory1, PortletCategory portletCategory2) {
040    
041                    String name1 = portletCategory1.getName();
042    
043                    if (name1.equals("category.highlighted")) {
044                            return -1;
045                    }
046    
047                    String name2 = portletCategory2.getName();
048    
049                    if (name2.equals("category.highlighted")) {
050                            return 1;
051                    }
052    
053                    Collator collator = Collator.getInstance(_locale);
054    
055                    name1 = LanguageUtil.get(_locale, name1);
056                    name2 = LanguageUtil.get(_locale, name2);
057    
058                    return collator.compare(name1, name2);
059            }
060    
061            private final Locale _locale;
062    
063    }