KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > watson > DefaultElementComparator


1 /*******************************************************************************
2  * Copyright (c) 2000, 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 package org.eclipse.core.internal.watson;
12
13 /**
14  * This is what you would expect for an element tree comparator.
15  * Clients of the element tree that want specific comparison behaviour
16  * must define their own element comparator (without subclassing or
17  * otherwise extending this comparator). Internal element tree operations
18  * rely on the behaviour of this type, and the ElementTree maintainer
19  * reserves the right to change its behaviour as necessary.
20  */

21 public final class DefaultElementComparator implements IElementComparator {
22     private static DefaultElementComparator singleton;
23
24     /**
25      * Force clients to use the singleton
26      */

27     protected DefaultElementComparator() {
28         super();
29     }
30
31     /**
32      * Returns the type of change.
33      */

34     public int compare(Object JavaDoc oldInfo, Object JavaDoc newInfo) {
35         if (oldInfo == null && newInfo == null)
36             return 0;
37         if (oldInfo == null || newInfo == null)
38             return 1;
39         return testEquality(oldInfo, newInfo) ? 0 : 1;
40     }
41
42     /**
43      * Returns the singleton instance
44      */

45     public static IElementComparator getComparator() {
46         if (singleton == null) {
47             singleton = new DefaultElementComparator();
48         }
49         return singleton;
50     }
51
52     /**
53      * Makes a comparison based on equality
54      */

55     protected boolean testEquality(Object JavaDoc oldInfo, Object JavaDoc newInfo) {
56         if (oldInfo == null && newInfo == null)
57             return true;
58         if (oldInfo == null || newInfo == null)
59             return false;
60
61         return oldInfo.equals(newInfo);
62     }
63 }
64
Popular Tags