Project

General

Profile

Download (2.4 KB) Statistics
| Branch: | Tag: | Revision:
1
/*******************************************************************************
2
 * Copyright (c) 2006, 2010 IBM Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
6
 * http://www.eclipse.org/legal/epl-v10.html
7
 *
8
 * Contributors:
9
 *     IBM Corporation - initial API and implementation
10
 *******************************************************************************/
11

    
12
package org.eclipse.ui.internal.navigator.extensions;
13

    
14
import java.util.Comparator;
15

    
16
import org.eclipse.ui.navigator.INavigatorContentDescriptor;
17
import org.eclipse.ui.navigator.INavigatorContentExtension;
18

    
19
/**
20
 * @since 3.2
21
 * 
22
 */
23
public class ExtensionSequenceNumberComparator implements Comparator {
24

    
25
	/**
26
	 * The initialized singleton instance.
27
	 */
28
	public static final ExtensionSequenceNumberComparator INSTANCE = new ExtensionSequenceNumberComparator(true);
29

    
30
	/**
31
	 * The initialized singleton instance.
32
	 */
33
	public static final ExtensionSequenceNumberComparator DESCENDING = new ExtensionSequenceNumberComparator(false);
34
	
35
	private final int sortAscending;
36
	
37
	/**
38
	 * Creates an instance that sorts according to the given boolean flag.
39
	 * 
40
	 * @param toSortAscending
41
	 *            <code>true</code> for ascending sort order or
42
	 *            <code>false</code> for descending sort order.
43
	 */
44
	public ExtensionSequenceNumberComparator(boolean toSortAscending) {
45
		sortAscending = toSortAscending ? 1 : -1; 
46
	}
47

    
48
	/*
49
	 * (non-Javadoc)
50
	 * 
51
	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
52
	 */
53
	public int compare(Object o1, Object o2) {
54

    
55
		INavigatorContentDescriptor lvalue = null;
56
		INavigatorContentDescriptor rvalue = null;
57

    
58
		if (o1 instanceof INavigatorContentDescriptor) {
59
			lvalue = (INavigatorContentDescriptor) o1;
60
		} else if (o1 instanceof NavigatorContentExtension) {
61
			lvalue = ((NavigatorContentExtension) o1).getDescriptor();
62
		}
63

    
64
		if (o2 instanceof INavigatorContentDescriptor) {
65
			rvalue = (INavigatorContentDescriptor) o2;
66
		} else if (o2 instanceof INavigatorContentExtension) {
67
			rvalue = ((NavigatorContentExtension) o2).getDescriptor();
68
		}
69

    
70
		if (lvalue == null || rvalue == null) {
71
			return  -1 * sortAscending;
72
		}
73

    
74
		int c = lvalue.getSequenceNumber() - rvalue.getSequenceNumber();
75
		if (c != 0) {
76
			return c * sortAscending;
77
		}
78
		return 0;
79

    
80
	}
81

    
82
}
(7-7/29)