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

Probable bug: method that nullifies model #1735

Open
wants to merge 4 commits into
base: master
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
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,18 +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 = alignToBaseDirectory(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;
}

/**
* 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 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ private <T> List<T> map(List<T> resources, Function<T, T> mapper) {

private Resource alignToBaseDirectory(Resource resource, Path basedir) {
if (resource != null) {
String newDir = alignToBaseDirectory(resource.getDirectory(), basedir);
String newDir = mayAlignToBaseDirectoryOrNull(resource.getDirectory(), basedir);
if (newDir != null) {
return resource.withDirectory(newDir);
}
Expand All @@ -125,6 +125,17 @@ private Resource alignToBaseDirectory(Resource resource, Path basedir) {
}

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.
*/
private String mayAlignToBaseDirectoryOrNull(String path, Path basedir) {
String newPath = pathTranslator.alignToBaseDirectory(path, basedir);
return Objects.equals(path, newPath) ? null : newPath;
}
Expand Down