Example usage for org.apache.commons.collections.comparators ComparatorChain compare

List of usage examples for org.apache.commons.collections.comparators ComparatorChain compare

Introduction

In this page you can find the example usage for org.apache.commons.collections.comparators ComparatorChain compare.

Prototype

public int compare(Object o1, Object o2) throws UnsupportedOperationException 

Source Link

Document

Perform comparisons on the Objects as per Comparator.compare(o1,o2).

Usage

From source file:org.openmrs.module.privilegehelper.PrivilegeLogEntry.java

/**
 * @see java.lang.Comparable#compareTo(java.lang.Object)
 *//*from  w  w w .jav a2s  . co  m*/
@Override
public int compareTo(PrivilegeLogEntry o) {
    ComparatorChain chain = new ComparatorChain();
    chain.addComparator(new Comparator<PrivilegeLogEntry>() {

        @Override
        public int compare(PrivilegeLogEntry o1, PrivilegeLogEntry o2) {
            if (o1.required == o2.required) {
                return 0;
            } else if (o1.required) {
                return 1;
            } else {
                return -1;
            }
        }
    });

    chain.addComparator(new Comparator<PrivilegeLogEntry>() {

        @Override
        public int compare(PrivilegeLogEntry o1, PrivilegeLogEntry o2) {
            if (o1.missing == o2.missing) {
                return 0;
            } else if (o1.missing) {
                return 1;
            } else {
                return -1;
            }
        }
    });

    chain.addComparator(new Comparator<PrivilegeLogEntry>() {

        @Override
        public int compare(PrivilegeLogEntry o1, PrivilegeLogEntry o2) {
            return o1.getPrivilege().compareTo(o2.getPrivilege());
        }
    }, true);

    int result = chain.compare(o, this);

    return result;
}