KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > osgi > internal > resolver > StateDeltaImpl


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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.osgi.internal.resolver;
12
13 import java.util.*;
14
15 import org.eclipse.osgi.service.resolver.*;
16
17 public class StateDeltaImpl implements StateDelta {
18     private State state;
19     private Map changes = new HashMap();
20
21     public StateDeltaImpl(State state) {
22         this.state = state;
23     }
24
25     public BundleDelta[] getChanges() {
26         return (BundleDelta[]) changes.values().toArray(new BundleDelta[changes.size()]);
27     }
28
29     public BundleDelta[] getChanges(int mask, boolean exact) {
30         List result = new ArrayList();
31         for (Iterator changesIter = changes.values().iterator(); changesIter.hasNext();) {
32             BundleDelta change = (BundleDelta) changesIter.next();
33             if (mask == change.getType() || (!exact && (change.getType() & mask) != 0))
34                 result.add(change);
35         }
36         return (BundleDelta[]) result.toArray(new BundleDelta[result.size()]);
37     }
38
39     public State getState() {
40         return state;
41     }
42
43     void recordBundleAdded(BundleDescriptionImpl added) {
44         BundleDeltaImpl change = (BundleDeltaImpl) changes.get(added);
45         if (change == null) {
46             changes.put(added, new BundleDeltaImpl(added, BundleDelta.ADDED));
47             return;
48         }
49         if (change.getType() == BundleDelta.REMOVED) {
50             changes.remove(added);
51             return;
52         }
53         int newType = change.getType();
54         if ((newType & BundleDelta.REMOVED) != 0)
55             newType &= ~BundleDelta.REMOVED;
56         change.setType(newType | BundleDelta.ADDED);
57         change.setBundle(added);
58     }
59
60     void recordBundleUpdated(BundleDescriptionImpl updated) {
61         BundleDeltaImpl change = (BundleDeltaImpl) changes.get(updated);
62         if (change == null) {
63             changes.put(updated, new BundleDeltaImpl(updated, BundleDelta.UPDATED));
64             return;
65         }
66         if ((change.getType() & (BundleDelta.ADDED | BundleDelta.REMOVED)) != 0)
67             return;
68         change.setType(change.getType() | BundleDelta.UPDATED);
69         change.setBundle(updated);
70     }
71
72     void recordBundleRemoved(BundleDescriptionImpl removed) {
73         BundleDeltaImpl change = (BundleDeltaImpl) changes.get(removed);
74         if (change == null) {
75             changes.put(removed, new BundleDeltaImpl(removed, BundleDelta.REMOVED));
76             return;
77         }
78         if (change.getType() == BundleDelta.ADDED) {
79             changes.remove(removed);
80             return;
81         }
82         int newType = change.getType();
83         if ((newType & BundleDelta.ADDED) != 0)
84             newType &= ~BundleDelta.ADDED;
85         change.setType(newType | BundleDelta.REMOVED);
86     }
87
88     void recordBundleRemovalPending(BundleDescriptionImpl removed) {
89         BundleDeltaImpl change = (BundleDeltaImpl) changes.get(removed);
90         if (change == null) {
91             changes.put(removed, new BundleDeltaImpl(removed, BundleDelta.REMOVAL_PENDING));
92             return;
93         }
94         int newType = change.getType();
95         if ((newType & BundleDelta.REMOVAL_COMPLETE) != 0)
96             newType &= ~BundleDelta.REMOVAL_COMPLETE;
97         change.setType(newType | BundleDelta.REMOVAL_PENDING);
98     }
99
100     void recordBundleRemovalComplete(BundleDescriptionImpl removed) {
101         BundleDeltaImpl change = (BundleDeltaImpl) changes.get(removed);
102         if (change == null) {
103             changes.put(removed, new BundleDeltaImpl(removed, BundleDelta.REMOVAL_COMPLETE));
104             return;
105         }
106         int newType = change.getType();
107         if ((newType & BundleDelta.REMOVAL_PENDING) != 0)
108             newType &= ~BundleDelta.REMOVAL_PENDING;
109         change.setType(newType | BundleDelta.REMOVAL_COMPLETE);
110     }
111
112     void recordBundleResolved(BundleDescriptionImpl resolved, boolean result) {
113         if (resolved.isResolved() == result)
114             return; // do not record anything if nothing has changed
115
BundleDeltaImpl change = (BundleDeltaImpl) changes.get(resolved);
116         int newType = result ? BundleDelta.RESOLVED : BundleDelta.UNRESOLVED;
117         if (change == null) {
118             change = new BundleDeltaImpl(resolved, newType);
119             changes.put(resolved, change);
120             return;
121         }
122
123         // new type will have only one of RESOLVED|UNRESOLVED bits set
124
newType = newType | (change.getType() & ~(BundleDelta.RESOLVED | BundleDelta.UNRESOLVED));
125         change.setType(newType);
126         change.setBundle(resolved);
127     }
128 }
129
Popular Tags