tmf: Fix generics warning in ViewerCompoundComparator
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.ui / src / org / eclipse / tracecompass / tmf / ui / viewers / table / ViewerCompoundComparator.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials are
5 * made available under the terms of the Eclipse Public License v1.0 which
6 * accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *
9 * Contributors:
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.tracecompass.tmf.ui.viewers.table;
14
15 import java.util.Comparator;
16
17 import org.eclipse.jface.viewers.Viewer;
18 import org.eclipse.jface.viewers.ViewerComparator;
19 import org.eclipse.tracecompass.common.core.NonNullUtils;
20
21 /**
22 * ViewerCompoundComparator that can be used to make compound comparisons (1st
23 * key 2nd key etc...)
24 *
25 * @since 2.0
26 *
27 */
28 public class ViewerCompoundComparator extends ViewerComparator {
29
30 /**
31 * String comparator, compares two objects by their toString values, if an
32 * object is null, it is assigned to an empty string
33 */
34 public static final ViewerCompoundComparator STRING_COMPARATOR = new ViewerCompoundComparator(new Comparator<Object>() {
35 @Override
36 public int compare(Object e1, Object e2) {
37 String left = NonNullUtils.nullToEmptyString(e1);
38 String right = NonNullUtils.nullToEmptyString(e2);
39 return left.compareTo(right);
40 }
41 });
42
43 private ViewerCompoundComparator fNext;
44
45 /**
46 * Create a viewer compound comparator
47 *
48 * @param comparator
49 * selected comparator
50 */
51 public ViewerCompoundComparator(Comparator<? extends Object> comparator) {
52 super(comparator);
53 }
54
55 /**
56 * Sets the next comparator
57 *
58 * @param next
59 * the next comparator
60 */
61 public void setNext(ViewerCompoundComparator next) {
62 fNext = next;
63 }
64
65 /**
66 * Get the next comparator
67 *
68 * @return the next comparator
69 */
70 public ViewerCompoundComparator getNext() {
71 return fNext;
72 }
73
74 private int getNextCompare(Viewer viewer, Object e1, Object e2) {
75 return (fNext != null) ? fNext.compare(viewer, e1, e2) : 0;
76 }
77
78 @Override
79 public Comparator<Object> getComparator() {
80 return super.getComparator();
81 }
82
83 @Override
84 public int compare(Viewer viewer, Object e1, Object e2) {
85 int retVal = getComparator().compare(e1, e2);
86 return (retVal != 0) ? retVal : getNextCompare(viewer, e1, e2);
87 }
88 }
This page took 0.032226 seconds and 5 git commands to generate.