public Collection<Group> getSharedGroups(String username) {
        Collection<Group> answer = new HashSet<>();
        Collection<Group> groups = GroupManager.getInstance().getSharedGroups(username);
        for (Group group : groups) {
            final SharedGroupVisibility sharedWith = group.getSharedWith();
            if (SharedGroupVisibility.usersOfGroups == sharedWith) {
                if (group.isUser(username)) {
                    // The user belongs to the group so add the group to the answer
                    answer.add(group);
                }
                else {
                    // Check if the user belongs to a group that may see this group
                    Collection<Group> groupList = parseGroups(group.getSharedWithUsersInGroupNames());
                    for (Group groupInList : groupList) {
                        if (groupInList.isUser(username)) {
                            answer.add(group);
                        }
                    }
                }
            }
            else if (SharedGroupVisibility.everybody == sharedWith) {
                // Anyone can see this group so add the group to the answer
                answer.add(group);
            }
        }
        return answer;
    }
Returns a collection with all the groups that the user may include in his roster. The
following criteria will be used to select the groups: 1) Groups that are configured so that
everybody can include in his roster, 2) Groups that are configured so that its users may
include the group in their rosters and the user is a group user of the group and 3) User
belongs to a Group that may see a Group that whose members may include the Group in their
rosters.

@param username the username of the user to return his shared groups.
@return a collection with all the groups that the user may include in his roster.
xmppserver/src/main/java/org/jivesoftware/openfire/roster/RosterManager.java