| 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 |
* EclipseSource - ongoing development |
| 14 |
*******************************************************************************/ |
| 15 |
package org.eclipse.equinox.internal.p2.metadata; |
| 16 |
|
| 17 |
import java.util.Objects; |
| 18 |
import org.eclipse.equinox.p2.metadata.IInstallableUnit; |
| 19 |
import org.eclipse.equinox.p2.metadata.IProvidedCapability; |
| 20 |
import org.eclipse.equinox.p2.metadata.IRequirement; |
| 21 |
import org.eclipse.equinox.p2.metadata.MetadataFactory; |
| 22 |
import org.eclipse.equinox.p2.metadata.VersionRange; |
| 23 |
import org.eclipse.equinox.p2.metadata.expression.IMatchExpression; |
| 24 |
import org.eclipse.equinox.p2.metadata.expression.IMemberProvider; |
| 25 |
|
| 26 |
/** |
| 27 |
* A requirement represents some external constraint on an {@link IInstallableUnit}. |
| 28 |
* Each requirement represents something an {@link IInstallableUnit} needs that |
| 29 |
* it expects to be provided by another {@link IInstallableUnit}. Requirements are |
| 30 |
* entirely generic, and are intended to be capable of representing anything that |
| 31 |
* an {@link IInstallableUnit} may need either at install time, or at runtime. |
| 32 |
* <p> |
| 33 |
* Instances of this class are handle objects and do not necessarily |
| 34 |
* reflect entities that exist in any particular profile or repository. These handle |
| 35 |
* objects can be created using {@link MetadataFactory}. |
| 36 |
* </p> |
| 37 |
* |
| 38 |
* @noimplement This interface is not intended to be implemented by clients. |
| 39 |
* @noextend This interface is not intended to be extended by clients. |
| 40 |
* @since 2.0 |
| 41 |
* @see IProvidedCapability |
| 42 |
* @see MetadataFactory#createRequirement(String, String, VersionRange, String, boolean, boolean, boolean) |
| 43 |
*/ |
| 44 |
public class Requirement implements IRequirement, IMemberProvider { |
| 45 |
public static final String MEMBER_FILTER = "filter"; //$NON-NLS-1$ |
| 46 |
public static final String MEMBER_MIN = "min"; //$NON-NLS-1$ |
| 47 |
public static final String MEMBER_MAX = "max"; //$NON-NLS-1$ |
| 48 |
public static final String MEMBER_GREEDY = "greedy"; //$NON-NLS-1$ |
| 49 |
public static final String MEMBER_MATCH = "match"; //$NON-NLS-1$ |
| 50 |
|
| 51 |
protected final IMatchExpression<IInstallableUnit> filter; |
| 52 |
protected final IMatchExpression<IInstallableUnit> matchExpression; |
| 53 |
protected final boolean greedy; |
| 54 |
protected final int min; |
| 55 |
protected final int max; |
| 56 |
protected final String description; |
| 57 |
|
| 58 |
public Requirement(IMatchExpression<IInstallableUnit> requirement, IMatchExpression<IInstallableUnit> filter, |
| 59 |
int min, int max, boolean greedy, String description) { |
| 60 |
this.matchExpression = requirement; |
| 61 |
this.filter = filter; |
| 62 |
this.min = min; |
| 63 |
this.max = max; |
| 64 |
this.greedy = greedy; |
| 65 |
this.description = description; |
| 66 |
} |
| 67 |
|
| 68 |
@Override |
| 69 |
public String toString() { |
| 70 |
StringBuilder result = new StringBuilder(); |
| 71 |
|
| 72 |
// Expression |
| 73 |
result.append(matchExpression); |
| 74 |
|
| 75 |
// Parameters |
| 76 |
Object[] params = matchExpression.getParameters(); |
| 77 |
if (params.length > 0) { |
| 78 |
result.append(" ("); //$NON-NLS-1$ |
| 79 |
for (int i = 0; i < params.length; i++) { |
| 80 |
if (i > 0) { |
| 81 |
result.append(", "); //$NON-NLS-1$ |
| 82 |
} |
| 83 |
result.append(params[i]); |
| 84 |
} |
| 85 |
result.append(')'); |
| 86 |
} |
| 87 |
|
| 88 |
return result.toString(); |
| 89 |
} |
| 90 |
|
| 91 |
@Override |
| 92 |
public int hashCode() { |
| 93 |
return Objects.hash(filter, matchExpression, min, max, greedy, matchExpression); |
| 94 |
} |
| 95 |
|
| 96 |
@Override |
| 97 |
public boolean equals(Object obj) { |
| 98 |
if (this == obj) { |
| 99 |
return true; |
| 100 |
} |
| 101 |
return obj instanceof IRequirement other // |
| 102 |
&& Objects.equals(filter, other.getFilter()) // |
| 103 |
&& min == other.getMin() && max == other.getMax() // |
| 104 |
&& greedy == other.isGreedy() // |
| 105 |
&& matchExpression.equals(other.getMatches()); |
| 106 |
} |
| 107 |
|
| 108 |
@Override |
| 109 |
public String getDescription() { |
| 110 |
return description; |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public int getMin() { |
| 115 |
return min; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public int getMax() { |
| 120 |
return max; |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public boolean isGreedy() { |
| 125 |
return greedy; |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public IMatchExpression<IInstallableUnit> getFilter() { |
| 130 |
return filter; |
| 131 |
} |
| 132 |
|
| 133 |
@Override |
| 134 |
public IMatchExpression<IInstallableUnit> getMatches() { |
| 135 |
return matchExpression; |
| 136 |
} |
| 137 |
|
| 138 |
@Override |
| 139 |
public boolean isMatch(IInstallableUnit candidate) { |
| 140 |
return matchExpression.isMatch(candidate); |
| 141 |
} |
| 142 |
|
| 143 |
@Override |
| 144 |
public Object getMember(String memberName) { |
| 145 |
return switch (memberName) { |
| 146 |
case MEMBER_FILTER -> filter; |
| 147 |
case MEMBER_MIN -> min; |
| 148 |
case MEMBER_MAX -> max; |
| 149 |
case MEMBER_GREEDY -> greedy; |
| 150 |
case MEMBER_MATCH -> matchExpression; |
| 151 |
default -> throw new IllegalArgumentException("No such member: " + memberName); //$NON-NLS-1$ |
| 152 |
}; |
| 153 |
} |
| 154 |
} |