boolean hasMutualVisibility(String user, Collection<Group> groups, JID otherUser,
            Collection<Group> otherGroups) {
        for (Group group : groups) {
            for (Group otherGroup : otherGroups) {
                // Skip this groups if the users are not group users of the groups
                if (!group.isUser(user) || !otherGroup.isUser(otherUser)) {
                    continue;
                }
                if (group.equals(otherGroup)) {
                     return true;
                }
                SharedGroupVisibility showInRoster = group.getSharedWith();
                SharedGroupVisibility otherShowInRoster = otherGroup.getSharedWith();
                // Return true if both groups are public groups (i.e. anybody can see them)
                if (SharedGroupVisibility.everybody == showInRoster && SharedGroupVisibility.everybody == otherShowInRoster) {
                    return true;
                }
                else if (SharedGroupVisibility.usersOfGroups == showInRoster && SharedGroupVisibility.usersOfGroups == otherShowInRoster) {
                    List<String> groupNames = group.getSharedWithUsersInGroupNames();
                    List<String> otherGroupNames = otherGroup.getSharedWithUsersInGroupNames();
                    // Return true if each group may see the other group
                    if (groupNames != null && otherGroupNames != null) {
                        if (groupNames.contains(otherGroup.getName()) &&
                                otherGroupNames.contains(group.getName())) {
                            return true;
                        }
                        // Check if each shared group can be seen by a group where each user belongs
                        Collection<Group> groupList = parseGroups(groupNames);
                        Collection<Group> otherGroupList = parseGroups(otherGroupNames);
                        for (Group groupName : groupList) {
                            if (groupName.isUser(otherUser)) {
                                for (Group otherGroupName : otherGroupList) {
                                    if (otherGroupName.isUser(user)) {
                                        return true;
                                    }
                                }
                            }
                        }
                    }
                }
                else if (SharedGroupVisibility.everybody == showInRoster && SharedGroupVisibility.usersOfGroups == otherShowInRoster) {
                    // Return true if one group is public and the other group allowed the public
                    // group to see him
                    List<String> otherGroupNames = otherGroup.getSharedWithUsersInGroupNames();
                    if (otherGroupNames != null && otherGroupNames.contains(group.getName())) {
                            return true;
                    }
                }
                else if (SharedGroupVisibility.usersOfGroups == showInRoster && SharedGroupVisibility.everybody == otherShowInRoster) {
                    // Return true if one group is public and the other group allowed the public
                    // group to see him
                    List<String> groupNames = group.getSharedWithUsersInGroupNames();
                    // Return true if each group may see the other group
                    if (groupNames != null && groupNames.contains(otherGroup.getName())) {
                            return true;
                    }
                }
            }
        }
        return false;
    }
Returns true if a group in the first collection may mutually see a group of the
second collection. More precisely, return true if both collections contain a public
group (i.e. anybody can see the group) or if both collection have a group that may see
each other and the users are members of those groups or if one group is public and the
other group allowed the public group to see it.

@param user the name of the user associated to the first collection of groups. This is always a local user.
@param groups a collection of groups to check against the other collection of groups.
@param otherUser the JID of the user associated to the second collection of groups.
@param otherGroups the other collection of groups to check against the first collection.
@return true if a group in the first collection may mutually see a group of the
        second collection.
xmppserver/src/main/java/org/jivesoftware/openfire/roster/RosterManager.java