Example usage for com.google.common.collect ComparisonChain result

List of usage examples for com.google.common.collect ComparisonChain result

Introduction

In this page you can find the example usage for com.google.common.collect ComparisonChain result.

Prototype

public abstract int result();

Source Link

Document

Ends this comparison chain and returns its result: a value having the same sign as the first nonzero comparison result in the chain, or zero if every result was zero.

Usage

From source file:com.bj58.oceanus.core.jdbc.result.RowSetComparator.java

@Override
public int compare(RowSet o1, RowSet o2) {
    ComparisonChain chain = ComparisonChain.start();
    try {//w w  w  .  j  a v a 2s .c  o m
        for (int i = 0; i < sortedColumns.length; i++) {
            TableColumn column = sortedColumns[i];
            OrderByType orderByType = column.getOrderByType();
            Object target1 = o1.getObject(column.getResultIndex());
            Object target2 = o2.getObject(column.getResultIndex());
            if (target1 == null) {// nullstring?
                target1 = "";
                if (target2 != null) {
                    target2 = target2.toString();
                } else {
                    target2 = "";
                }
            } else if (target2 == null) {
                target2 = "";
                target1 = target1.toString();
            }

            if (orderByType == null || orderByType == OrderByType.ASC) {
                chain = chain.compare((Comparable<?>) target1, (Comparable<?>) target2);
            } else {
                chain = chain.compare((Comparable<?>) target2, (Comparable<?>) target1);
            }

        }
    } catch (Exception e) {
        throw new ShardException("compare error!row=" + this, e);
    }
    return chain.result();
}