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 @SuppressWarnings("rawtypes")
028 public abstract class OrderByComparator implements Comparator, Serializable {
029
030 public abstract int compare(Object obj1, Object obj2);
031
032 public String getOrderBy() {
033 return null;
034 }
035
036 public String[] getOrderByConditionFields() {
037 return getOrderByFields();
038 }
039
040 public Object[] getOrderByConditionValues(Object obj) {
041 String[] fields = getOrderByConditionFields();
042
043 Object[] values = new Object[fields.length];
044
045 for (int i = 0; i < fields.length; i++) {
046 values[i] = BeanPropertiesUtil.getObject(obj, fields[i]);
047 }
048
049 return values;
050 }
051
052 public String[] getOrderByFields() {
053 String orderBy = getOrderBy();
054
055 if (orderBy == null) {
056 return null;
057 }
058
059 String[] parts = StringUtil.split(orderBy);
060
061 String[] fields = new String[parts.length];
062
063 for (int i = 0; i < parts.length; i++) {
064 String part = parts[i];
065
066 int x = part.indexOf(CharPool.PERIOD);
067 int y = part.indexOf(CharPool.SPACE, x);
068
069 if (y == -1) {
070 y = part.length();
071 }
072
073 fields[i] = part.substring(x + 1, y);
074 }
075
076 return fields;
077 }
078
079 public boolean isAscending() {
080 String orderBy = getOrderBy();
081
082 if ((orderBy == null) ||
083 (orderBy.toUpperCase().endsWith(_ORDER_BY_DESC))) {
084
085 return false;
086 }
087 else {
088 return true;
089 }
090 }
091
092 public boolean isAscending(String field) {
093 return isAscending();
094 }
095
096 @Override
097 public String toString() {
098 return getOrderBy();
099 }
100
101 private static final String _ORDER_BY_DESC = " DESC";
102
103 }