Commit | Line | Data |
---|---|---|
853beb54 MK |
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 | *******************************************************************************/ | |
8b4318bb | 12 | |
853beb54 MK |
13 | package org.eclipse.tracecompass.tmf.ui.viewers.table; |
14 | ||
8b4318bb FLN |
15 | import java.util.Comparator; |
16 | ||
853beb54 MK |
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 | * | |
34c87ae8 | 25 | * @since 2.0 |
853beb54 MK |
26 | * |
27 | */ | |
8b4318bb | 28 | public class ViewerCompoundComparator extends ViewerComparator { |
853beb54 MK |
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 | */ | |
8b4318bb | 34 | public static final ViewerCompoundComparator STRING_COMPARATOR = new ViewerCompoundComparator(new Comparator<Object>() { |
853beb54 MK |
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 | } | |
8b4318bb | 41 | }); |
853beb54 | 42 | |
8b4318bb FLN |
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 | } | |
853beb54 MK |
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 | ||
8b4318bb FLN |
74 | private int getNextCompare(Viewer viewer, Object e1, Object e2) { |
75 | return (fNext != null) ? fNext.compare(viewer, e1, e2) : 0; | |
853beb54 MK |
76 | } |
77 | ||
78 | @Override | |
1bba26cf | 79 | public Comparator<Object> getComparator() { |
8b4318bb | 80 | return super.getComparator(); |
853beb54 MK |
81 | } |
82 | ||
8b4318bb FLN |
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 | } | |
853beb54 | 88 | } |