1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.kernel.util;
16  
17  import java.io.Serializable;
18  
19  import java.util.Comparator;
20  
21  /**
22   * <a href="OrderByComparator.java.html"><b><i>View Source</i></b></a>
23   *
24   * @author Brian Wing Shun Chan
25   */
26  public abstract class OrderByComparator implements Comparator, Serializable {
27  
28      public abstract int compare(Object obj1, Object obj2);
29  
30      public String getOrderBy() {
31          return null;
32      }
33  
34      public String[] getOrderByFields() {
35          String orderBy = getOrderBy();
36  
37          if (orderBy == null) {
38              return null;
39          }
40  
41          String[] parts = StringUtil.split(orderBy);
42  
43          String[] fields = new String[parts.length];
44  
45          for (int i = 0; i < parts.length; i++) {
46              String part = parts[i];
47  
48              int x = part.indexOf(StringPool.PERIOD);
49              int y = part.indexOf(StringPool.SPACE, x);
50  
51              if (y == -1) {
52                  y = part.length();
53              }
54  
55              fields[i] = part.substring(x + 1, y);
56          }
57  
58          return fields;
59      }
60  
61      public boolean isAscending() {
62          String orderBy = getOrderBy();
63  
64          if ((orderBy == null) || orderBy.toUpperCase().endsWith(" DESC")) {
65              return false;
66          }
67          else {
68              return true;
69          }
70      }
71  
72      public String toString() {
73          return getOrderBy();
74      }
75  
76  }