Skip to content

Commit

Permalink
Merge pull request #45045 from aloubyansky/dep-any-flag
Browse files Browse the repository at this point in the history
Minor change to add Dependency.isAnyFlagSet(int flags) and make FlagDependencyIterator use that
  • Loading branch information
aloubyansky authored Dec 11, 2024
2 parents 240cfde + 7433129 commit 47bbd25
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 12 deletions.
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

0 comments on commit 47bbd25

Please sign in to comment.