comparison dwtx/core/commands/common/NamedHandleObjectComparator.d @ 3:6518c18a01f7

eclipse.core package without osgi dependencies
author Frank Benoit <benoit@tionex.de>
date Wed, 26 Mar 2008 00:57:19 +0100
parents
children 46a6e0e6ccd4
comparison
equal deleted inserted replaced
2:a012107a911c 3:6518c18a01f7
1 /*******************************************************************************
2 * Copyright (c) 2005 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 * Port to the D programming language:
11 * Frank Benoit <benoit@tionex.de>
12 ******************************************************************************/
13
14 module dwtx.core.commands.common.NamedHandleObjectComparator;
15
16 // import java.util.Comparator;
17 import dwtx.core.commands.common.NotDefinedException;
18 import dwtx.core.commands.common.NamedHandleObject;
19 import dwtx.core.internal.commands.util.Util;
20 import dwt.dwthelper.utils;
21
22 /**
23 * Comparator for instances of <code>NamedHandleObject</code> for display to
24 * an end user. The comparison is based on the name of the instances.
25 *
26 * @since 3.2
27 */
28 public class NamedHandleObjectComparator /+: Comparator+/ {
29
30 /**
31 * Compares to instances of NamedHandleObject based on their names. This is
32 * useful is they are display to an end user.
33 *
34 * @param left
35 * The first obect to compare; may be <code>null</code>.
36 * @param right
37 * The second object to compare; may be <code>null</code>.
38 * @return <code>-1</code> if <code>left</code> is <code>null</code>
39 * and <code>right</code> is not <code>null</code>;
40 * <code>0</code> if they are both <code>null</code>;
41 * <code>1</code> if <code>left</code> is not <code>null</code>
42 * and <code>right</code> is <code>null</code>. Otherwise, the
43 * result of <code>left.compareTo(right)</code>.
44 */
45 public final int compare(Object left, Object right) {
46 NamedHandleObject a = cast(NamedHandleObject) left;
47 NamedHandleObject b = cast(NamedHandleObject) right;
48
49 String aName = null;
50 try {
51 aName = a.getName();
52 } catch (NotDefinedException e) {
53 // Leave aName as null.
54 }
55 String bName = null;
56 try {
57 bName = b.getName();
58 } catch (NotDefinedException e) {
59 // Leave bName as null.
60 }
61
62 return Util.compare(aName, bName);
63 }
64 }