ShareGroup getOrMaybeCreatePersistedShareGroup(
        String groupId,
        boolean createIfNotExists
    ) throws GroupIdNotFoundException {
        Group group = groups.get(groupId);

        if (group == null && !createIfNotExists) {
            throw new IllegalStateException(String.format("Share group %s not found.", groupId));
        }

        if (group == null) {
            ShareGroup shareGroup = new ShareGroup(snapshotRegistry, groupId);
            groups.put(groupId, shareGroup);
            return shareGroup;
        }

        if (group.type() != SHARE) {
            // We don't support upgrading/downgrading between protocols at the moment so
            // we throw an exception if a group exists with the wrong type.
            throw new GroupIdNotFoundException(String.format("Group %s is not a share group.", groupId));
        }

        return (ShareGroup) group;
    }
Gets or maybe creates a share group.

@param groupId           The group id.
@param createIfNotExists A boolean indicating whether the group should be
                         created if it does not exist.

@return A ShareGroup.
@throws GroupIdNotFoundException if the group does not exist and createIfNotExists is false or
                                 if the group is not a consumer group.

Package private for testing.
group-coordinator/src/main/java/org/apache/kafka/coordinator/group/GroupMetadataManager.java