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

Minor change to add Dependency.isAnyFlagSet(int flags) and make FlagDependencyIterator use that #45045

Merged
merged 1 commit into from
Dec 11, 2024
Merged
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 @@ -40,13 +40,30 @@ public interface ApplicationModel {
*/
Iterable<ResolvedDependency> getDependencies(int flags);

/**
* Returns application dependencies that have any of the flags combined in the value of the {@code flags} arguments set.
*
* @param flags dependency flags to match
* @return application dependencies that matched the flags
*/
Iterable<ResolvedDependency> getDependenciesWithAnyFlag(int flags);

/**
* Returns application dependencies that have any of the flags passed in as arguments set.
*
* @param flags dependency flags to match
* @return application dependencies that matched the flags
*/
Iterable<ResolvedDependency> getDependenciesWithAnyFlag(int... flags);
default Iterable<ResolvedDependency> getDependenciesWithAnyFlag(int... flags) {
if (flags.length == 0) {
throw new IllegalArgumentException("Flags are empty");
}
int combined = flags[0];
for (int i = 1; i < flags.length; ++i) {
combined |= flags[i];
}
return getDependenciesWithAnyFlag(combined);
}

/**
* Runtime dependencies of an application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ public Collection<ResolvedDependency> getRuntimeDependencies() {

@Override
public Iterable<ResolvedDependency> getDependencies(int flags) {
return new FlagDependencyIterator(new int[] { flags });
return new FlagDependencyIterator(flags, false);
}

public Iterable<ResolvedDependency> getDependenciesWithAnyFlag(int... flags) {
return new FlagDependencyIterator(flags);
@Override
public Iterable<ResolvedDependency> getDependenciesWithAnyFlag(int flags) {
return new FlagDependencyIterator(flags, true);
}

@Override
Expand Down Expand Up @@ -118,10 +119,20 @@ private Set<ArtifactKey> collectKeys(int flags) {

private class FlagDependencyIterator implements Iterable<ResolvedDependency> {

private final int[] flags;

private FlagDependencyIterator(int[] flags) {
private final int flags;
private final boolean any;

/**
* Iterates over application model dependencies that match requested flags.
* The {@code any} boolean argument controls whether any or all the flags have to match
* for a dependency to be selected.
*
* @param flags flags to match
* @param any whether any or all of the flags have to be matched
*/
private FlagDependencyIterator(int flags, boolean any) {
this.flags = flags;
this.any = any;
}

@Override
Expand Down Expand Up @@ -152,11 +163,14 @@ public ResolvedDependency next() {

private void moveOn() {
next = null;
while (index < dependencies.size()) {
while (index < dependencies.size() && next == null) {
var d = dependencies.get(index++);
if (d.hasAnyFlag(flags)) {
if (any) {
if (d.isAnyFlagSet(flags)) {
next = d;
}
} else if (d.isFlagSet(flags)) {
next = d;
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,27 @@ default boolean isClassLoaderParentFirst() {

/**
* Checks whether a dependency has a given flag set.
* If the value of the {@code flag} argument combines multiple flags,
* the implementation will return {@code true} only if the dependency
* has all the flags set.
*
* @param flag flag to check
* @param flag flag (or flags) to check
* @return true if the flag is set, otherwise false
*/
default boolean isFlagSet(int flag) {
return (getFlags() & flag) == flag;
}

/**
* Checks whether a dependency has any of the flags combined in the value of {@code flags} set.
*
* @param flags flags to check
* @return true, if any of the flags is set, otherwise - false
*/
default boolean isAnyFlagSet(int flags) {
return (getFlags() & flags) > 0;
}

/**
* Checks whether any of the flags are set on a dependency
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public Iterable<ResolvedDependency> getDependencies(int flags) {
}

@Override
public Iterable<ResolvedDependency> getDependenciesWithAnyFlag(int... flags) {
public Iterable<ResolvedDependency> getDependenciesWithAnyFlag(int flags) {
return delegate.getDependenciesWithAnyFlag(flags);
}

Expand Down
Loading