| OrderByComparator.java |
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.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 }