001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.kernel.search;
022:
023: import java.util.ArrayList;
024: import java.util.Comparator;
025: import java.util.List;
026:
027: /**
028: * <a href="DocumentComparator.java.html"><b><i>View Source</i></b></a>
029: *
030: * @author Brian Wing Shun Chan
031: *
032: */
033: public class DocumentComparator implements Comparator {
034:
035: public DocumentComparator() {
036: this (true, false);
037: }
038:
039: public DocumentComparator(boolean asc, boolean caseSensitive) {
040: _asc = asc;
041: _caseSensitive = caseSensitive;
042: }
043:
044: public void addOrderBy(String name) {
045: addOrderBy(name, _asc, _caseSensitive);
046: }
047:
048: public void addOrderBy(String name, boolean asc,
049: boolean caseSensitive) {
050: DocumentComparatorOrderBy orderBy = new DocumentComparatorOrderBy(
051: name, asc, caseSensitive);
052:
053: _columns.add(orderBy);
054: }
055:
056: public int compare(Object obj1, Object obj2) {
057: if (!(obj1 instanceof Document)) {
058: return -1;
059: }
060:
061: if (!(obj2 instanceof Document)) {
062: return 1;
063: }
064:
065: Document doc1 = (Document) obj1;
066: Document doc2 = (Document) obj2;
067:
068: for (int i = 0; i < _columns.size(); i++) {
069: DocumentComparatorOrderBy orderBy = (DocumentComparatorOrderBy) _columns
070: .get(i);
071:
072: String value1 = doc1.get(orderBy.getName());
073: String value2 = doc2.get(orderBy.getName());
074:
075: if (!orderBy.isAsc()) {
076: String temp = value1;
077:
078: value1 = value2;
079: value2 = temp;
080: }
081:
082: int result = 0;
083:
084: if ((value1 != null) && (value2 != null)) {
085: if (orderBy.isCaseSensitive()) {
086: result = value1.compareTo(value2);
087: } else {
088: result = value1.compareToIgnoreCase(value2);
089: }
090: }
091:
092: if (result != 0) {
093: return result;
094: }
095: }
096:
097: return 0;
098: }
099:
100: private boolean _asc;
101: private boolean _caseSensitive;
102: private List _columns = new ArrayList();
103:
104: }
|