1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
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  /**
27   * <a href="DocumentComparator.java.html"><b><i>View Source</i></b></a>
28   *
29   * @author Brian Wing Shun Chan
30   *
31   */
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  }