Skip to content

Commit

Permalink
rename to Tags.includesAny
Browse files Browse the repository at this point in the history
I think that better reflects the intent.
- also adjusting javadoc accordingly
- and move static methods to bottom

Signed-off-by: Christoph Rueger <[email protected]>
  • Loading branch information
chrisrueger committed May 21, 2024
1 parent 7f04909 commit 7313fbc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 26 deletions.
15 changes: 14 additions & 1 deletion biz.aQute.bndlib/src/aQute/bnd/service/Registry.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,21 @@
* A registry for objects.
*/
public interface Registry {

/**
* @param <T>
* @param c
* @return all plugins matching the given class
*/
<T> List<T> getPlugins(Class<T> c);

/**
* @param <T>
* @param c
* @param tags
* @return all plugins matching the given class and any of the given tags.
* If no tags are given, all plugins are returned without filtering.
*/
default <T> List<T> getPlugins(Class<T> c, String... tags) {

if (tags.length == 0) {
Expand All @@ -19,7 +32,7 @@ default <T> List<T> getPlugins(Class<T> c, String... tags) {

return getPlugins(c).stream()
.filter(repo -> repo instanceof Tagged taggedRepo && taggedRepo.getTags()
.isIncluded(tags))
.includesAny(tags))
.collect(Collectors.toList());
}

Expand Down
58 changes: 33 additions & 25 deletions biz.aQute.bndlib/src/aQute/bnd/service/tags/Tags.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,27 +29,6 @@ private Tags(Collection<? extends String> c) {
this.internalSet = unmodifiableSortedSet(new TreeSet<>(c));
}

public static Tags of(String... name) {
return new Tags(Set.of(name));
}

/**
* Parses a comma-separated string of tags into a Tags object.
*
* @param csvTags
* @param defaultTags a default used when csvTags is null or blank
* @return populated Tags or the passed defaultTags.
*/
public static Tags parse(String csvTags, Tags defaultTags) {
if (csvTags == null || csvTags.isBlank()) {
return defaultTags; // default
}

return new Tags(Arrays.stream(csvTags.split(","))
.map(String::trim)
.collect(Collectors.toCollection(LinkedHashSet::new)));
}

@Override
public int size() {
return internalSet.size();
Expand Down Expand Up @@ -131,14 +110,19 @@ public String toString() {
}

/**
* @param <T>
* @param tags
* @return <code>true</code> if the passed object matches any of the given
* tags, otherwise returns <code>false</code>
* @return <code>true</code> if any of the given tags is included in the
* current set of tags, otherwise returns <code>false</code>. Also
* if the current set of tags is empty, also <code>true</code> is
* returned.
*/
public <T> boolean isIncluded(String... tags) {
public boolean includesAny(String... tags) {

if (isEmpty()) {
// this is on purpose to maintain backwards compatibility for
// entities which do not handle tags yet and return an empty set. In
// other words: if the current set is
// empty that means "yes I match any of what you passed".
return true;
}

Expand All @@ -151,5 +135,29 @@ public <T> boolean isIncluded(String... tags) {
return false;
}

/**
* @param name
* @return a Tags instance with the given tags.
*/
public static Tags of(String... name) {
return new Tags(Set.of(name));
}

/**
* Parses a comma-separated string of tags into a Tags object.
*
* @param csvTags
* @param defaultTags a default used when csvTags is null or blank
* @return populated Tags or the passed defaultTags.
*/
public static Tags parse(String csvTags, Tags defaultTags) {
if (csvTags == null || csvTags.isBlank()) {
return defaultTags; // default
}

return new Tags(Arrays.stream(csvTags.split(","))
.map(String::trim)
.collect(Collectors.toCollection(LinkedHashSet::new)));
}

}

0 comments on commit 7313fbc

Please sign in to comment.