001
014
015 package com.liferay.portal.kernel.util;
016
017 import com.liferay.portal.kernel.bean.BeanPropertiesUtil;
018
019 import java.io.Serializable;
020
021 import java.util.Comparator;
022
023
027 public abstract class OrderByComparator implements Comparator, Serializable {
028
029 public abstract int compare(Object obj1, Object obj2);
030
031 public String getOrderBy() {
032 return null;
033 }
034
035 public String[] getOrderByFields() {
036 String orderBy = getOrderBy();
037
038 if (orderBy == null) {
039 return null;
040 }
041
042 String[] parts = StringUtil.split(orderBy);
043
044 String[] fields = new String[parts.length];
045
046 for (int i = 0; i < parts.length; i++) {
047 String part = parts[i];
048
049 int x = part.indexOf(CharPool.PERIOD);
050 int y = part.indexOf(CharPool.SPACE, x);
051
052 if (y == -1) {
053 y = part.length();
054 }
055
056 fields[i] = part.substring(x + 1, y);
057 }
058
059 return fields;
060 }
061
062 public Object[] getOrderByValues(Object obj) {
063 String[] fields = getOrderByFields();
064
065 Object[] values = new Object[fields.length];
066
067 for (int i = 0; i< fields.length; i++) {
068 values[i] = BeanPropertiesUtil.getObject(obj, fields[i]);
069 }
070
071 return values;
072 }
073
074 public boolean isAscending() {
075 String orderBy = getOrderBy();
076
077 if ((orderBy == null) ||
078 (orderBy.toUpperCase().endsWith(_ORDER_BY_DESC))) {
079
080 return false;
081 }
082 else {
083 return true;
084 }
085 }
086
087 public String toString() {
088 return getOrderBy();
089 }
090
091 private static final String _ORDER_BY_DESC = " DESC";
092
093 }