|
| 1 | +/* |
| 2 | + * Copyright 2012-2023 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.ws.gradle.conventions.support; |
| 18 | + |
| 19 | +import java.io.Serializable; |
| 20 | +import java.util.Objects; |
| 21 | + |
| 22 | +/** |
| 23 | + * ë A version representation that provides a major and minor identifier. For instance, |
| 24 | + * {@code 1.2.5} would have a {@code major} of "1" and {@code minor} of "1.2". |
| 25 | + * |
| 26 | + * @author Stephane Nicoll |
| 27 | + */ |
| 28 | +public class Version { |
| 29 | + |
| 30 | + private final String id; |
| 31 | + |
| 32 | + private final String major; |
| 33 | + |
| 34 | + private final String minor; |
| 35 | + |
| 36 | + private final Qualifier qualifier; |
| 37 | + |
| 38 | + private final Parts parts; |
| 39 | + |
| 40 | + Version(String id, String major, String minor, Qualifier qualifer, Parts parts) { |
| 41 | + this.id = id; |
| 42 | + this.major = major; |
| 43 | + this.minor = minor; |
| 44 | + this.qualifier = qualifer; |
| 45 | + this.parts = parts; |
| 46 | + } |
| 47 | + |
| 48 | + public static Version from(String version) { |
| 49 | + return VersionParser.safeParse(version); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * Return the version. |
| 54 | + * @return the version |
| 55 | + */ |
| 56 | + public String getId() { |
| 57 | + return this.id; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Return the major qualifier or {@code null}. |
| 62 | + * @return the major |
| 63 | + */ |
| 64 | + public String getMajor() { |
| 65 | + return this.major; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Return the minor qualifier or {@code null}. |
| 70 | + * @return the minor |
| 71 | + */ |
| 72 | + public String getMinor() { |
| 73 | + return this.minor; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Return tue {@link Qualifier} or {@code null}. |
| 78 | + * @return the qualifier |
| 79 | + */ |
| 80 | + public Qualifier getQualifier() { |
| 81 | + return this.qualifier; |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Return the elements of the version, if any. Does not apply for non-numeric version |
| 86 | + * such as a release train. |
| 87 | + * @return the parts |
| 88 | + */ |
| 89 | + public Parts getParts() { |
| 90 | + return this.parts; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Returns whether this version has the same major and minor versions as the |
| 95 | + * {@code other} version. |
| 96 | + * @param other the version to test |
| 97 | + * @return {@code true} if this version has the same major and minor, otherwise |
| 98 | + * {@code false} |
| 99 | + */ |
| 100 | + public boolean isSameMinor(Version other) { |
| 101 | + return isSameMajor(other) && Objects.equals(this.parts.minor, other.parts.minor); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Returns whether this version has the same major version as the {@code other} |
| 106 | + * version. |
| 107 | + * @param other the version to test |
| 108 | + * @return {@code true} if this version has the same major, otherwise {@code false} |
| 109 | + */ |
| 110 | + public boolean isSameMajor(Version other) { |
| 111 | + return (this.parts != null && other.parts != null && Objects.equals(this.parts.major, other.parts.major)); |
| 112 | + } |
| 113 | + |
| 114 | + @Override |
| 115 | + public boolean equals(Object o) { |
| 116 | + if (this == o) { |
| 117 | + return true; |
| 118 | + } |
| 119 | + if (!(o instanceof Version version)) { |
| 120 | + return false; |
| 121 | + } |
| 122 | + return Objects.equals(this.id, version.id); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public int hashCode() { |
| 127 | + return Objects.hashCode(this.id); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public String toString() { |
| 132 | + return this.id; |
| 133 | + } |
| 134 | + |
| 135 | + public record Parts(Integer major, Integer minor, Integer patch, Integer hotPatch) { |
| 136 | + |
| 137 | + /** |
| 138 | + * Return a unique number for this instance that allows to compare two versions. |
| 139 | + * @return a comparable number |
| 140 | + */ |
| 141 | + public long toNumber() { |
| 142 | + String paddedValue = paddedNumber(this.major) + paddedNumber(this.minor) + paddedNumber(this.patch) |
| 143 | + + paddedNumber(this.hotPatch); |
| 144 | + return Long.parseLong(paddedValue); |
| 145 | + } |
| 146 | + |
| 147 | + private String paddedNumber(Integer number) { |
| 148 | + if (number != null) { |
| 149 | + return String.format("%02d", number); |
| 150 | + } |
| 151 | + return "00"; |
| 152 | + } |
| 153 | + |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * A version qualifier. |
| 158 | + * |
| 159 | + * @param id the identifier of the qualifier |
| 160 | + * @param version the version or {@code null} |
| 161 | + * @param separator the separator |
| 162 | + */ |
| 163 | + public record Qualifier(String id, Integer version, String separator) implements Serializable { |
| 164 | + |
| 165 | + public Qualifier(String id) { |
| 166 | + this(id, null, "."); |
| 167 | + } |
| 168 | + |
| 169 | + } |
| 170 | + |
| 171 | +} |
0 commit comments