KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > commands > common > NamedHandleObjectComparator


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  ******************************************************************************/

11
12 package org.eclipse.core.commands.common;
13
14 import java.util.Comparator JavaDoc;
15
16 import org.eclipse.core.internal.commands.util.Util;
17
18 /**
19  * Comparator for instances of <code>NamedHandleObject</code> for display to
20  * an end user. The comparison is based on the name of the instances.
21  *
22  * @since 3.2
23  */

24 public class NamedHandleObjectComparator implements Comparator JavaDoc {
25
26     /**
27      * Compares to instances of NamedHandleObject based on their names. This is
28      * useful is they are display to an end user.
29      *
30      * @param left
31      * The first obect to compare; may be <code>null</code>.
32      * @param right
33      * The second object to compare; may be <code>null</code>.
34      * @return <code>-1</code> if <code>left</code> is <code>null</code>
35      * and <code>right</code> is not <code>null</code>;
36      * <code>0</code> if they are both <code>null</code>;
37      * <code>1</code> if <code>left</code> is not <code>null</code>
38      * and <code>right</code> is <code>null</code>. Otherwise, the
39      * result of <code>left.compareTo(right)</code>.
40      */

41     public final int compare(final Object JavaDoc left, final Object JavaDoc right) {
42         final NamedHandleObject a = (NamedHandleObject) left;
43         final NamedHandleObject b = (NamedHandleObject) right;
44
45         String JavaDoc aName = null;
46         try {
47             aName = a.getName();
48         } catch (final NotDefinedException e) {
49             // Leave aName as null.
50
}
51         String JavaDoc bName = null;
52         try {
53             bName = b.getName();
54         } catch (final NotDefinedException e) {
55             // Leave bName as null.
56
}
57
58         return Util.compare(aName, bName);
59     }
60 }
61
Popular Tags