|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.semver; |
| 10 | + |
| 11 | +import org.opensearch.Version; |
| 12 | +import org.opensearch.common.Nullable; |
| 13 | +import org.opensearch.core.xcontent.ToXContentFragment; |
| 14 | +import org.opensearch.core.xcontent.XContentBuilder; |
| 15 | +import org.opensearch.semver.expr.Caret; |
| 16 | +import org.opensearch.semver.expr.Equal; |
| 17 | +import org.opensearch.semver.expr.Expression; |
| 18 | +import org.opensearch.semver.expr.Tilde; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Objects; |
| 22 | +import java.util.Optional; |
| 23 | + |
| 24 | +import static java.util.Arrays.stream; |
| 25 | + |
| 26 | +/** |
| 27 | + * Represents a single semver range that allows for specifying which {@code org.opensearch.Version}s satisfy the range. |
| 28 | + * It is composed of a range version and a range operator. Following are the supported operators: |
| 29 | + * <ul> |
| 30 | + * <li>'=' Requires exact match with the range version. For example, =1.2.3 range would match only 1.2.3</li> |
| 31 | + * <li>'~' Allows for patch version variability starting from the range version. For example, ~1.2.3 range would match versions greater than or equal to 1.2.3 but less than 1.3.0</li> |
| 32 | + * <li>'^' Allows for patch and minor version variability starting from the range version. For example, ^1.2.3 range would match versions greater than or equal to 1.2.3 but less than 2.0.0</li> |
| 33 | + * </ul> |
| 34 | + */ |
| 35 | +public class SemverRange implements ToXContentFragment { |
| 36 | + |
| 37 | + private final Version rangeVersion; |
| 38 | + private final RangeOperator rangeOperator; |
| 39 | + |
| 40 | + public SemverRange(final Version rangeVersion, final RangeOperator rangeOperator) { |
| 41 | + this.rangeVersion = rangeVersion; |
| 42 | + this.rangeOperator = rangeOperator; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructs a {@code SemverRange} from its string representation. |
| 47 | + * @param range given range |
| 48 | + * @return a {@code SemverRange} |
| 49 | + */ |
| 50 | + public static SemverRange fromString(final String range) { |
| 51 | + RangeOperator rangeOperator = RangeOperator.fromRange(range); |
| 52 | + String version = range.replaceFirst(rangeOperator.asEscapedString(), ""); |
| 53 | + if (!Version.stringHasLength(version)) { |
| 54 | + throw new IllegalArgumentException("Version cannot be empty"); |
| 55 | + } |
| 56 | + return new SemverRange(Version.fromString(version), rangeOperator); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * Return the range operator for this range. |
| 61 | + * @return range operator |
| 62 | + */ |
| 63 | + public RangeOperator getRangeOperator() { |
| 64 | + return rangeOperator; |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * Return the version for this range. |
| 69 | + * @return the range version |
| 70 | + */ |
| 71 | + public Version getRangeVersion() { |
| 72 | + return rangeVersion; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Check if range is satisfied by given version string. |
| 77 | + * |
| 78 | + * @param versionToEvaluate version to check |
| 79 | + * @return {@code true} if range is satisfied by version, {@code false} otherwise |
| 80 | + */ |
| 81 | + public boolean isSatisfiedBy(final String versionToEvaluate) { |
| 82 | + return isSatisfiedBy(Version.fromString(versionToEvaluate)); |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Check if range is satisfied by given version. |
| 87 | + * |
| 88 | + * @param versionToEvaluate version to check |
| 89 | + * @return {@code true} if range is satisfied by version, {@code false} otherwise |
| 90 | + * @see #isSatisfiedBy(String) |
| 91 | + */ |
| 92 | + public boolean isSatisfiedBy(final Version versionToEvaluate) { |
| 93 | + return this.rangeOperator.expression.evaluate(this.rangeVersion, versionToEvaluate); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean equals(@Nullable final Object o) { |
| 98 | + if (this == o) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + if (o == null || getClass() != o.getClass()) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + SemverRange range = (SemverRange) o; |
| 105 | + return Objects.equals(rangeVersion, range.rangeVersion) && rangeOperator == range.rangeOperator; |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public int hashCode() { |
| 110 | + return Objects.hash(rangeVersion, rangeOperator); |
| 111 | + } |
| 112 | + |
| 113 | + @Override |
| 114 | + public String toString() { |
| 115 | + return rangeOperator.asString() + rangeVersion; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public XContentBuilder toXContent(final XContentBuilder builder, final Params params) throws IOException { |
| 120 | + return builder.value(toString()); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * A range operator. |
| 125 | + */ |
| 126 | + public enum RangeOperator { |
| 127 | + |
| 128 | + EQ("=", new Equal()), |
| 129 | + TILDE("~", new Tilde()), |
| 130 | + CARET("^", new Caret()), |
| 131 | + DEFAULT("", new Equal()); |
| 132 | + |
| 133 | + private final String operator; |
| 134 | + private final Expression expression; |
| 135 | + |
| 136 | + RangeOperator(final String operator, final Expression expression) { |
| 137 | + this.operator = operator; |
| 138 | + this.expression = expression; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * String representation of the range operator. |
| 143 | + * |
| 144 | + * @return range operator as string |
| 145 | + */ |
| 146 | + public String asString() { |
| 147 | + return operator; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * Escaped string representation of the range operator, |
| 152 | + * if operator is a regex character. |
| 153 | + * |
| 154 | + * @return range operator as escaped string, if operator is a regex character |
| 155 | + */ |
| 156 | + public String asEscapedString() { |
| 157 | + if (Objects.equals(operator, "^")) { |
| 158 | + return "\\^"; |
| 159 | + } |
| 160 | + return operator; |
| 161 | + } |
| 162 | + |
| 163 | + public static RangeOperator fromRange(final String range) { |
| 164 | + Optional<RangeOperator> rangeOperator = stream(values()).filter( |
| 165 | + operator -> operator != DEFAULT && range.startsWith(operator.asString()) |
| 166 | + ).findFirst(); |
| 167 | + return rangeOperator.orElse(DEFAULT); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments