Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(projects): Improve project relationship iteration with VersionComparator #2963

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright Ankush1oo8, 2025.
* Copyright NEXT Ankush1oo8, 2025.
* Part of the SW360 Portal Project.
*
* SPDX-License-Identifier: EPL-2.0
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*/
package org.eclipse.sw360.common.utils;

import java.util.Comparator;

public class VersionComparator implements Comparator<String> {
@Override
public int compare(String v1, String v2) {
if (v1 == null || v2 == null) {
return (v1 == null) ? ((v2 == null) ? 0 : -1) : 1;
}
String[] parts1 = v1.split("\\.");
String[] parts2 = v2.split("\\.");

int length = Math.max(parts1.length, parts2.length);
for (int i = 0; i < length; i++) {
int num1 = (i < parts1.length) ? Integer.parseInt(parts1[i]) : 0;
int num2 = (i < parts2.length) ? Integer.parseInt(parts2[i]) : 0;

if (num1 != num2) {
return Integer.compare(num1, num2);
}
}
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
import static org.eclipse.sw360.datahandler.permissions.PermissionUtils.makePermission;
import org.eclipse.sw360.exporter.ProjectExporter;
import java.nio.ByteBuffer;

import org.eclipse.sw360.common.utils.VersionComparator;
/**
* Class for accessing the CouchDB database
*
Expand Down Expand Up @@ -991,7 +991,7 @@ private List<ProjectLink> iterateProjectRelationShips(Map<String, ProjectProject
}
projectLinkOptional.ifPresent(out::add);
}
out.sort(Comparator.comparing(ProjectLink::getName).thenComparing(ProjectLink::getVersion));
out.sort(Comparator.comparing(ProjectLink::getName).thenComparing(ProjectLink::getVersion,new VersionComparator()));
return out;
}

Expand Down Expand Up @@ -2444,7 +2444,7 @@ private List<ProjectLink> iterateProjectRelationShips(Map<String, ProjectProject
}
projectLinkOptional.ifPresent(out::add);
}
out.sort(Comparator.comparing(ProjectLink::getName).thenComparing(ProjectLink::getVersion));
out.sort(Comparator.comparing(ProjectLink::getName).thenComparing(ProjectLink::getVersion, new VersionComparator()));
return out;
}

Expand Down Expand Up @@ -2473,7 +2473,7 @@ private List<ProjectLink> iterateProjectRelationShipsWithAllReleases(Map<String,
parentNodeId, visitedIds, maxDepth, user, true, WITH_ALL_RELEASES);
projectLinkOptional.ifPresent(out::add);
}
out.sort(Comparator.comparing(ProjectLink::getName).thenComparing(ProjectLink::getVersion));
out.sort(Comparator.comparing(ProjectLink::getName).thenComparing(ProjectLink::getVersion,new VersionComparator()));
return out;
}

Expand Down