KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > packageview > PackageExplorerElementComparer


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.ui.packageview;
12
13 import org.eclipse.jface.viewers.IElementComparer;
14
15 import org.eclipse.jdt.core.ICompilationUnit;
16 import org.eclipse.jdt.core.IJavaElement;
17
18 /* package */ class PackageExplorerElementComparer implements IElementComparer {
19
20     public boolean equals(Object JavaDoc o1, Object JavaDoc o2) {
21         if (o1 == o2) // this handles also the case that both are null
22
return true;
23         if (o1 == null)
24             return false; // o2 != null if we reach this point
25
if (o1.equals(o2))
26             return true;
27         IJavaElement j1= (o1 instanceof IJavaElement) ? (IJavaElement)o1 : null;
28         IJavaElement j2= (o2 instanceof IJavaElement) ? (IJavaElement)o2 : null;
29         if (j1 == null || j2 == null)
30             return false;
31         ICompilationUnit c1= (ICompilationUnit)j1.getAncestor(IJavaElement.COMPILATION_UNIT);
32         ICompilationUnit c2= (ICompilationUnit)j2.getAncestor(IJavaElement.COMPILATION_UNIT);
33         if (c1 == null || c2 == null)
34             return false;
35         if (c1.isWorkingCopy() && c2.isWorkingCopy() || !c1.isWorkingCopy() && !c2.isWorkingCopy())
36             return false;
37         // From here on either c1 or c2 is a working copy.
38
if (c1.isWorkingCopy()) {
39             j1= c1.getOriginal(j1);
40         } else if (c2.isWorkingCopy()) {
41             j2= c2.getOriginal(j2);
42         }
43         if (j1 == null || j2 == null)
44             return false;
45         return j1.equals(j2);
46     }
47
48     public int hashCode(Object JavaDoc o1) {
49         IJavaElement j1= (o1 instanceof IJavaElement) ? (IJavaElement)o1 : null;
50         if (j1 == null)
51             return o1.hashCode();
52         ICompilationUnit c1= (ICompilationUnit)j1.getAncestor(IJavaElement.COMPILATION_UNIT);
53         if (c1 == null || !c1.isWorkingCopy())
54             return o1.hashCode();
55         // From here on c1 is a working copy.
56
j1= c1.getOriginal(j1);
57         if (j1 == null)
58             return o1.hashCode();
59         return j1.hashCode();
60     }
61 }
62
Popular Tags