Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 11b73aaf2fcd3da2fb6850fc34bc2813bb04942e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*******************************************************************************
 * Copyright (c) 2003, 2016 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Rob Harrop - SpringSource Inc. (bug 247522)
 *******************************************************************************/
package org.eclipse.osgi.internal.resolver;

import java.util.*;
import org.eclipse.osgi.service.resolver.*;

/**
 * This class is threadsafe.
 *
 */
final class StateDeltaImpl implements StateDelta {

	private final State state;

	private final Map<BundleDescription, BundleDelta> changes = new HashMap<>();
	private ResolverHookException error;

	public StateDeltaImpl(State state) {
		this.state = state;
	}

	public BundleDelta[] getChanges() {
		synchronized (this.changes) {
			return changes.values().toArray(new BundleDelta[changes.size()]);
		}
	}

	public BundleDelta[] getChanges(int mask, boolean exact) {
		synchronized (this.changes) {
			List<BundleDelta> result = new ArrayList<>();
			for (Iterator<BundleDelta> changesIter = changes.values().iterator(); changesIter.hasNext();) {
				BundleDelta change = changesIter.next();
				if (mask == change.getType() || (!exact && (change.getType() & mask) != 0))
					result.add(change);
			}
			return result.toArray(new BundleDelta[result.size()]);
		}
	}

	public State getState() {
		return state;
	}

	public ResolverHookException getResovlerHookException() {
		return error;
	}

	void setResolverHookException(ResolverHookException error) {
		this.error = error;
	}

	void recordBundleAdded(BundleDescriptionImpl added) {
		synchronized (this.changes) {
			BundleDeltaImpl change = (BundleDeltaImpl) changes.get(added);
			if (change == null) {
				changes.put(added, new BundleDeltaImpl(added, BundleDelta.ADDED));
				return;
			}
			if (change.getType() == BundleDelta.REMOVED) {
				changes.remove(added);
				return;
			}
			int newType = change.getType();
			if ((newType & BundleDelta.REMOVED) != 0)
				newType &= ~BundleDelta.REMOVED;
			change.setType(newType | BundleDelta.ADDED);
			change.setBundle(added);
		}
	}

	void recordBundleUpdated(BundleDescriptionImpl updated) {
		synchronized (this.changes) {
			BundleDeltaImpl change = (BundleDeltaImpl) changes.get(updated);
			if (change == null) {
				changes.put(updated, new BundleDeltaImpl(updated, BundleDelta.UPDATED));
				return;
			}
			if ((change.getType() & (BundleDelta.ADDED | BundleDelta.REMOVED)) != 0)
				return;
			change.setType(change.getType() | BundleDelta.UPDATED);
			change.setBundle(updated);
		}
	}

	void recordBundleRemoved(BundleDescriptionImpl removed) {
		synchronized (this.changes) {
			BundleDeltaImpl change = (BundleDeltaImpl) changes.get(removed);
			if (change == null) {
				changes.put(removed, new BundleDeltaImpl(removed, BundleDelta.REMOVED));
				return;
			}
			if (change.getType() == BundleDelta.ADDED) {
				changes.remove(removed);
				return;
			}
			int newType = change.getType();
			if ((newType & BundleDelta.ADDED) != 0)
				newType &= ~BundleDelta.ADDED;
			change.setType(newType | BundleDelta.REMOVED);
		}
	}

	void recordBundleRemovalPending(BundleDescriptionImpl removed) {
		synchronized (this.changes) {
			BundleDeltaImpl change = (BundleDeltaImpl) changes.get(removed);
			if (change == null) {
				changes.put(removed, new BundleDeltaImpl(removed, BundleDelta.REMOVAL_PENDING));
				return;
			}
			int newType = change.getType();
			if ((newType & BundleDelta.REMOVAL_COMPLETE) != 0)
				newType &= ~BundleDelta.REMOVAL_COMPLETE;
			change.setType(newType | BundleDelta.REMOVAL_PENDING);
		}
	}

	void recordBundleRemovalComplete(BundleDescriptionImpl removed) {
		synchronized (this.changes) {
			BundleDeltaImpl change = (BundleDeltaImpl) changes.get(removed);
			if (change == null) {
				changes.put(removed, new BundleDeltaImpl(removed, BundleDelta.REMOVAL_COMPLETE));
				return;
			}
			int newType = change.getType();
			if ((newType & BundleDelta.REMOVAL_PENDING) != 0)
				newType &= ~BundleDelta.REMOVAL_PENDING;
			change.setType(newType | BundleDelta.REMOVAL_COMPLETE);
		}
	}

	void recordBundleResolved(BundleDescriptionImpl resolved, boolean result) {
		synchronized (this.changes) {
			if (resolved.isResolved() == result)
				return; // do not record anything if nothing has changed
			BundleDeltaImpl change = (BundleDeltaImpl) changes.get(resolved);
			int newType = result ? BundleDelta.RESOLVED : BundleDelta.UNRESOLVED;
			if (change == null) {
				change = new BundleDeltaImpl(resolved, newType);
				changes.put(resolved, change);
				return;
			}
			// new type will have only one of RESOLVED|UNRESOLVED bits set
			newType = newType | (change.getType() & ~(BundleDelta.RESOLVED | BundleDelta.UNRESOLVED));
			change.setType(newType);
			change.setBundle(resolved);
		}
	}
}

Back to the top