1
14
15 package com.liferay.portal.kernel.search;
16
17 import java.util.ArrayList;
18 import java.util.Comparator;
19 import java.util.List;
20
21
26 public class DocumentComparator implements Comparator<Document> {
27
28 public DocumentComparator() {
29 this(true, false);
30 }
31
32 public DocumentComparator(boolean ascending, boolean caseSensitive) {
33 _ascending = ascending;
34 _caseSensitive = caseSensitive;
35 }
36
37 public void addOrderBy(String name) {
38 addOrderBy(name, _ascending, _caseSensitive);
39 }
40
41 public void addOrderBy(String name, boolean ascending) {
42 addOrderBy(name, ascending, _caseSensitive);
43 }
44
45 public void addOrderBy(
46 String name, boolean ascending, boolean caseSensitive) {
47
48 DocumentComparatorOrderBy orderBy = new DocumentComparatorOrderBy(
49 name, ascending, caseSensitive);
50
51 _columns.add(orderBy);
52 }
53
54 public int compare(Document doc1, Document doc2) {
55 for (DocumentComparatorOrderBy orderBy : _columns) {
56 String value1 = doc1.get(orderBy.getName());
57 String value2 = doc2.get(orderBy.getName());
58
59 if (!orderBy.isAsc()) {
60 String temp = value1;
61
62 value1 = value2;
63 value2 = temp;
64 }
65
66 int result = 0;
67
68 if ((value1 != null) && (value2 != null)) {
69 if (orderBy.isCaseSensitive()) {
70 result = value1.compareTo(value2);
71 }
72 else {
73 result = value1.compareToIgnoreCase(value2);
74 }
75 }
76
77 if (result != 0) {
78 return result;
79 }
80 }
81
82 return 0;
83 }
84
85 private boolean _ascending;
86 private boolean _caseSensitive;
87 private List<DocumentComparatorOrderBy> _columns =
88 new ArrayList<DocumentComparatorOrderBy>();
89
90 }