diff --git a/buildScripts/setup.ant.xml b/buildScripts/setup.ant.xml index f8fb5e51..ba15d491 100644 --- a/buildScripts/setup.ant.xml +++ b/buildScripts/setup.ant.xml @@ -201,11 +201,9 @@ This buildfile is part of projectlombok.org. It sets up the build itself. - - + - diff --git a/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java b/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java index 2391a1a9..0e8d5895 100644 --- a/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java +++ b/src/support/lombok/eclipse/dependencies/DownloadEclipseDependencies.java @@ -1,3 +1,24 @@ +/* + * Copyright (C) 2022-2024 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.eclipse.dependencies; import java.io.BufferedInputStream; diff --git a/src/support/lombok/eclipse/dependencies/UniqueQueue.java b/src/support/lombok/eclipse/dependencies/UniqueQueue.java deleted file mode 100644 index 6a97612e..00000000 --- a/src/support/lombok/eclipse/dependencies/UniqueQueue.java +++ /dev/null @@ -1,21 +0,0 @@ -package lombok.eclipse.dependencies; - -import java.util.ArrayDeque; -import java.util.HashSet; -import java.util.Set; - -/** - * An incomplete implementation of a queue that maintains a history of all queued items and ensures unique elements. - * Elements cannot be added to the queue if they already exist in the history. - */ -public class UniqueQueue extends ArrayDeque { - private Set added = new HashSet<>(); - - @Override - public boolean add(T e) { - if (added.add(e)) { - return super.add(e); - } - return false; - } -} diff --git a/src/support/lombok/eclipse/dependencies/UpdateSite.java b/src/support/lombok/eclipse/dependencies/UpdateSite.java index c3379361..40524568 100644 --- a/src/support/lombok/eclipse/dependencies/UpdateSite.java +++ b/src/support/lombok/eclipse/dependencies/UpdateSite.java @@ -1,3 +1,24 @@ +/* + * Copyright (C) 2024 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.eclipse.dependencies; import java.io.BufferedInputStream; @@ -5,11 +26,9 @@ import java.io.FileNotFoundException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; -import java.util.ArrayList; -import java.util.HashMap; +import java.util.ArrayDeque; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Queue; import java.util.Set; import java.util.stream.Collectors; @@ -25,10 +44,10 @@ import javax.xml.transform.sax.SAXSource; import org.xml.sax.InputSource; import lombok.eclipse.dependencies.model.Child; -import lombok.eclipse.dependencies.model.Provided; import lombok.eclipse.dependencies.model.Repository; import lombok.eclipse.dependencies.model.Required; import lombok.eclipse.dependencies.model.Unit; +import lombok.eclipse.dependencies.model.VersionRange; public class UpdateSite { private static final String OS_NAME = System.getProperty("os.name").toLowerCase(); @@ -38,13 +57,11 @@ public class UpdateSite { private JAXBContext jaxbContext; private Repository repository; - private Map> providesIndex; private String resolvedUrl; private SAXParserFactory saxParserFactory; public UpdateSite() throws Exception { jaxbContext = JAXBContext.newInstance(Repository.class); - providesIndex = new HashMap<>(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); @@ -72,15 +89,7 @@ public class UpdateSite { try (InputStream inputStream = readJarOrXml(resolvedUrl, "content")) { repository = unmarshalRepository(inputStream); - - // Build index - for (Unit unit : repository.units) { - for (Provided provides : unit.provides) { - providesIndex.computeIfAbsent(provides.namespace + ":" + provides.name, k -> new ArrayList<>()).add(unit); - } - } } - ; } public Set resolveWithoutDependencies(List dependencies) { @@ -92,40 +101,42 @@ public class UpdateSite { } private Set resolve(List dependencies, boolean withDependencies) { - Queue toResolve = new UniqueQueue<>(); + Queue toResolve = new ArrayDeque<>(); for (String dependency : dependencies) { - toResolve.add(dependency); + String[] split = dependency.split(":"); + Required required = new Required(); + required.namespace = split[0]; + required.name = split[1]; + required.range = VersionRange.ALL; + toResolve.add(required); } Set resolved = new HashSet<>(); while (!toResolve.isEmpty()) { - String next = toResolve.poll(); + Required next = toResolve.poll(); - List providedUnits = providesIndex.get(next); + // Skip already resolved + if (resolved.stream().anyMatch(u -> u.satisfies(next))) { + continue; + } + + List satisfyingUnits = repository.units.stream().filter(u -> u.satisfies(next)).collect(Collectors.toList()); // Skip unknown - if (providedUnits == null) { + if (satisfyingUnits.isEmpty()) { System.out.println("Skipping unknown unit " + next); continue; } - // Remove a.jre.javase dependency - List filteredProvidedUnits = providedUnits.stream() - .filter(u -> !u.id.equals("a.jre.javase")) // Remove - .collect(Collectors.toList()); - if (filteredProvidedUnits.size() == 0) { - // This is a JDK only dependency, skip + // Skip JDK dependencies + boolean jdkDependency = satisfyingUnits.stream().anyMatch(u -> u.id.equals("a.jre.javase")); + if (jdkDependency) { continue; } - // Skip ambiguous (we could use version ranges to solve that...) - if (filteredProvidedUnits.size() > 1) { - boolean alreadyResolved = filteredProvidedUnits.stream().anyMatch(resolved::contains); - if (!alreadyResolved) { - System.out.println("Ambiguous resolution for " + next + ": " + filteredProvidedUnits.toString()); - continue; - } + if (satisfyingUnits.size() > 1) { + System.out.println("Ambiguous resolution for " + next + ": " + satisfyingUnits.toString() + ", picking first"); } - Unit unit = filteredProvidedUnits.get(0); + Unit unit = satisfyingUnits.get(0); resolved.add(unit); if (withDependencies && unit.requires != null) { @@ -133,7 +144,7 @@ public class UpdateSite { if (required.optional) continue; if (!matchesFilter(required.filter)) continue; - toResolve.add(required.namespace + ":" + required.name); + toResolve.add(required); } } } diff --git a/src/support/lombok/eclipse/dependencies/model/Child.java b/src/support/lombok/eclipse/dependencies/model/Child.java index 818645e2..1aedc77f 100644 --- a/src/support/lombok/eclipse/dependencies/model/Child.java +++ b/src/support/lombok/eclipse/dependencies/model/Child.java @@ -1,3 +1,24 @@ +/* + * Copyright (C) 2023-2024 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.eclipse.dependencies.model; import javax.xml.bind.annotation.XmlAttribute; diff --git a/src/support/lombok/eclipse/dependencies/model/Provided.java b/src/support/lombok/eclipse/dependencies/model/Provided.java index b531d3e1..29762f8c 100644 --- a/src/support/lombok/eclipse/dependencies/model/Provided.java +++ b/src/support/lombok/eclipse/dependencies/model/Provided.java @@ -1,6 +1,28 @@ +/* + * Copyright (C) 2023-2024 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.eclipse.dependencies.model; import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; public class Provided { @XmlAttribute @@ -8,5 +30,11 @@ public class Provided { @XmlAttribute public String name; @XmlAttribute - public String version; + @XmlJavaTypeAdapter(VersionAdapter.class) + public Version version; + + @Override + public String toString() { + return namespace + ":" + name + "_" + version; + } } diff --git a/src/support/lombok/eclipse/dependencies/model/Repository.java b/src/support/lombok/eclipse/dependencies/model/Repository.java index 1d81bf8c..17c1b236 100644 --- a/src/support/lombok/eclipse/dependencies/model/Repository.java +++ b/src/support/lombok/eclipse/dependencies/model/Repository.java @@ -1,3 +1,24 @@ +/* + * Copyright (C) 2023-2024 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.eclipse.dependencies.model; import java.util.List; diff --git a/src/support/lombok/eclipse/dependencies/model/Required.java b/src/support/lombok/eclipse/dependencies/model/Required.java index 36fbe46e..0d5ae2d0 100644 --- a/src/support/lombok/eclipse/dependencies/model/Required.java +++ b/src/support/lombok/eclipse/dependencies/model/Required.java @@ -1,7 +1,29 @@ +/* + * Copyright (C) 2023-2024 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.eclipse.dependencies.model; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; public class Required { @XmlAttribute @@ -9,9 +31,15 @@ public class Required { @XmlAttribute public String name; @XmlAttribute - public String range; + @XmlJavaTypeAdapter(VersionRangeAdapter.class) + public VersionRange range; @XmlAttribute public boolean optional; @XmlElement public String filter; + + @Override + public String toString() { + return namespace + ":" + name + "_" + range; + } } diff --git a/src/support/lombok/eclipse/dependencies/model/Unit.java b/src/support/lombok/eclipse/dependencies/model/Unit.java index 82d7809e..781651ec 100644 --- a/src/support/lombok/eclipse/dependencies/model/Unit.java +++ b/src/support/lombok/eclipse/dependencies/model/Unit.java @@ -1,3 +1,24 @@ +/* + * Copyright (C) 2023-2024 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ package lombok.eclipse.dependencies.model; import java.util.List; @@ -5,12 +26,14 @@ import java.util.List; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElementWrapper; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; public class Unit { @XmlAttribute public String id; @XmlAttribute - public String version; + @XmlJavaTypeAdapter(VersionAdapter.class) + public Version version; @XmlElementWrapper(name = "provides") @XmlElement(name="provided") @@ -19,7 +42,11 @@ public class Unit { @XmlElementWrapper(name = "requires") @XmlElement(name="required") public List requires; - + + public boolean satisfies(Required required) { + return provides.stream().anyMatch(p -> p.namespace.equals(required.namespace) && p.name.equals(required.name) && required.range.contains(p.version)); + } + @Override public String toString() { return id + "_" + version; diff --git a/src/support/lombok/eclipse/dependencies/model/Version.java b/src/support/lombok/eclipse/dependencies/model/Version.java new file mode 100644 index 00000000..0ebcf2ef --- /dev/null +++ b/src/support/lombok/eclipse/dependencies/model/Version.java @@ -0,0 +1,66 @@ +/* + * Copyright (C) 2024 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.eclipse.dependencies.model; + +public class Version { + private int major; + private int minor; + private int micro; + private String qualifier = ""; + + public static Version parse(String v) { + String[] parts = v.split("\\.", 4); + + Version version = new Version(); + version.major = Integer.parseInt(parts[0]); + if (parts.length > 1) { + version.minor = Integer.parseInt(parts[1]); + } + if (parts.length > 2) { + version.micro = Integer.parseInt(parts[2]); + } + if (parts.length > 3) { + version.qualifier = parts[3]; + } + return version; + } + + public int compareTo(Version other) { + int result = major - other.major; + if (result != 0) return result; + + result = minor - other.minor; + if (result != 0) return result; + + result = micro - other.micro; + if (result != 0) return result; + + return qualifier.compareTo(other.qualifier); + } + + @Override + public String toString() { + String result = major + "." + minor + "." + micro; + if (!qualifier.isEmpty()) result += "." + qualifier; + return result; + } +} diff --git a/src/support/lombok/eclipse/dependencies/model/VersionAdapter.java b/src/support/lombok/eclipse/dependencies/model/VersionAdapter.java new file mode 100644 index 00000000..3c0f0ca1 --- /dev/null +++ b/src/support/lombok/eclipse/dependencies/model/VersionAdapter.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2024 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.eclipse.dependencies.model; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + +public class VersionAdapter extends XmlAdapter { + + @Override + public Version unmarshal(String v) throws Exception { + return Version.parse(v); + } + + @Override + public String marshal(Version v) throws Exception { + throw new UnsupportedOperationException(); + } + +} diff --git a/src/support/lombok/eclipse/dependencies/model/VersionRange.java b/src/support/lombok/eclipse/dependencies/model/VersionRange.java new file mode 100644 index 00000000..7dda2dce --- /dev/null +++ b/src/support/lombok/eclipse/dependencies/model/VersionRange.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2024 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.eclipse.dependencies.model; + +public class VersionRange { + public static final VersionRange ALL = VersionRange.parse("0.0.0"); + + private Version min; + private Version max; + private boolean leftInclusive; + private boolean leftExclusive; + private boolean rightInclusive; + private boolean rightExclusive; + + public static VersionRange parse(String v) { + VersionRange versionRange = new VersionRange(); + versionRange.leftInclusive = v.startsWith("["); + versionRange.leftExclusive = v.startsWith("("); + versionRange.rightInclusive = v.endsWith("]"); + versionRange.rightExclusive = v.endsWith(")"); + + if (!versionRange.leftInclusive && !versionRange.leftExclusive) { + Version version = Version.parse(v); + versionRange.min = version; + versionRange.leftInclusive = true; + return versionRange; + } + + String[] parts = v.replaceAll("[\\[\\(\\)\\]]", "").split(","); + versionRange.min = Version.parse(parts[0]); + versionRange.max = Version.parse(parts[1]); + return versionRange; + } + + public boolean contains(Version version) { + if (min != null) { + int result = min.compareTo(version); + if (result > 0) return false; + if (result == 0 && !leftInclusive) return false; + } + if (max != null) { + int result = max.compareTo(version); + if (result < 0) return false; + if (result == 0 && !rightInclusive) return false; + } + return true; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + if (leftInclusive) sb.append("["); + if (leftExclusive) sb.append("("); + if (min != null) sb.append(min); + sb.append(","); + if (max != null) sb.append(max); + if (rightInclusive) sb.append("]"); + if (rightExclusive) sb.append(")"); + return sb.toString(); + } +} diff --git a/src/support/lombok/eclipse/dependencies/model/VersionRangeAdapter.java b/src/support/lombok/eclipse/dependencies/model/VersionRangeAdapter.java new file mode 100644 index 00000000..43331817 --- /dev/null +++ b/src/support/lombok/eclipse/dependencies/model/VersionRangeAdapter.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2024 The Project Lombok Authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package lombok.eclipse.dependencies.model; + +import javax.xml.bind.annotation.adapters.XmlAdapter; + +public class VersionRangeAdapter extends XmlAdapter { + + @Override + public VersionRange unmarshal(String v) throws Exception { + return VersionRange.parse(v); + } + + @Override + public String marshal(VersionRange v) throws Exception { + throw new UnsupportedOperationException(); + } + +}