Skip to content

Commit

Permalink
Fix the logic to return the old value when unchanged
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed Sep 24, 2024
1 parent 691cecd commit e5ac889
Showing 1 changed file with 25 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private <T> List<T> map(List<T> resources, Function<T, T> mapper) {
for (int i = 0; i < resources.size(); i++) {
T resource = resources.get(i);
T newResource = mapper.apply(resource);
if (newResource != null) {
if (newResource != resource) {
if (newResources == null) {
newResources = new ArrayList<>(resources);
}
Expand All @@ -105,29 +105,39 @@ private <T> List<T> map(List<T> resources, Function<T, T> mapper) {
return newResources;
}

/**
* Returns a resource with all properties identical to the given resource, except the paths
* which are resolved according the given {@code basedir}. If the paths are unchanged, then
* this method returns the previous instance.
*
* @param resource the resource to relocate, or {@code null}
* @param basedir the new base directory
* @return relocated resource, or {@code null} if the given resource was null
*/
@SuppressWarnings("StringEquality") // Identity comparison is ok in this method.
private Resource alignToBaseDirectory(Resource resource, Path basedir) {
if (resource != null) {
String newDir = mayAlignToBaseDirectoryOrNull(resource.getDirectory(), basedir);
if (newDir != null) {
return resource.withDirectory(newDir);
String oldDir = resource.getDirectory();
String newDir = alignToBaseDirectory(oldDir, basedir);
if (newDir != oldDir) {
return resource.with().directory(newDir).build();
}
}
return resource;
}

private String alignToBaseDirectory(String path, Path basedir) {
String newPath = mayAlignToBaseDirectoryOrNull(path, basedir);
if (newPath != null) {
return newPath;
}
return path;
}

/**
* Returns aligned path or {@code null} if no need for change.
* Returns a path relocated to the given base directory. If the result of this operation
* is the same path as before, then this method returns the old {@code path} instance.
* It is okay for the caller to compare the {@link String} instances using the identity
* comparator for detecting changes.
*
* @param path the path to relocate, or {@code null}
* @param basedir the new base directory
* @return relocated path, or {@code null} if the given path was null
*/
private String mayAlignToBaseDirectoryOrNull(String path, Path basedir) {
private String alignToBaseDirectory(String path, Path basedir) {
String newPath = pathTranslator.alignToBaseDirectory(path, basedir);
return Objects.equals(path, newPath) ? null : newPath;
return Objects.equals(path, newPath) ? path : newPath;
}
}

0 comments on commit e5ac889

Please sign in to comment.