KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > events > ResourceComparator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.events;
12
13 import org.eclipse.core.internal.resources.ICoreConstants;
14 import org.eclipse.core.internal.resources.ResourceInfo;
15 import org.eclipse.core.internal.watson.IElementComparator;
16 import org.eclipse.core.resources.IResource;
17 import org.eclipse.core.resources.IResourceDelta;
18
19 /**
20  * Compares two Resources and returns flags describing how
21  * they have changed, for use in computing deltas.
22  * Implementation note: rather than defining a partial order
23  * as specified by IComparator, the compare operation returns
24  * a set of flags instead. The delta computation only cares
25  * whether the comparison is zero (equal) or non-zero (not equal).
26  */

27 public class ResourceComparator implements IElementComparator, ICoreConstants {
28     /* Singleton instances */
29     protected static final ResourceComparator notificationSingleton = new ResourceComparator(true, false);
30     protected static final ResourceComparator buildSingleton = new ResourceComparator(false, false);
31
32     /**
33      * Boolean indicating whether or not this comparator is to be used for
34      * a notification. (as opposed to a build) Notifications include extra information
35      * like marker and sync info changes.
36      */

37     private boolean notification;
38
39     /**
40      * Boolean indicating whether or not this comparator is to be used for
41      * snapshot. Snapshots care about extra information such as the used bit.
42      */

43     private boolean save;
44
45     /**
46      * Returns a comparator which compares resource infos, suitable for computing
47      * save and snapshot deltas.
48      */

49     public static ResourceComparator getSaveComparator() {
50         return new ResourceComparator(false, true);
51     }
52
53     /**
54      * Returns a comparator which compares resource infos, suitable for computing
55      * build deltas.
56      */

57     public static ResourceComparator getBuildComparator() {
58         return buildSingleton;
59     }
60
61     /**
62      * Returns a comparator which compares resource infos, suitable for computing
63      * build deltas.
64      */

65     public static ResourceComparator getNotificationComparator() {
66         return notificationSingleton;
67     }
68
69     /**
70      * Create a comparator which compares resource infos.
71      * @param notification if true, check for marker deltas.
72      * @param save if true, check for all resource changes that snapshot needs
73      */

74     private ResourceComparator(boolean notification, boolean save) {
75         this.notification = notification;
76         this.save = save;
77     }
78
79     /**
80      * Compare the ElementInfos for two resources.
81      */

82     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
83         // == handles null, null.
84
if (o1 == o2)
85             return IResourceDelta.NO_CHANGE;
86         int result = 0;
87         if (o1 == null)
88             return ((ResourceInfo) o2).isSet(M_PHANTOM) ? IResourceDelta.ADDED_PHANTOM : IResourceDelta.ADDED;
89         if (o2 == null)
90             return ((ResourceInfo) o1).isSet(M_PHANTOM) ? IResourceDelta.REMOVED_PHANTOM : IResourceDelta.REMOVED;
91         if (!(o1 instanceof ResourceInfo && o2 instanceof ResourceInfo))
92             return IResourceDelta.NO_CHANGE;
93         ResourceInfo oldElement = (ResourceInfo) o1;
94         ResourceInfo newElement = (ResourceInfo) o2;
95         if (!oldElement.isSet(M_PHANTOM) && newElement.isSet(M_PHANTOM))
96             return IResourceDelta.REMOVED;
97         if (oldElement.isSet(M_PHANTOM) && !newElement.isSet(M_PHANTOM))
98             return IResourceDelta.ADDED;
99         if (!compareOpen(oldElement, newElement))
100             result |= IResourceDelta.OPEN;
101         if (!compareContents(oldElement, newElement)) {
102             if (oldElement.getType() == IResource.PROJECT)
103                 result |= IResourceDelta.DESCRIPTION;
104             else
105                 result |= IResourceDelta.CONTENT;
106         }
107         if (!compareType(oldElement, newElement))
108             result |= IResourceDelta.TYPE;
109         if (!compareNodeIDs(oldElement, newElement)) {
110             result |= IResourceDelta.REPLACED;
111             // if the node was replaced and the old and new were files, this is also a content change.
112
if (oldElement.getType() == IResource.FILE && newElement.getType() == IResource.FILE)
113                 result |= IResourceDelta.CONTENT;
114         }
115         if (!compareCharsets(oldElement, newElement))
116             result |= IResourceDelta.ENCODING;
117         if (notification && !compareSync(oldElement, newElement))
118             result |= IResourceDelta.SYNC;
119         if (notification && !compareMarkers(oldElement, newElement))
120             result |= IResourceDelta.MARKERS;
121         if (save && !compareUsed(oldElement, newElement))
122             result |= IResourceDelta.CHANGED;
123         return result == 0 ? 0 : result | IResourceDelta.CHANGED;
124     }
125
126     private boolean compareCharsets(ResourceInfo oldElement, ResourceInfo newElement) {
127         return oldElement.getCharsetGenerationCount() == newElement.getCharsetGenerationCount();
128     }
129
130     /**
131      * Compares the contents of the ResourceInfo.
132      */

133     private boolean compareContents(ResourceInfo oldElement, ResourceInfo newElement) {
134         return oldElement.getContentId() == newElement.getContentId();
135     }
136
137     private boolean compareMarkers(ResourceInfo oldElement, ResourceInfo newElement) {
138         // If both sets of markers are null then perhaps we added some markers
139
// but then deleted them right away before notification. In that case
140
// don't signify a marker change in the delta.
141
boolean bothNull = oldElement.getMarkers(false) == null && newElement.getMarkers(false) == null;
142         return bothNull || oldElement.getMarkerGenerationCount() == newElement.getMarkerGenerationCount();
143     }
144
145     /**
146      * Compares the node IDs of the ElementInfos for two resources.
147      */

148     private boolean compareNodeIDs(ResourceInfo oldElement, ResourceInfo newElement) {
149         return oldElement.getNodeId() == newElement.getNodeId();
150     }
151
152     /**
153      * Compares the open state of the ElementInfos for two resources.
154      */

155     private boolean compareOpen(ResourceInfo oldElement, ResourceInfo newElement) {
156         return oldElement.isSet(M_OPEN) == newElement.isSet(M_OPEN);
157     }
158
159     private boolean compareSync(ResourceInfo oldElement, ResourceInfo newElement) {
160         return oldElement.getSyncInfoGenerationCount() == newElement.getSyncInfoGenerationCount();
161     }
162
163     /**
164      * Compares the type of the ResourceInfo.
165      */

166     private boolean compareType(ResourceInfo oldElement, ResourceInfo newElement) {
167         return oldElement.getType() == newElement.getType();
168     }
169
170     /**
171      * Compares the open state of the ElementInfos for two resources.
172      */

173     private boolean compareUsed(ResourceInfo oldElement, ResourceInfo newElement) {
174         return oldElement.isSet(M_USED) == newElement.isSet(M_USED);
175     }
176 }
177
Popular Tags