All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.mentabean.util.OrderBy Maven / Gradle / Ivy

There is a newer version: 2.2.4
Show newest version
package org.mentabean.util;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

public class OrderBy {
	
	public static enum SortOrder { ASC, DESC };
	
	private final Map fields = new LinkedHashMap();
	
	public OrderBy orderByAsc(String field) {
		
		if (field == null) {
			return orderByAsc((Object) null);
		}
		
		fields.put(field, SortOrder.ASC);
		
		return this;
	}
	
	public OrderBy orderByAsc(Object field) {
		return orderByAsc(PropertiesProxy.getPropertyName());
	}
	
	public OrderBy orderByDesc(String field) {
		
		if (field == null) {
			return orderByDesc((Object) null);
		}
		
		fields.put(field, SortOrder.DESC);
		
		return this;
	}
	
	public OrderBy orderByDesc(Object field) {
		return orderByDesc(PropertiesProxy.getPropertyName());
	}
	
	@Override
	public String toString() {
		if (fields.size() == 0) return "";
		StringBuilder sb = new StringBuilder(128);
		Iterator iter = fields.keySet().iterator();
		while(iter.hasNext()) {
			String field = iter.next();
			SortOrder so = fields.get(field);
			if (sb.length() > 0) sb.append(", ");
			sb.append(field).append(" ").append(so.toString().toLowerCase());
		}
		return sb.toString();
	}
	
	public boolean isEmpty() {
		return fields.isEmpty();
	}
	
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy