1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portlet.bookmarks.util.comparator;
16  
17  import com.liferay.portal.kernel.util.OrderByComparator;
18  import com.liferay.portlet.bookmarks.model.BookmarksEntry;
19  
20  /**
21   * <a href="EntryVisitsComparator.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Brian Wing Shun Chan
24   */
25  public class EntryVisitsComparator extends OrderByComparator {
26  
27      public static String ORDER_BY_ASC = "visits ASC";
28  
29      public static String ORDER_BY_DESC = "visits DESC";
30  
31      public static String[] ORDER_BY_FIELDS = {"visits"};
32  
33      public EntryVisitsComparator() {
34          this(false);
35      }
36  
37      public EntryVisitsComparator(boolean asc) {
38          _asc = asc;
39      }
40  
41      public int compare(Object obj1, Object obj2) {
42          BookmarksEntry entry1 = (BookmarksEntry)obj1;
43          BookmarksEntry entry2 = (BookmarksEntry)obj2;
44  
45          int value = 0;
46  
47          if (entry1.getVisits() < entry2.getVisits()) {
48              value = -1;
49          }
50          else if (entry1.getVisits() > entry2.getVisits()) {
51              value = 1;
52          }
53  
54          if (_asc) {
55              return value;
56          }
57          else {
58              return -value;
59          }
60      }
61  
62      public String getOrderBy() {
63          if (_asc) {
64              return ORDER_BY_ASC;
65          }
66          else {
67              return ORDER_BY_DESC;
68          }
69      }
70  
71      public String[] getOrderByFields() {
72          return ORDER_BY_FIELDS;
73      }
74  
75      public boolean isAscending() {
76          return _asc;
77      }
78  
79      private boolean _asc;
80  
81  }