KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > dependencies > ResolutionDelta


1 /*******************************************************************************
2  * Copyright (c) 2003, 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
12 package org.eclipse.core.internal.dependencies;
13
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 /**
18  * Collects changes that happened during a resolution operation.
19  * Not to be implemented by clients.
20  * @see ElementChange
21  */

22 public class ResolutionDelta {
23
24     class ElementIdentifier {
25         private Object JavaDoc id;
26         private Object JavaDoc userObject;
27         private Object JavaDoc versionId;
28
29         ElementIdentifier(Object JavaDoc id, Object JavaDoc versionId, Object JavaDoc userObject) {
30             this.id = id;
31             this.versionId = versionId;
32             this.userObject = userObject;
33         }
34
35         public boolean equals(Object JavaDoc anObject) {
36             if (!(anObject instanceof ElementIdentifier))
37                 return false;
38             ElementIdentifier change = (ElementIdentifier) anObject;
39             return (change.userObject != null && change.userObject.equals(this.userObject)) || (this.id.equals(change.id) && this.versionId.equals(change.versionId) && change.userObject == null && this.userObject == null);
40         }
41
42         public int hashCode() {
43             return (id.hashCode() << 16) | (versionId.hashCode() & 0xFFFF);
44         }
45     }
46
47     private Map JavaDoc changes;
48
49     ResolutionDelta() {
50         this.changes = new HashMap JavaDoc();
51     }
52
53     public ElementChange[] getAllChanges() {
54         return (ElementChange[]) changes.values().toArray(new ElementChange[changes.size()]);
55     }
56
57     public ElementChange getChange(Object JavaDoc id, Object JavaDoc versionId, Object JavaDoc userObject) {
58         return (ElementChange) changes.get(new ElementIdentifier(id, versionId, userObject));
59     }
60
61     /**
62      * Record a new status change.
63      */

64     void recordChange(Element element, int kind) {
65         // check if a change has already been recorded for the element
66
ElementChange existingChange = this.getChange(element.getId(), element.getVersionId(), element.getUserObject());
67         // if not, just record it and we are done
68
if (existingChange == null) {
69             this.changes.put(new ElementIdentifier(element.getId(), element.getVersionId(), element.getUserObject()), new ElementChange(element, kind));
70             return;
71         }
72         // a removal cancels any existing addition
73
if (kind == ElementChange.REMOVED)
74             if (existingChange.getKind() == ElementChange.ADDED) {
75                 // if it was just an addition, just forget the change
76
this.changes.remove(new ElementIdentifier(element.getId(), element.getVersionId(), element.getUserObject()));
77                 return;
78             } else if ((existingChange.getKind() & ElementChange.ADDED) != 0) {
79                 // if it was an addition among other things, forget the addition bit, and ensure the removal bit is set
80
existingChange.setKind((existingChange.getKind() & ~ElementChange.ADDED) | ElementChange.REMOVED);
81                 return;
82             }
83         // otherwise, just update the new status for the existing change object
84
existingChange.setKind(existingChange.getKind() | kind);
85     }
86
87     public String JavaDoc toString() {
88         return changes.values().toString();
89     }
90 }
Popular Tags