001    /**
002     * Copyright (c) 2000-2013 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.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    }