1 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 27 public class ResourceComparator implements IElementComparator, ICoreConstants { 28 29 protected static final ResourceComparator notificationSingleton = new ResourceComparator(true, false); 30 protected static final ResourceComparator buildSingleton = new ResourceComparator(false, false); 31 32 37 private boolean notification; 38 39 43 private boolean save; 44 45 49 public static ResourceComparator getSaveComparator() { 50 return new ResourceComparator(false, true); 51 } 52 53 57 public static ResourceComparator getBuildComparator() { 58 return buildSingleton; 59 } 60 61 65 public static ResourceComparator getNotificationComparator() { 66 return notificationSingleton; 67 } 68 69 74 private ResourceComparator(boolean notification, boolean save) { 75 this.notification = notification; 76 this.save = save; 77 } 78 79 82 public int compare(Object o1, Object o2) { 83 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 (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 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 boolean bothNull = oldElement.getMarkers(false) == null && newElement.getMarkers(false) == null; 142 return bothNull || oldElement.getMarkerGenerationCount() == newElement.getMarkerGenerationCount(); 143 } 144 145 148 private boolean compareNodeIDs(ResourceInfo oldElement, ResourceInfo newElement) { 149 return oldElement.getNodeId() == newElement.getNodeId(); 150 } 151 152 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 166 private boolean compareType(ResourceInfo oldElement, ResourceInfo newElement) { 167 return oldElement.getType() == newElement.getType(); 168 } 169 170 173 private boolean compareUsed(ResourceInfo oldElement, ResourceInfo newElement) { 174 return oldElement.isSet(M_USED) == newElement.isSet(M_USED); 175 } 176 } 177 | Popular Tags |