1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
10 * Matthew Khouzam - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.tracecompass
.tmf
.ui
.viewers
.table
;
15 import java
.util
.Comparator
;
17 import org
.eclipse
.jface
.viewers
.Viewer
;
18 import org
.eclipse
.jface
.viewers
.ViewerComparator
;
19 import org
.eclipse
.tracecompass
.common
.core
.NonNullUtils
;
22 * ViewerCompoundComparator that can be used to make compound comparisons (1st
28 public class ViewerCompoundComparator
extends ViewerComparator
{
31 * String comparator, compares two objects by their toString values, if an
32 * object is null, it is assigned to an empty string
34 public static final ViewerCompoundComparator STRING_COMPARATOR
= new ViewerCompoundComparator(new Comparator
<Object
>() {
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
);
43 private ViewerCompoundComparator fNext
;
46 * Create a viewer compound comparator
51 public ViewerCompoundComparator(Comparator
<?
extends Object
> comparator
) {
56 * Sets the next comparator
61 public void setNext(ViewerCompoundComparator next
) {
66 * Get the next comparator
68 * @return the next comparator
70 public ViewerCompoundComparator
getNext() {
74 private int getNextCompare(Viewer viewer
, Object e1
, Object e2
) {
75 return (fNext
!= null) ? fNext
.compare(viewer
, e1
, e2
) : 0;
79 public Comparator
<Object
> getComparator() {
80 return super.getComparator();
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
);