1
19
20 package com.liferay.portal.kernel.search;
21
22 import java.util.ArrayList;
23 import java.util.Comparator;
24 import java.util.List;
25
26
32 public class DocumentComparator implements Comparator<Document> {
33
34 public DocumentComparator() {
35 this(true, false);
36 }
37
38 public DocumentComparator(boolean asc, boolean caseSensitive) {
39 _asc = asc;
40 _caseSensitive = caseSensitive;
41 }
42
43 public void addOrderBy(String name) {
44 addOrderBy(name, _asc, _caseSensitive);
45 }
46
47 public void addOrderBy(String name, boolean asc, boolean caseSensitive) {
48 DocumentComparatorOrderBy orderBy = new DocumentComparatorOrderBy(
49 name, asc, 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 _asc;
86 private boolean _caseSensitive;
87 private List<DocumentComparatorOrderBy> _columns =
88 new ArrayList<DocumentComparatorOrderBy>();
89
90 }