Example usage for java.util Comparator compare

List of usage examples for java.util Comparator compare

Introduction

In this page you can find the example usage for java.util Comparator compare.

Prototype

int compare(T o1, T o2);

Source Link

Document

Compares its two arguments for order.

Usage

From source file:org.apache.hadoop.hbase.util.TestFSTableDescriptors.java

@Test
public void testFormatTableInfoSequenceId() {
    Path p0 = assertWriteAndReadSequenceId(0);
    // Assert p0 has format we expect.
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < FSTableDescriptors.WIDTH_OF_SEQUENCE_ID; i++) {
        sb.append("0");
    }//from w w w .  j a  va2 s . c  o  m
    assertEquals(FSTableDescriptors.TABLEINFO_FILE_PREFIX + "." + sb.toString(), p0.getName());
    // Check a few more.
    Path p2 = assertWriteAndReadSequenceId(2);
    Path p10000 = assertWriteAndReadSequenceId(10000);
    // Get a .tablinfo that has no sequenceid suffix.
    Path p = new Path(p0.getParent(), FSTableDescriptors.TABLEINFO_FILE_PREFIX);
    FileStatus fs = new FileStatus(0, false, 0, 0, 0, p);
    FileStatus fs0 = new FileStatus(0, false, 0, 0, 0, p0);
    FileStatus fs2 = new FileStatus(0, false, 0, 0, 0, p2);
    FileStatus fs10000 = new FileStatus(0, false, 0, 0, 0, p10000);
    Comparator<FileStatus> comparator = FSTableDescriptors.TABLEINFO_FILESTATUS_COMPARATOR;
    assertTrue(comparator.compare(fs, fs0) > 0);
    assertTrue(comparator.compare(fs0, fs2) > 0);
    assertTrue(comparator.compare(fs2, fs10000) > 0);
}

From source file:org.openvpms.web.component.im.act.ActHierarchyLister.java

/**
 * Sorts tree nodes./* w  w  w.ja v  a2s.  c  om*/
 *
 * @param nodes      the nodes to sort
 * @param comparator the comparator to sort nodes
 */
protected List<Node<T>> sort(List<Node<T>> nodes, final Comparator<T> comparator) {
    Comparator<Node<T>> nodeComparator = new Comparator<Node<T>>() {
        @Override
        public int compare(Node<T> o1, Node<T> o2) {
            return comparator.compare(o1.value, o2.value);
        }
    };
    Collections.sort(nodes, nodeComparator);
    return nodes;
}

From source file:com.xpn.xwiki.objects.ElementComparator.java

/**
 * Compares two objects (that implement ElementInterface) by name according 
 * to the rules for the compare method.//w ww.j  ava2  s.  c om
 * 
 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
 */
public int compare(Object o1, Object o2) {
    // Get a null comparator that is backed by a static "natural" comparator
    Comparator c = ComparatorUtils.nullLowComparator(null);

    // convert o1 and o2 into the string names when not null
    Object no1 = (o1 == null) ? null : ((ElementInterface) o1).getName();
    Object no2 = (o2 == null) ? null : ((ElementInterface) o2).getName();

    // let the null comparator handle possible null values, where null < non-null string
    return c.compare(no1, no2);
}

From source file:org.kuali.rice.krad.datadictionary.impl.FieldOverrideForListElementBase.java

protected int getElementPositionInList(Object object, List theList) {
    Comparator comparator = this.getComparator();
    int pos = -1;

    if (object != null && theList != null) {
        for (int i = 0; i < theList.size(); ++i) {
            Object item = theList.get(i);
            boolean equalFlag = false;
            if (comparator != null) {
                equalFlag = comparator.compare(object, item) == 0;
            } else {
                equalFlag = item.equals(object);
            }/*  w w w  .  j  a v a2s .c  om*/
            if (equalFlag) {
                pos = i;
                break;
            }
        }
    }
    return pos;
}

From source file:Main.java

public static boolean containsAll(Collection a, Collection b) {
    // fast paths
    if (a == b)//from ww w  . jav a 2 s .  c om
        return true;
    if (b.size() == 0)
        return true;
    if (a.size() < b.size())
        return false;

    if (a instanceof SortedSet && b instanceof SortedSet) {
        SortedSet aa = (SortedSet) a;
        SortedSet bb = (SortedSet) b;
        Comparator bbc = bb.comparator();
        Comparator aac = aa.comparator();
        if (bbc == null && aac == null) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Comparable ao = (Comparable) ai.next(); // these are ok, since the sizes are != 0
            Comparable bo = (Comparable) bi.next();
            while (true) {
                int rel = ao.compareTo(bo);
                if (rel == 0) {
                    if (!bi.hasNext())
                        return true;
                    if (!ai.hasNext())
                        return false;
                    bo = (Comparable) bi.next();
                    ao = (Comparable) ai.next();
                } else if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = (Comparable) ai.next();
                } else {
                    return false;
                }
            }
        } else if (bbc.equals(aac)) {
            Iterator ai = aa.iterator();
            Iterator bi = bb.iterator();
            Object ao = ai.next(); // these are ok, since the sizes are != 0
            Object bo = bi.next();
            while (true) {
                int rel = aac.compare(ao, bo);
                if (rel == 0) {
                    if (!bi.hasNext())
                        return true;
                    if (!ai.hasNext())
                        return false;
                    bo = bi.next();
                    ao = ai.next();
                } else if (rel < 0) {
                    if (!ai.hasNext())
                        return false;
                    ao = ai.next();
                } else {
                    return false;
                }
            }
        }
    }
    return a.containsAll(b);
}

From source file:module.regulation.dispatch.presentationTier.RegulationDispatchAction.java

private java.util.List<RegulationDispatchWorkflowMetaProcess> limitAndOrderSearchedEntries(
        java.util.Set searchedEntries, final Comparator[] propertiesToCompare, final Integer[] orderToUse,
        Integer iDisplayStart, Integer iDisplayLength) {
    final List<RegulationDispatchWorkflowMetaProcess> result = new ArrayList<>();
    result.addAll(searchedEntries);//  ww  w .j  av a  2s .  c om

    Collections.sort(result, new Comparator<RegulationDispatchWorkflowMetaProcess>() {

        @Override
        public int compare(RegulationDispatchWorkflowMetaProcess oLeft,
                RegulationDispatchWorkflowMetaProcess oRight) {
            for (int i = 0; i < propertiesToCompare.length; i++) {
                try {
                    Comparator comparator = propertiesToCompare[i];

                    if (comparator.compare(oLeft, oRight) != 0) {
                        return orderToUse[i] * comparator.compare(oLeft, oRight);
                    }
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }

            return 0;
        }
    });

    return result.subList(iDisplayStart, Math.min(iDisplayStart + iDisplayLength, searchedEntries.size()));
}

From source file:org.hippoecm.frontend.plugins.cms.admin.users.UserDataProvider.java

@Override
public Iterator<User> iterator(long first, long count) {
    List<User> userList = new ArrayList<>(getList());

    final Comparator<String> nullSafeComparator = new NullComparator(false);

    Collections.sort(userList, new Comparator<User>() {
        public int compare(User user1, User user2) {
            int direction = getSort().isAscending() ? 1 : -1;

            if ("frontend:firstname".equals(getSort().getProperty())) {
                return direction * (nullSafeComparator.compare(user1.getFirstName(), user2.getFirstName()));
            } else if ("frontend:lastname".equals(getSort().getProperty())) {
                return direction * (nullSafeComparator.compare(user1.getLastName(), user2.getLastName()));
            } else {
                return direction * (nullSafeComparator.compare(user1.getUsername(), user2.getUsername()));
            }//from  w  ww.java 2s  . c  o  m
        }
    });

    return userList.subList((int) first, (int) Math.min(first + count, userList.size())).iterator();
}

From source file:kobic.msb.swing.comparator.JMsbComparatorChain.java

@SuppressWarnings("unchecked")
public int compare(Object o1, Object o2) throws UnsupportedOperationException {
    if (isLocked == false) {
        this.checkChainIntegrity();
        isLocked = true;/*  w w w  . ja  va  2s . c  o  m*/
    }

    // iterate over all comparators in the chain
    Iterator comparators = comparatorChain.iterator();
    for (int comparatorIndex = 0; comparators.hasNext(); ++comparatorIndex) {

        Comparator comparator = (Comparator) comparators.next();
        int retval = comparator.compare(o1, o2);
        if (retval != 0) {
            // invert the order if it is a reverse sort
            if (orderingBits.get(comparatorIndex) == true) {
                if (Integer.MIN_VALUE == retval) {
                    retval = Integer.MAX_VALUE;
                } else {
                    retval *= -1;
                }
            }

            return retval;
        }
    }

    // if comparators are exhausted, return 0
    return 0;
}

From source file:gov.nih.nci.ncicb.tcga.dcc.datareports.service.DatareportsServiceImpl.java

@Override
public Comparator<ExtJsFilter> comparatorExtJsFilter() {
    final Comparator alphaNum = new AlphanumComparator();
    return new Comparator<ExtJsFilter>() {
        public int compare(final ExtJsFilter o1, final ExtJsFilter o2) {
            try {
                return alphaNum.compare(o1.getText(), o2.getText());
            } catch (Exception e) {
                logger.debug(FancyExceptionLogger.printException(e));
                return 0;
            }/*from  w w w . j  a  v  a2  s .c  o  m*/
        }
    };
}

From source file:org.openvpms.web.workspace.customer.CustomerMailContext.java

/**
 * Sorts contacts by party name.//  w w  w. j  a v  a 2  s.co  m
 *
 * @param contacts the contacts to sort
 */
@SuppressWarnings("unchecked")
private void sort(List<Contact> contacts) {
    final Comparator<Object> comparator = (Comparator<Object>) ComparatorUtils.nullHighComparator(null);
    // sort the contacts on party name
    Collections.sort(contacts, new Comparator<Contact>() {
        public int compare(Contact o1, Contact o2) {
            return comparator.compare(o1.getParty().getName(), o2.getParty().getName());
        }
    });
}