Package com.tnsai.agents.groups
Class GroupRegistry
java.lang.Object
com.tnsai.agents.groups.GroupRegistry
Central registry for managing AgentGroups.
GroupRegistry provides a singleton access point for group management, enabling agents to join, leave, and discover groups dynamically.
Key Features
- Thread-safe group registration and lookup
- Auto-creation of groups when needed
- Agent-to-group membership tracking
- Global group discovery
Usage Example
GroupRegistry registry = GroupRegistry.getInstance();
// Get or create a group
AgentGroup group = registry.getOrCreate("research-team");
// Register an existing group
registry.register(myGroup);
// Find all groups for an agent
Set<AgentGroup> agentGroups = registry.getGroupsFor(agent);
Integration with @GroupMember
This registry is used by the agent initialization system to handle
the @GroupMember annotation's autoJoin and autoCreateGroups
parameters.
- Author:
- TnsAI Team
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionvoidclear()Clears all registrations (for testing).booleanChecks if a group exists.Gets a group by ID.Gets all registered groups.intGets the number of registered groups.getGroupsFor(Agent agent) Gets all groups that an agent is a member of.static GroupRegistryReturns the singleton instance.getOrCreate(String groupId) Gets an existing group or creates a new one if it doesn't exist.voidrecordMembership(Agent agent, AgentGroup group) Records that an agent has joined a group.voidregister(AgentGroup group) Registers an existing group.booleanRemoves a group from the registry.voidremoveMembership(Agent agent, AgentGroup group) Removes an agent's membership record from a group.
-
Method Details
-
getInstance
Returns the singleton instance.- Returns:
- the GroupRegistry instance
-
getOrCreate
Gets an existing group or creates a new one if it doesn't exist.The created group uses default configuration. For custom configuration, use
AgentGroup.builder()andregister(AgentGroup).- Parameters:
groupId- the group identifier- Returns:
- the existing or newly created group
-
get
Gets a group by ID.- Parameters:
groupId- the group identifier- Returns:
- the group if found
-
exists
Checks if a group exists.- Parameters:
groupId- the group identifier- Returns:
- true if the group exists
-
register
Registers an existing group.If a group with the same ID already exists, this method does nothing.
- Parameters:
group- the group to register- Throws:
IllegalArgumentException- if group is null
-
remove
Removes a group from the registry.This does not dissolve the group or remove members - it only removes the registry entry.
- Parameters:
groupId- the group identifier- Returns:
- true if a group was removed
-
getGroupsFor
Gets all groups that an agent is a member of.- Parameters:
agent- the agent- Returns:
- set of groups (never null, may be empty)
-
recordMembership
Records that an agent has joined a group.This is called internally when an agent joins a group.
- Parameters:
agent- the agentgroup- the group
-
removeMembership
Removes an agent's membership record from a group.This is called internally when an agent leaves a group.
- Parameters:
agent- the agentgroup- the group
-
getAllGroups
Gets all registered groups.- Returns:
- unmodifiable set of all groups
-
getGroupCount
public int getGroupCount()Gets the number of registered groups.- Returns:
- group count
-
clear
public void clear()Clears all registrations (for testing).
-