Debian Codegraph @

bundles/org.eclipse.equinox.p2.engine/src/org/eclipse/equinox/internal/p2/engine/ProvisioningPlan.java

raw
1 /*******************************************************************************
2 * Copyright (c) 2007, 2023 IBM Corporation and others.
3 *
4 * This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
8 *
9 * SPDX-License-Identifier: EPL-2.0
10 *
11 * Contributors:
12 * IBM Corporation - initial API and implementation
13 *******************************************************************************/
14 package org.eclipse.equinox.internal.p2.engine;
15
16 import java.util.*;
17 import java.util.stream.Stream;
18 import org.eclipse.core.runtime.*;
19 import org.eclipse.equinox.p2.engine.*;
20 import org.eclipse.equinox.p2.metadata.IInstallableUnit;
21 import org.eclipse.equinox.p2.query.*;
22
23 /**
24 * @since 2.0
25 */
26 public class ProvisioningPlan implements IProvisioningPlan {
27
28 final IProfile profile;
29 final List<Operand> operands = new ArrayList<>();
30 final ProvisioningContext context;
31 IQueryable<IInstallableUnit> futureState;
32 IStatus status;
33 private IProvisioningPlan installerPlan;
34
35 public ProvisioningPlan(IProfile profile, Operand[] operands, ProvisioningContext context) {
36 this(Status.OK_STATUS, profile, operands, context, null);
37 }
38
39 public ProvisioningPlan(IStatus status, IProfile profile, ProvisioningContext context, IProvisioningPlan installerPlan) {
40 this(status, profile, null, context, installerPlan);
41 }
42
43 public ProvisioningPlan(IStatus status, IProfile profile, Operand[] operands, ProvisioningContext context, IProvisioningPlan installerPlan) {
44 Assert.isNotNull(profile);
45 this.status = status;
46 this.profile = profile;
47 if (operands != null)
48 this.operands.addAll(Arrays.asList(operands));
49 this.context = (context == null) ? new ProvisioningContext(profile.getProvisioningAgent()) : context;
50 this.installerPlan = installerPlan;
51 }
52
53 @Override
54 public IStatus getStatus() {
55 return status;
56 }
57
58 @Override
59 public void setStatus(IStatus status) {
60 this.status = status;
61 }
62
63 @Override
64 public IProfile getProfile() {
65 return profile;
66 }
67
68 public Operand[] getOperands() {
69 return operands.toArray(new Operand[operands.size()]);
70 }
71
72 @Override
73 public boolean isEmpty() {
74 return operands.isEmpty();
75 }
76
77 @Override
78 public IQueryable<IInstallableUnit> getRemovals() {
79 return new QueryablePlan(false);
80 }
81
82 @Override
83 public IQueryable<IInstallableUnit> getAdditions() {
84 return new QueryablePlan(true);
85 }
86
87 private class QueryablePlan implements IQueryable<IInstallableUnit> {
88 private boolean addition;
89
90 public QueryablePlan(boolean add) {
91 this.addition = add;
92 }
93
94 private Stream<IInstallableUnit> installableUnits() {
95 if (operands.isEmpty() || status.getSeverity() == IStatus.ERROR) {
96 return Stream.empty();
97 }
98 return operands.stream() //
99 .filter(InstallableUnitOperand.class::isInstance).map(InstallableUnitOperand.class::cast)
100 .map(addition ? InstallableUnitOperand::second : InstallableUnitOperand::first)
101 .filter(Objects::nonNull);
102 }
103
104 @Override
105 public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IProgressMonitor monitor) {
106 Iterator<IInstallableUnit> units = installableUnits().iterator();
107 if (!units.hasNext()) {
108 return Collector.emptyCollector();
109 }
110 return query.perform(units);
111 }
112
113 @Override
114 public boolean contains(IInstallableUnit element) {
115 return installableUnits().anyMatch(iu -> Objects.equals(iu, element));
116 }
117 }
118
119 @Override
120 public IProvisioningPlan getInstallerPlan() {
121 return installerPlan;
122 }
123
124 @Override
125 public ProvisioningContext getContext() {
126 return context;
127 }
128
129 @Override
130 public void setInstallerPlan(IProvisioningPlan p) {
131 installerPlan = p;
132 }
133
134 @Override
135 public void addInstallableUnit(IInstallableUnit iu) {
136 operands.add(new InstallableUnitOperand(null, iu));
137 }
138
139 @Override
140 public void removeInstallableUnit(IInstallableUnit iu) {
141 operands.add(new InstallableUnitOperand(iu, null));
142 }
143
144 @Override
145 public void updateInstallableUnit(IInstallableUnit iu1, IInstallableUnit iu2) {
146 operands.add(new InstallableUnitOperand(iu1, iu2));
147 }
148
149 @Override
150 public void setProfileProperty(String name, String value) {
151 String currentValue = profile.getProperty(name);
152 if (value == null && currentValue == null)
153 return;
154 operands.add(new PropertyOperand(name, currentValue, value));
155 }
156
157 @Override
158 public void setInstallableUnitProfileProperty(IInstallableUnit iu, String name, String value) {
159 String currentValue = profile.getInstallableUnitProperty(iu, name);
160 if (value == null && currentValue == null)
161 return;
162 operands.add(new InstallableUnitPropertyOperand(iu, name, currentValue, value));
163 }
164
165 @Override
166 public IQueryable<IInstallableUnit> getFutureState() {
167 return futureState;
168 }
169
170 @Override
171 public void setFuturePlan(IQueryable<IInstallableUnit> futureState) {
172 this.futureState = futureState;
173 }
174 }