001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.util.comparator;
016    
017    import com.liferay.portal.kernel.util.LocaleUtil;
018    import com.liferay.portal.kernel.util.OrderByComparator;
019    import com.liferay.portal.model.Group;
020    
021    import java.text.Collator;
022    
023    import java.util.Locale;
024    
025    /**
026     * @author Brian Wing Shun Chan
027     */
028    public class GroupNameComparator extends OrderByComparator {
029    
030            public static final String ORDER_BY_ASC = "groupName ASC";
031    
032            public static final String ORDER_BY_DESC = "groupName DESC";
033    
034            public static final String[] ORDER_BY_FIELDS = {"groupName"};
035    
036            public GroupNameComparator() {
037                    this(false);
038            }
039    
040            public GroupNameComparator(boolean ascending) {
041                    this(ascending, LocaleUtil.getDefault());
042            }
043    
044            public GroupNameComparator(boolean ascending, Locale locale) {
045                    _ascending = ascending;
046    
047                    _collator = Collator.getInstance(locale);
048            }
049    
050            @Override
051            public int compare(Object obj1, Object obj2) {
052                    Group group1 = (Group)obj1;
053                    Group group2 = (Group)obj2;
054    
055                    String name1 = group1.getName();
056                    String name2 = group2.getName();
057    
058                    int value = _collator.compare(name1, name2);
059    
060                    if (_ascending) {
061                            return value;
062                    }
063                    else {
064                            return -value;
065                    }
066            }
067    
068            @Override
069            public String getOrderBy() {
070                    if (_ascending) {
071                            return ORDER_BY_ASC;
072                    }
073                    else {
074                            return ORDER_BY_DESC;
075                    }
076            }
077    
078            @Override
079            public String[] getOrderByFields() {
080                    return ORDER_BY_FIELDS;
081            }
082    
083            @Override
084            public boolean isAscending() {
085                    return _ascending;
086            }
087    
088            private boolean _ascending;
089            private final Collator _collator;
090    
091    }