From b2ef4bbd71e3e0d510e2a0a9013d2ad1dc965ca6 Mon Sep 17 00:00:00 2001 From: "elena.parovyshnaya" Date: Thu, 18 Jun 2020 12:38:22 +0300 Subject: [PATCH 1/3] Bug 564328 API revision | conditions | rethink key interfaces > relates to 561540 base implementation of `condition` interfaces Signed-off-by: elena.parovyshnaya --- .../api/conditions/EvaluationType.java | 2 +- .../META-INF/MANIFEST.MF | 1 + .../base/conditions/BaseCondition.java | 62 +++++++++++++ .../BaseEvaluationInstructions.java | 43 +++++++++ .../conditions/BaseValidityPeriodClosed.java | 53 +++++++++++ .../base/conditions/BaseVersionMatch.java | 43 +++++++++ .../conditions/MatchingRuleCompatible.java | 51 ++++++++++ .../base/conditions/MatchingRuleDefault.java | 34 +++++++ .../conditions/MatchingRuleEquivalent.java | 52 +++++++++++ .../conditions/MatchingRuleForIdentifier.java | 36 +++++++ .../MatchingRuleGreaterOrEqual.java | 43 +++++++++ .../base/conditions/MatchingRulePerfect.java | 37 ++++++++ .../RequiredVersionVsAllowedVersion.java | 93 +++++++++++++++++++ .../base/conditions/StrictMatchingRule.java | 52 +++++++++++ 14 files changed, 601 insertions(+), 1 deletion(-) create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseCondition.java create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseEvaluationInstructions.java create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseValidityPeriodClosed.java create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseVersionMatch.java create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleCompatible.java create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleDefault.java create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleEquivalent.java create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleGreaterOrEqual.java create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRulePerfect.java create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/RequiredVersionVsAllowedVersion.java create mode 100644 bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/StrictMatchingRule.java diff --git a/bundles/org.eclipse.passage.lic.api/src/org/eclipse/passage/lic/internal/api/conditions/EvaluationType.java b/bundles/org.eclipse.passage.lic.api/src/org/eclipse/passage/lic/internal/api/conditions/EvaluationType.java index 4c0d8adbf..1ad816e5d 100644 --- a/bundles/org.eclipse.passage.lic.api/src/org/eclipse/passage/lic/internal/api/conditions/EvaluationType.java +++ b/bundles/org.eclipse.passage.lic.api/src/org/eclipse/passage/lic/internal/api/conditions/EvaluationType.java @@ -30,7 +30,7 @@ public abstract class EvaluationType { private final String identifier; protected EvaluationType(String identifier) { - Objects.requireNonNull(identifier, "Identifier is mandatory for condition type"); //$NON-NLS-1$ + Objects.requireNonNull(identifier, "EvaluationType::identifier"); //$NON-NLS-1$ this.identifier = identifier.trim().toLowerCase(); } diff --git a/bundles/org.eclipse.passage.lic.base/META-INF/MANIFEST.MF b/bundles/org.eclipse.passage.lic.base/META-INF/MANIFEST.MF index 210a3ddd3..4ed89206a 100644 --- a/bundles/org.eclipse.passage.lic.base/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.passage.lic.base/META-INF/MANIFEST.MF @@ -15,6 +15,7 @@ Export-Package: org.eclipse.passage.lic.base, org.eclipse.passage.lic.base.requirements, org.eclipse.passage.lic.base.restrictions, org.eclipse.passage.lic.internal.base;x-internal:=true, + org.eclipse.passage.lic.internal.base.conditions;x-internal:=true, org.eclipse.passage.lic.internal.base.i18n;x-internal:=true, org.eclipse.passage.lic.internal.base.permission;x-internal:=true, org.eclipse.passage.lic.internal.base.permission.observatory;x-internal:=true, diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseCondition.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseCondition.java new file mode 100644 index 000000000..d026734b9 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseCondition.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.conditions; + +import java.util.Objects; + +import org.eclipse.passage.lic.internal.api.conditions.Condition; +import org.eclipse.passage.lic.internal.api.conditions.EvaluationInstructions; +import org.eclipse.passage.lic.internal.api.conditions.ValidityPeriod; +import org.eclipse.passage.lic.internal.api.conditions.VersionMatch; + +@SuppressWarnings("restriction") +public final class BaseCondition implements Condition { + + private final String identifier; + private final VersionMatch version; + private final ValidityPeriod period; + private final EvaluationInstructions instructions; + + public BaseCondition(String identifier, VersionMatch version, ValidityPeriod period, + EvaluationInstructions instructions) { + Objects.requireNonNull(identifier, "BaseCondition::Identifier"); //$NON-NLS-1$ + Objects.requireNonNull(version, "BaseCondition::VersionMatch"); //$NON-NLS-1$ + Objects.requireNonNull(period, "BaseCondition::ValidityPeriod"); //$NON-NLS-1$ + Objects.requireNonNull(instructions, "BaseCondition::EvaluationInstructions"); //$NON-NLS-1$ + this.identifier = identifier; + this.version = version; + this.period = period; + this.instructions = instructions; + } + + @Override + public String feature() { + return identifier; + } + + @Override + public VersionMatch versionMatch() { + return version; + } + + @Override + public ValidityPeriod validityPeriod() { + return period; + } + + @Override + public EvaluationInstructions evaluationInstructions() { + return instructions; + } + +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseEvaluationInstructions.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseEvaluationInstructions.java new file mode 100644 index 000000000..c15883f5a --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseEvaluationInstructions.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.conditions; + +import java.util.Objects; + +import org.eclipse.passage.lic.internal.api.conditions.EvaluationInstructions; +import org.eclipse.passage.lic.internal.api.conditions.EvaluationType; + +@SuppressWarnings("restriction") +public final class BaseEvaluationInstructions implements EvaluationInstructions { + + private final EvaluationType type; + private final String expression; + + public BaseEvaluationInstructions(EvaluationType type, String expression) { + Objects.requireNonNull(type, "BaseEvaluationInstructions::type"); //$NON-NLS-1$ + Objects.requireNonNull(expression, "BaseEvaluationInstructions::expression"); //$NON-NLS-1$ + this.type = type; + this.expression = expression; + } + + @Override + public EvaluationType type() { + return type; + } + + @Override + public String expression() { + return expression; + } + +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseValidityPeriodClosed.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseValidityPeriodClosed.java new file mode 100644 index 000000000..5f8020178 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseValidityPeriodClosed.java @@ -0,0 +1,53 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.conditions; + +import java.util.Date; +import java.util.Objects; + +import org.eclipse.passage.lic.internal.api.conditions.ValidityPeriodClosed; + +@SuppressWarnings("restriction") +public final class BaseValidityPeriodClosed implements ValidityPeriodClosed { + + private final Date from; + private final Date to; + + public BaseValidityPeriodClosed(Date from, Date to) { + Objects.requireNonNull(from, "BaseValidityPeriodClosed::from"); //$NON-NLS-1$ + Objects.requireNonNull(to, "BaseValidityPeriodClosed:to"); //$NON-NLS-1$ + if (!from.before(to)) { + throw new IllegalStateException("BaseValidityPeriodClosed: 'from' must be strictly less than 'to'"); //$NON-NLS-1$ + } + this.from = from; + this.to = to; + } + + @Override + public Date from() { + return new Date(from.getTime()); + } + + @Override + public Date to() { + return new Date(to.getTime()); + } + + @Override + public boolean valid(Date date) { + Objects.requireNonNull(date, "BaseValidityPeriodClosed::valid::date"); //$NON-NLS-1$ + return (from.before(date) || date.equals(from)) && // + (date.before(to) || date.equals(to)); + } + +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseVersionMatch.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseVersionMatch.java new file mode 100644 index 000000000..f7d82af18 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/BaseVersionMatch.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.conditions; + +import java.util.Objects; + +import org.eclipse.passage.lic.internal.api.conditions.MatchingRule; +import org.eclipse.passage.lic.internal.api.conditions.VersionMatch; + +@SuppressWarnings("restriction") +public final class BaseVersionMatch implements VersionMatch { + + private final String version; + private final MatchingRule rule; + + public BaseVersionMatch(String version, MatchingRule rule) { + Objects.requireNonNull(version, "BaseVersionMatch::Version"); //$NON-NLS-1$ + Objects.requireNonNull(rule, "BaseVersionMatch::MatchingRule"); //$NON-NLS-1$ + this.version = version; + this.rule = rule; + } + + @Override + public String version() { + return version; + } + + @Override + public MatchingRule rule() { + return rule; + } + +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleCompatible.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleCompatible.java new file mode 100644 index 000000000..db83c3637 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleCompatible.java @@ -0,0 +1,51 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.conditions; + +/** + *

+ * We check two versions {@code required} (comes from requirements) and + * {@code allowed} (originated in licenses) to find out out if the requirement + * version is s compatible to the allowed one or not + *

+ *

+ * Key segment to check is {@code minor}. For successful matching + * {@code required.minor} must be greater or equal to {@code allowed.minor}.All + * higher segments (major) are expected to be equal for successful match. All + * lower segments (service, qualifier) are ignored and do not affect the + * matching. + *

+ *

+ * So, formally, {@code required} and {@code allowed} are compatible iff + *

+ * + *

+ * {@code default required} always matches to any {@code allowed} version. + *

+ */ +public final class MatchingRuleCompatible extends StrictMatchingRule { + + @Override + public String identifier() { + return "compatible"; //$NON-NLS-1$ + } + + @Override + protected boolean safeMatch(String required, String allowed) { + return new RequiredVersionVsAllowedVersion(required, allowed).match(1); + } + +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleDefault.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleDefault.java new file mode 100644 index 000000000..5106c0500 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleDefault.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.conditions; + +@SuppressWarnings("restriction") +public final class MatchingRuleDefault extends StrictMatchingRule { + + private final StrictMatchingRule assignee; + + public MatchingRuleDefault() { + assignee = new MatchingRuleCompatible(); + } + + @Override + public String identifier() { + return assignee.identifier(); + } + + @Override + protected boolean safeMatch(String required, String allowed) { + return assignee.safeMatch(required, allowed); + } + +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleEquivalent.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleEquivalent.java new file mode 100644 index 000000000..73f5088ea --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleEquivalent.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.conditions; + +/** + *

+ * We check two versions: {@code required} (comes from requirements) and + * {@code allowed} (originated in licenses) to find out if the requirement + * version is s equivalent to the allowed one or not. + *

+ *

+ * Key segment to check is {@code service}. For successful matching + * {@code required.service} must be greater or equal to + * {@code allowed.service}.All higher segments (major, minor) are expected to be + * equal for successful match. All lower segments (qualifier) are ignored and do + * not affect the matching. + *

+ *

+ * So, formally, {@code required} and {@code allowed} are equivalent iff + *

+ * + *

+ * {@code default required} always matches to any {@code allowed} version. + *

+ */ +public final class MatchingRuleEquivalent extends StrictMatchingRule { + + @Override + public String identifier() { + return "equivalent"; //$NON-NLS-1$ + } + + @Override + protected boolean safeMatch(String required, String allowed) { + return new RequiredVersionVsAllowedVersion(required, allowed).match(2); + } + +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java new file mode 100644 index 000000000..c074937f2 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java @@ -0,0 +1,36 @@ +package org.eclipse.passage.lic.internal.base.conditions; + +import java.util.function.Supplier; + +import org.eclipse.passage.lic.internal.api.conditions.MatchingRule; + +@SuppressWarnings("restriction") +public final class MatchingRuleForIdentifier implements Supplier { + private final String identifier; + + public MatchingRuleForIdentifier(String identifier) { + this.identifier = identifier; + } + + @Override + public MatchingRule get() { + switch (identifier.toLowerCase()) { + case "compatible": { //$NON-NLS-1$ + return new MatchingRuleCompatible(); + } + case "equivalent": { //$NON-NLS-1$ + return new MatchingRuleEquivalent(); + } + case "greaterorequal": { //$NON-NLS-1$ + return new MatchingRuleGreaterOrEqual(); + } + case "perfect": { //$NON-NLS-1$ + return new MatchingRulePerfect(); + } + default: { + return new MatchingRuleDefault(); + } + } + } + +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleGreaterOrEqual.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleGreaterOrEqual.java new file mode 100644 index 000000000..92f57d9f7 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleGreaterOrEqual.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.conditions; + +/** + *

+ * We check two versions: {@code required} (comes from requirements) and + * {@code allowed} (originated in licenses) to find out if the requirement + * version is s greater or equal the the allowed one or not. + *

+ *

+ * Key segment to check is {@code major}. For successful matching + * {@code required.majour} must be greater or equal to {@code allowed.major}. + * All lower segments (minor, service, qualifier) are ignored and do not affect + * the matching. + *

+ *

+ * {@code default required} always matches to any {@code allowed} version. + *

+ */ +public final class MatchingRuleGreaterOrEqual extends StrictMatchingRule { + + @Override + public String identifier() { + return "greaterOrEqual"; //$NON-NLS-1$ + } + + @Override + protected boolean safeMatch(String required, String allowed) { + return new RequiredVersionVsAllowedVersion(required, allowed).match(0); + } + +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRulePerfect.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRulePerfect.java new file mode 100644 index 000000000..7429bbf78 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRulePerfect.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.conditions; + +import java.util.Objects; + +/** + * Does not event tries to parse the given strings. Matching is successful iff + * {@code required} string representing version is absolutely equal to + * {@code allowed}. Comparison is case sensitive. + * + * @author user + * + */ +public final class MatchingRulePerfect extends StrictMatchingRule { + + @Override + public String identifier() { + return "perfect"; //$NON-NLS-1$ + } + + @Override + protected boolean safeMatch(String required, String allowed) { + return Objects.equals(required, allowed); + } + +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/RequiredVersionVsAllowedVersion.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/RequiredVersionVsAllowedVersion.java new file mode 100644 index 000000000..5bca798f6 --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/RequiredVersionVsAllowedVersion.java @@ -0,0 +1,93 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.conditions; + +import java.util.List; +import java.util.Objects; + +import org.eclipse.passage.lic.internal.api.requirements.Requirement; +import org.eclipse.passage.lic.internal.base.version.DefaultVersion; +import org.eclipse.passage.lic.internal.base.version.NumericalVersion; +import org.eclipse.passage.lic.internal.base.version.SafeVersion; + +/** + * Define if the {@code actual} version (that originates in declared + * {@linkplain Requirement}s) fits to the {@code expected} version (which comes + * from license by means of licensing {@linkplain Condition}s. + */ +@SuppressWarnings("restriction") +public final class RequiredVersionVsAllowedVersion { + + private final String required; + private final String allowed; + private final String defaultVersion = new DefaultVersion().value(); + + public RequiredVersionVsAllowedVersion(String required, String allowed) { + Objects.requireNonNull(required, "MatchRuleParsing::requires"); //$NON-NLS-1$ + Objects.requireNonNull(allowed, "MatchRuleParsing::allowed"); //$NON-NLS-1$ + this.required = required; + this.allowed = allowed; + } + + /** + *

+ * Goes through both versions segments pair by pair. + *

+ *

+ * Expects the first {@code equals} pairs (head) to be equal; reports + * {@code do not match} result, if some equality expectation here is not met. + *

+ *

+ * Then takes the next pair for analysis. Reports {@code true} if required + * (actual)'s segment is greater or equal the the corresponding segment of + * allowed (expected) version. Reports {@code false} otherwise. + *

+ *

+ * The rest of the segments, if any, are skipped. + *

+ * + * @param equals number of version segments that are demanded to be equal before + * the next pair is analyzed for the final result. The rest of the + * version is skipped. + * @return {@code true} if the {@code actual} version value (required, from + * requirements) is greater or equal to expected version (allowed, from + * license) and {@code false} otherwise. + */ + public boolean match(int equals) { + if (Objects.equals(required, allowed)) { + return true; + } + if (defaultVersion.equals(required)) { + return true; + } + List expected = segments(allowed); + List actual = segments(required); + if (!headsAreEqual(expected, actual, equals)) { + return false; + } + return expected.get(equals) <= actual.get(equals); + } + + private List segments(String raw) { + return new NumericalVersion(new SafeVersion(raw).semantic()).get(); + } + + private boolean headsAreEqual(List expected, List actual, int length) { + for (int i = 0; i < length; i++) { + if (expected.get(i) != actual.get(i)) { + return false; + } + } + return true; + } +} diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/StrictMatchingRule.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/StrictMatchingRule.java new file mode 100644 index 000000000..b81cce3bc --- /dev/null +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/StrictMatchingRule.java @@ -0,0 +1,52 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ +package org.eclipse.passage.lic.internal.base.conditions; + +import java.util.Objects; + +import org.eclipse.passage.lic.internal.api.conditions.MatchingRule; + +/** + * Does not tolerate {@code null}s as a version for matching. + */ +@SuppressWarnings("restriction") +public abstract class StrictMatchingRule implements MatchingRule { + + @Override + public int hashCode() { + return Objects.hash(identifier()); + } + + @Override + public boolean equals(Object obj) { + if (!MatchingRule.class.isInstance(obj)) { + return false; + } + return identifier().equals(((MatchingRule) obj).identifier()); + } + + @Override + public String toString() { + return identifier(); + } + + @Override + public final boolean match(String required, String allowed) { + Objects.requireNonNull(required, "StrctMatchingRule::match::required"); //$NON-NLS-1$ + Objects.requireNonNull(allowed, "StrctMatchingRule::match::allowed"); //$NON-NLS-1$ + return safeMatch(required, allowed); + } + + protected abstract boolean safeMatch(String required, String allowed); + +} From 65e5ab034768a6104fff3e3200eb759fdbf5612d Mon Sep 17 00:00:00 2001 From: "elena.parovyshnaya" Date: Thu, 18 Jun 2020 12:40:06 +0300 Subject: [PATCH 2/3] Bug 564328 API revision | conditions | rethink key interfaces add license header Signed-off-by: elena.parovyshnaya --- .../base/conditions/MatchingRuleForIdentifier.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java index c074937f2..04339bf76 100644 --- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java @@ -1,3 +1,15 @@ +/******************************************************************************* + * Copyright (c) 2020 ArSysOp + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License 2.0 which is available at + * https://www.eclipse.org/legal/epl-2.0/. + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * ArSysOp - initial API and implementation + *******************************************************************************/ package org.eclipse.passage.lic.internal.base.conditions; import java.util.function.Supplier; From 974608710740f09e215788092bb5452c4bf3ce91 Mon Sep 17 00:00:00 2001 From: "elena.parovyshnaya" Date: Thu, 18 Jun 2020 13:13:30 +0300 Subject: [PATCH 3/3] Bug 564328 API revision | conditions | rethink key interfaces - prohibit null as a ctor argument Signed-off-by: elena.parovyshnaya --- .../lic/internal/base/conditions/MatchingRuleForIdentifier.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java index 04339bf76..ea4158a39 100644 --- a/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java +++ b/bundles/org.eclipse.passage.lic.base/src/org/eclipse/passage/lic/internal/base/conditions/MatchingRuleForIdentifier.java @@ -12,6 +12,7 @@ *******************************************************************************/ package org.eclipse.passage.lic.internal.base.conditions; +import java.util.Objects; import java.util.function.Supplier; import org.eclipse.passage.lic.internal.api.conditions.MatchingRule; @@ -21,6 +22,7 @@ public final class MatchingRuleForIdentifier implements Supplier { private final String identifier; public MatchingRuleForIdentifier(String identifier) { + Objects.requireNonNull(identifier, "MatchingRuleForIdentifier::identifier"); //$NON-NLS-1$ this.identifier = identifier; }