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.kernel.search;
016    
017    import java.util.ArrayList;
018    import java.util.Comparator;
019    import java.util.List;
020    
021    /**
022     * @author Brian Wing Shun Chan
023     */
024    public class DocumentComparator implements Comparator<Document> {
025    
026            public DocumentComparator() {
027                    this(true, false);
028            }
029    
030            public DocumentComparator(boolean ascending, boolean caseSensitive) {
031                    _ascending = ascending;
032                    _caseSensitive = caseSensitive;
033            }
034    
035            public void addOrderBy(String name) {
036                    addOrderBy(name, _ascending, _caseSensitive);
037            }
038    
039            public void addOrderBy(String name, boolean ascending) {
040                    addOrderBy(name, ascending, _caseSensitive);
041            }
042    
043            public void addOrderBy(
044                    String name, boolean ascending, boolean caseSensitive) {
045    
046                    DocumentComparatorOrderBy orderBy = new DocumentComparatorOrderBy(
047                            name, ascending, caseSensitive);
048    
049                    _columns.add(orderBy);
050            }
051    
052            @Override
053            public int compare(Document doc1, Document doc2) {
054                    for (DocumentComparatorOrderBy orderBy : _columns) {
055                            String value1 = doc1.get(orderBy.getName());
056                            String value2 = doc2.get(orderBy.getName());
057    
058                            if (!orderBy.isAsc()) {
059                                    String temp = value1;
060    
061                                    value1 = value2;
062                                    value2 = temp;
063                            }
064    
065                            int result = 0;
066    
067                            if ((value1 != null) && (value2 != null)) {
068                                    if (orderBy.isCaseSensitive()) {
069                                            result = value1.compareTo(value2);
070                                    }
071                                    else {
072                                            result = value1.compareToIgnoreCase(value2);
073                                    }
074                            }
075    
076                            if (result != 0) {
077                                    return result;
078                            }
079                    }
080    
081                    return 0;
082            }
083    
084            private boolean _ascending;
085            private boolean _caseSensitive;
086            private List<DocumentComparatorOrderBy> _columns =
087                    new ArrayList<DocumentComparatorOrderBy>();
088    
089    }