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.portlet.dynamicdatamapping.util.comparator;
016    
017    import com.liferay.portal.kernel.util.LocaleThreadLocal;
018    import com.liferay.portlet.dynamicdatamapping.model.DDMStructure;
019    
020    import java.text.Collator;
021    
022    import java.util.Comparator;
023    import java.util.Locale;
024    
025    /**
026     * @author Alberto Chaparro
027     * @author Mariano ??lvaro
028     */
029    public class DDMStructureNameComparator implements Comparator<DDMStructure> {
030    
031            public DDMStructureNameComparator() {
032                    this(true, LocaleThreadLocal.getSiteDefaultLocale());
033            }
034    
035            public DDMStructureNameComparator(Locale locale) {
036                    this(true, locale);
037            }
038    
039            public DDMStructureNameComparator(boolean ascending, Locale locale) {
040                    _ascending = ascending;
041                    _locale = locale;
042            }
043    
044            @Override
045            public int compare(DDMStructure DDMStructure1, DDMStructure DDMStructure2) {
046                    Collator collator = Collator.getInstance(_locale);
047    
048                    int value = collator.compare(
049                            DDMStructure1.getName(_locale), DDMStructure2.getName(_locale));
050    
051                    if (_ascending) {
052                            return value;
053                    }
054                    else {
055                            return - value;
056                    }
057            }
058    
059            private final boolean _ascending;
060            private final Locale _locale;
061    
062    }