Example usage for com.google.common.collect Ordering from

List of usage examples for com.google.common.collect Ordering from

Introduction

In this page you can find the example usage for com.google.common.collect Ordering from.

Prototype

@GwtCompatible(serializable = true)
@Deprecated
public static <T> Ordering<T> from(Ordering<T> ordering) 

Source Link

Document

Simply returns its argument.

Usage

From source file:org.apache.tez.history.parser.datamodel.VertexInfo.java

private Ordering<TaskInfo> orderingOnTimeTaken() {
    return Ordering.from(new Comparator<TaskInfo>() {
        @Override//w ww .  j  a  v a2s .co m
        public int compare(TaskInfo o1, TaskInfo o2) {
            return Long.compare(o1.getTimeTaken(), o2.getTimeTaken());
        }
    });
}

From source file:org.kuali.kpme.core.earncode.service.EarnCodeServiceImpl.java

@Override
public Map<String, String> getEarnCodesForDisplayWithEffectiveDate(String principalId, LocalDate asOfDate,
        boolean isLeavePlanningCalendar) {
    List<EarnCode> earnCodes = this.getEarnCodesForPrincipal(principalId, asOfDate, isLeavePlanningCalendar);

    boolean futureDate = asOfDate.isAfter(LocalDate.now());
    List<EarnCode> copyList = new ArrayList<EarnCode>();
    copyList.addAll(earnCodes);/*from   www  .ja  v  a2  s. c o m*/
    for (EarnCode earnCode : copyList) {
        if (futureDate && !earnCode.getAllowScheduledLeave().equalsIgnoreCase("Y")) {
            earnCodes.remove(earnCode);
        }
    }
    Comparator<EarnCode> earnCodeComparator = new Comparator<EarnCode>() {
        @Override
        public int compare(EarnCode ec1, EarnCode ec2) {
            return ec1.getEarnCode().compareToIgnoreCase(ec2.getEarnCode());
        }
    };
    // Order by leaveCode ascending
    Ordering<EarnCode> ordering = Ordering.from(earnCodeComparator);

    Map<String, String> earnCodesForDisplay = new LinkedHashMap<String, String>();
    for (EarnCode earnCode : ordering.sortedCopy(earnCodes)) {
        earnCodesForDisplay.put(getEarnCodeKeyForDisplay(earnCode), getEarnCodeValueForDisplay(earnCode));
    }
    return earnCodesForDisplay;
}

From source file:org.apache.tez.history.parser.datamodel.VertexInfo.java

private Ordering<TaskInfo> orderingOnStartTime() {
    return Ordering.from(new Comparator<TaskInfo>() {
        @Override/*from  w  ww  .j a v a  2 s.  c  o m*/
        public int compare(TaskInfo o1, TaskInfo o2) {
            return Long.compare(o1.getStartTimeInterval(), o2.getStartTimeInterval());
        }
    });
}

From source file:org.apache.tez.history.parser.datamodel.DagInfo.java

private Ordering<VertexInfo> getVertexOrdering() {
    return Ordering.from(new Comparator<VertexInfo>() {
        @Override/*from   w  ww  . j  a v  a2 s.com*/
        public int compare(VertexInfo o1, VertexInfo o2) {
            return (o1.getTimeTaken() < o2.getTimeTaken()) ? -1
                    : ((o1.getTimeTaken() == o2.getTimeTaken()) ? 0 : 1);
        }
    });
}

From source file:com.facebook.presto.verifier.Validator.java

private static Comparator<List<Object>> rowComparator(int precision) {
    final Comparator<Object> comparator = Ordering.from(columnComparator(precision)).nullsFirst();
    return new Comparator<List<Object>>() {
        @Override//from   w  w w.  j a  va2  s.com
        public int compare(List<Object> a, List<Object> b) {
            if (a.size() != b.size()) {
                return Integer.compare(a.size(), b.size());
            }
            for (int i = 0; i < a.size(); i++) {
                int r = comparator.compare(a.get(i), b.get(i));
                if (r != 0) {
                    return r;
                }
            }
            return 0;
        }
    };
}

From source file:org.apache.tez.history.parser.datamodel.VertexInfo.java

private Ordering<TaskAttemptInfo> orderingOnAttemptStartTime() {
    return Ordering.from(new Comparator<TaskAttemptInfo>() {
        @Override//  w  w w  .j a  v  a  2 s.  c  o  m
        public int compare(TaskAttemptInfo o1, TaskAttemptInfo o2) {
            return Long.compare(o1.getStartTimeInterval(), o2.getStartTimeInterval());
        }
    });
}

From source file:io.prestosql.verifier.Validator.java

private static Comparator<List<Object>> rowComparator(int precision) {
    Comparator<Object> comparator = Ordering.from(columnComparator(precision)).nullsFirst();
    return (a, b) -> {
        if (a.size() != b.size()) {
            return Integer.compare(a.size(), b.size());
        }/* w w w .  jav a  2s.c o m*/
        for (int i = 0; i < a.size(); i++) {
            int r = comparator.compare(a.get(i), b.get(i));
            if (r != 0) {
                return r;
            }
        }
        return 0;
    };
}

From source file:edu.harvard.med.screensaver.ui.users.UserViewer.java

public DataModel getLabMembersDataModel() {
    if (_labMembersDataModel == null && isScreeningRoomUserViewMode()) {
        _labMembersDataModel = new ListDataModel(Ordering.from(ScreensaverUserComparator.getInstance())
                .sortedCopy(getScreeningRoomUser().getLab().getLabMembers()));
    }/* w  ww.  j a  v  a 2  s. co m*/
    return _labMembersDataModel;
}

From source file:edu.udo.scaffoldhunter.view.scaffoldtree.VTree.java

/**
 * Reorders the scaffolds on the first ring according to the given
 * properties. This method is synchronized, because multiple simultaneous sortings could cause race conditions.
 * Also writes data into a given {@link SortState} object, which are generated during the sort process.
 * @param <T>/*from w  ww .  jav  a2 s.  c  o  m*/
 *          The comparable type over which the scaffold nodes are sorted.
 * 
 * @param comparator
 *          A comparator, which is used for sorting VNodes after they are mapped to objects of type <T>
 * @param function
 *          A mapping, which maps a VNode into an object of type <T>. Afterwards the VNode can be sorted using the comparator
 * @param colorSegments
 *          If true the tree is also colored mapping the property range to a color gradient.
 * @param addCaption
 *          If true, each color segment receives a label, which shows the value of the given property.
 * @param color
 *          The color, which is used for mapping values.
 * @param node 
 *          Indicates the node, for which the sorting is applied. If null, the sorting is applied to whole tree. 
 *          If not null only one node and its subnodes are sorted without recoloring.
 * @param sortState
 *          This object contains information for the sorting legend in the sidebar. It is passed to the VTree to add some values 
 *          concerning sorting values and colors.          
 */
public synchronized <T> void propertySort(Comparator<T> comparator, Function<VNode, T> function,
        boolean colorSegments, boolean addCaption, Color color, VNode node, SortState sortState) {

    Ordering<VNode> vnodeOrdering = Ordering.from(comparator).onResultOf(function);

    if (node != null) {
        // if node is not null then the sorting is only applied to this single node
        node.sortSubtree(vnodeOrdering);
        layoutInvalid = true;
        return;
    } else {
        // else the sorting is applied to the whole tree and coloring is added to the tree
        vroot.sortSubtree(vnodeOrdering);
    }
    List<VNode> nodes = getRoot().getTreeChildren();
    if (nodes.isEmpty())
        return;

    layout.clearSeparators();
    if (colorSegments) {
        ArrayList<VNode> separators = Lists.newArrayList();
        ArrayList<String> captions = new ArrayList<String>();
        separators.add(nodes.get(0));
        T lastValue = function.apply(nodes.get(0));
        if (lastValue != null)
            captions.add(addCaption ? lastValue.toString() : "");
        else
            captions.add("");
        for (VNode n : nodes) {
            T currentValue = function.apply(n);
            if (currentValue == lastValue)
                continue; // both null
            if (currentValue != null && lastValue != null && currentValue.equals(lastValue))
                continue;
            separators.add(n);
            lastValue = currentValue;
            if (lastValue != null)
                captions.add(addCaption ? lastValue.toString() : "");
            else
                captions.add("");
        }
        ArrayList<Color> colors = new ArrayList<Color>();
        double colorLevelIncrement = 255.0 / separators.size();
        for (int i = 0; i < separators.size(); i++) {
            colors.add(new Color(color.getRed(), color.getGreen(), color.getBlue(),
                    (int) (colorLevelIncrement * (i + 1))));
        }

        // This part generates the data for the SortState
        int segments = Math.min(11, separators.size());

        ArrayList<Color> colorsForSortState = new ArrayList<Color>(segments);
        ArrayList<Double> values = new ArrayList<Double>(segments);

        /* I assume that the given function maps a ScaffoldNode into a Double value. If that is not the case
         * then this part is skipped by a caught CastException and no data is written into the SortState.
         * This will result into the Sort Legend (sidebar) not showing visual information about the colors.
         */
        try {
            for (int i = 0; i < (segments - 1); i++) {
                values.add((Double) function.apply(separators.get(((i) * separators.size() / (segments - 1)))));
            }
            values.add((Double) function.apply(separators.get(separators.size() - 1)));

            for (int i = 0; i < (segments - 1); i++) {
                colorsForSortState.add(colors.get(((i) * separators.size() / (segments - 1))));
            }
            colorsForSortState.add(colors.get(separators.size() - 1));

            sortState.setValues(values);
            sortState.setColors(colorsForSortState);
        } catch (ClassCastException e) {

        }

        /*
         * float hsb[] = Color.RGBtoHSB(color.getRed(), color.getGreen(),
         * color.getBlue(), null); float colorLevelIncrement = (1-hsb[2])
         * /separators.size(); for (int i = 0; i < separators.size(); i++)
         * colors.add(Color.getHSBColor(hsb[0], hsb[1],
         * 1-(i*colorLevelIncrement)));
         */

        layout.setSeparators(separators, captions, colors);
    }

    layoutInvalid = true;
    return;
}

From source file:com.netxforge.netxstudio.common.model.StudioUtils.java

/**
 * Split a collection of {@link Value } objects. The splitting criteria is
 * the interval in minutes. Group the values by their time stamp with the
 * interval as boundary./*from www. ja va  2 s . c o m*/
 * 
 * For week target, do not consider the month field. Note that if a week is
 * partially in two consecutive years, it will be split by this algo.
 */
public static List<List<Value>> values_(List<Value> values, int targetInterval) {

    final Map<String, List<Value>> targetMap = Maps.newHashMap();

    final int[] fields = NonModelUtils.fieldsForTargetInterval(targetInterval);

    GregorianCalendar cal;
    for (Value v : values) {
        cal = v.getTimeStamp().toGregorianCalendar();

        StringBuffer sb = new StringBuffer();
        for (int field : fields) {
            // Get the target map.
            final int currentFieldValue = cal.get(field);
            sb.append("_" + currentFieldValue);
        }
        String key = sb.toString();

        if (targetMap.containsKey(key)) {
            targetMap.get(key).add(v);
        } else {
            List<Value> vList = Lists.newArrayList();
            vList.add(v);
            targetMap.put(key, vList);
        }
    }

    // Sort the entries of our map.

    List<List<Value>> valueMatrix = Ordering.from(new Comparator<List<Value>>() {

        public int compare(List<Value> o1, List<Value> o2) {
            if (o1.isEmpty() || o2.isEmpty()) {
                return 0;
            }

            // Sort the collections first.
            List<Value> o1Sorted = sortValuesByTimeStamp(o1);
            List<Value> o2Sorted = sortValuesByTimeStamp(o2);

            return valueValueCompare().compare(o1Sorted.get(0), o2Sorted.get(0));
        }
    }).sortedCopy(targetMap.values());

    if (CommonActivator.DEBUG) {
        CommonActivator.TRACE.trace(CommonActivator.TRACE_COMMON_UTILS_OPTION,
                "Value by period splitter, key output");
        List<String> sortedKeys = Ordering.from(new Comparator<String>() {

            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }

        }).sortedCopy(targetMap.keySet());
        for (String s : sortedKeys) {
            CommonActivator.TRACE.trace(CommonActivator.TRACE_COMMON_UTILS_OPTION, "key: " + s);
        }
    }

    return valueMatrix;
}