Merge "Add intersection to GroupMembership"
This commit is contained in:
@@ -39,6 +39,20 @@ public interface GroupMembership {
|
|||||||
*/
|
*/
|
||||||
boolean containsAnyOf(Iterable<AccountGroup.UUID> groupIds);
|
boolean containsAnyOf(Iterable<AccountGroup.UUID> groupIds);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns a set containing an input member of {@code contains(id)} is true.
|
||||||
|
* <p>
|
||||||
|
* This is batch form of contains that returns specific group information.
|
||||||
|
* Implementors may implement the method as:
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* Set<AccountGroup.UUID> r = Sets.newHashSet();
|
||||||
|
* for (AccountGroup.UUID id : groupIds)
|
||||||
|
* if (contains(id)) r.add(id);
|
||||||
|
* </pre>
|
||||||
|
*/
|
||||||
|
Set<AccountGroup.UUID> intersection(Iterable<AccountGroup.UUID> groupIds);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the set of groups that can be determined by the implementation.
|
* Returns the set of groups that can be determined by the implementation.
|
||||||
* This may not return all groups the {@link #contains(AccountGroup.UUID)}
|
* This may not return all groups the {@link #contains(AccountGroup.UUID)}
|
||||||
|
@@ -70,6 +70,17 @@ public class IncludingGroupMembership implements GroupMembership {
|
|||||||
return findIncludedGroup(query);
|
return findIncludedGroup(query);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<AccountGroup.UUID> intersection(Iterable<AccountGroup.UUID> groupIds) {
|
||||||
|
Set<AccountGroup.UUID> r = Sets.newHashSet();
|
||||||
|
for (AccountGroup.UUID id : groupIds) {
|
||||||
|
if (contains(id)) {
|
||||||
|
r.add(id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
|
||||||
private boolean findIncludedGroup(Set<AccountGroup.UUID> query) {
|
private boolean findIncludedGroup(Set<AccountGroup.UUID> query) {
|
||||||
boolean found = false;
|
boolean found = false;
|
||||||
while (!found && !groupQueue.isEmpty()) {
|
while (!found && !groupQueue.isEmpty()) {
|
||||||
|
@@ -24,7 +24,6 @@ import java.util.Set;
|
|||||||
* GroupMembership over an explicit list.
|
* GroupMembership over an explicit list.
|
||||||
*/
|
*/
|
||||||
public class ListGroupMembership implements GroupMembership {
|
public class ListGroupMembership implements GroupMembership {
|
||||||
|
|
||||||
private final Set<AccountGroup.UUID> groups;
|
private final Set<AccountGroup.UUID> groups;
|
||||||
|
|
||||||
public ListGroupMembership(Iterable<AccountGroup.UUID> groupIds) {
|
public ListGroupMembership(Iterable<AccountGroup.UUID> groupIds) {
|
||||||
@@ -46,6 +45,11 @@ public class ListGroupMembership implements GroupMembership {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<AccountGroup.UUID> intersection(Iterable<AccountGroup.UUID> groupIds) {
|
||||||
|
return Sets.intersection(ImmutableSet.copyOf(groupIds), groups);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<AccountGroup.UUID> getKnownGroups() {
|
public Set<AccountGroup.UUID> getKnownGroups() {
|
||||||
return Sets.newHashSet(groups);
|
return Sets.newHashSet(groups);
|
||||||
|
@@ -156,6 +156,26 @@ public class UniversalGroupBackend implements GroupBackend {
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<AccountGroup.UUID> intersection(Iterable<AccountGroup.UUID> uuids) {
|
||||||
|
Multimap<GroupMembership, AccountGroup.UUID> lookups =
|
||||||
|
ArrayListMultimap.create();
|
||||||
|
for (AccountGroup.UUID uuid : uuids) {
|
||||||
|
GroupMembership m = membership(uuid);
|
||||||
|
if (m == null) {
|
||||||
|
log.warn("Unknown GroupMembership for UUID: " + uuid);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
lookups.put(m, uuid);
|
||||||
|
}
|
||||||
|
Set<AccountGroup.UUID> groups = Sets.newHashSet();
|
||||||
|
for (Map.Entry<GroupMembership, Collection<AccountGroup.UUID>> entry
|
||||||
|
: lookups.asMap().entrySet()) {
|
||||||
|
groups.addAll(entry.getKey().intersection(entry.getValue()));
|
||||||
|
}
|
||||||
|
return groups;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<AccountGroup.UUID> getKnownGroups() {
|
public Set<AccountGroup.UUID> getKnownGroups() {
|
||||||
Set<AccountGroup.UUID> groups = Sets.newHashSet();
|
Set<AccountGroup.UUID> groups = Sets.newHashSet();
|
||||||
|
Reference in New Issue
Block a user