ravenclaw例子阅读_CRegistry类

代码

package dmcore.agents.coreagents;

import java.util.HashMap;

import dmcore.agents.mytypedef.AgentFactory;

import android.util.Log;

interface FRegisterAgent{
	public void DeclareAgentInterface(String sAName);
}

public class CRegistry {
	// D: the AgentsRegistry object
	public static CRegistry AgentsRegistry = new CRegistry();
	// ----------------------------------------------------------------------------
	// private members
	// ---------------------------------------------------------------------------

	// hash holding agent name -> agent mapping
	private HashMap<String, CAgent> AgentsHash = new HashMap<String, CAgent>();
	// hash holding agent type -> agent mapping
	private HashMap<String, AgentFactory> AgentsTypeHash = new HashMap<String, AgentFactory>();

	// -----------------------------------------------------------------------------
	// Constructors and Destructors
	// -----------------------------------------------------------------------------

	// D: Constructor
	public CRegistry() {
		// nothing here
		Log.d("TEST","Constructor completed");
	}

	// Log Tag
	private static final String REGISTRY_STREAM_TAG = "REGISTRY";

	// D: Initializes the registry, empties everything
	public void Clear() {
		Log.d(REGISTRY_STREAM_TAG, "Clearing up the registry ...");
		// clear the hashes
		AgentsHash.clear();
		AgentsTypeHash.clear();
		Log.d(REGISTRY_STREAM_TAG,
				"Clearing up remaining registered agents completed.");
	}

	// -----------------------------------------------------------------------------
	//
	// Registry specific functions for registered agent names
	//
	// -----------------------------------------------------------------------------

	// D: return a pointer to an agent, given the agent's name. Returns NULL if
	//  the agent is not found
	public CAgent getAgentGivenName(String sAgentName) {
		if(!AgentsHash.containsKey(sAgentName)) {
			// if the agent is not found, return NULL
			return null;
		} else {
			// otherwise, return the pointer to the agent
			return AgentsHash.get(sAgentName);
		}
	}
	// D: return true if the agent is already registered
	public boolean IsRegisteredAgent(String sAgentName) {
		return AgentsHash.containsKey(sAgentName);
	}

	// D: register an agent into the registry.
	public void RegisterAgent(String sAgentName, CAgent pAgent) {
		// check that there's no agent already registered under the same name
		if (IsRegisteredAgent(sAgentName)) {
			Log.e(REGISTRY_STREAM_TAG,
					"An agent already registered under the same name ("
							+ sAgentName + ") was found.");
		}
		// register the agent
		AgentsHash.put(sAgentName, pAgent);
		// and log that
		Log.d(REGISTRY_STREAM_TAG, "Agent " + sAgentName
				+ " registered successfully.");
	}

	// D: unregister an agent
	public void UnRegisterAgent(String sAgentName) {
		if (AgentsHash.remove(sAgentName) == null) {
			Log.e(REGISTRY_STREAM_TAG, "Could not find agent " + sAgentName
					+ " to unregister.");
		}
		// and log that
		Log.d(REGISTRY_STREAM_TAG, "Agent " + sAgentName
				+ " unregistered successfully.");
	}
	
	// D: return a pointer to an agent, given the agent's name. Returns NULL if
	//  the agent is not found
	CAgent getAgentPointer(String sAgentName) {
		if(!AgentsHash.containsKey(sAgentName)){
			// if the agent is not found, return NULL
			return null;
		} else {
			// otherwise, return the pointer to the agent
			return AgentsHash.get(sAgentName);
		}
	}

	// -----------------------------------------------------------------------------
	//
	// Registry specific functions for registered agent types
	//
	// -----------------------------------------------------------------------------

	// D: return true if the agent type is already registered
	public boolean IsRegisteredAgentType(String sAgentTypeName) {
		return AgentsTypeHash.containsKey(sAgentTypeName);
	}

	// D: register an agent type into the registry.
	public void RegisterAgentType(String sAgentTypeName, AgentFactory cCreateAgent) {
		// check that there's no agent already registered under the same name
		if (IsRegisteredAgentType(sAgentTypeName)) {
			Log.e(REGISTRY_STREAM_TAG,
					"An agent type already registered under the same name ("
							+ sAgentTypeName + ") was found.");
		}

		// register the agent type
		AgentsTypeHash.put(sAgentTypeName, cCreateAgent);

		// and log that
		Log.d(REGISTRY_STREAM_TAG, "Agent type " + sAgentTypeName
				+ " registered successfully.");
	}

	// D: unregister an agent type
	public void UnRegisterAgentType(String sAgentTypeName) {
		if (AgentsTypeHash.remove(sAgentTypeName) == null) {
			Log.e(REGISTRY_STREAM_TAG, "Could not find agent type"
					+ sAgentTypeName + " to unregister.");
		}

		// and log that
		Log.d(REGISTRY_STREAM_TAG, "Agent type " + sAgentTypeName
				+ " unregistered successfully.");
	}
	
	// D: create a new agent from a given agent type
	public CAgent CreateAgent(String sAgentTypeName, String sAgentName) {
		
		// default AgentConfiguration value
		String sAgentConfiguration="";
		// test if the agent type is in the registry
		if(!AgentsTypeHash.containsKey(sAgentTypeName)) {
			// if the agent type is not in the registry, we're in bad shape
			Log.e(REGISTRY_STREAM_TAG,"Could not create agent of type " + sAgentTypeName + 
					   ". Type not found in the registry.");
			return null;
		} else {
			// if we found the agent, call the create method.
			// AgentFactory dynamically calls the create method.
			CAgent pNewAgent = AgentsTypeHash.get(sAgentTypeName).AgentFactory(sAgentName, sAgentConfiguration);
			if(pNewAgent != null) {
				// call the create function for the agent
				pNewAgent.Create();
				Log.d(REGISTRY_STREAM_TAG, "Agent "+sAgentName+" created successfully.");
				// and return a pointer to it
				return pNewAgent;
			} else {
				// if creation failed, trigger a fatal error
				Log.e(REGISTRY_STREAM_TAG,"Error creating agent of type " + sAgentTypeName + ".");
				return null;
			}
		}
	}
	// OverLoad: create a new agent from a given agent type,name and configuration
	public CAgent CreateAgent(String sAgentTypeName, String sAgentName,String sAgentConfiguration) {
		
		// test if the agent type is in the registry
		if(!AgentsTypeHash.containsKey(sAgentTypeName)) {
			// if the agent type is not in the registry, we're in bad shape
			Log.e(REGISTRY_STREAM_TAG,"Could not create agent of type " + sAgentTypeName + 
					   ". Type not found in the registry.");
			return null;
		} else {
			// if we found the agent, call the create method.
			// AgentFactory dynamically calls the create method.
			CAgent pNewAgent = AgentsTypeHash.get(sAgentTypeName).AgentFactory(sAgentName, sAgentConfiguration);
			if(pNewAgent != null) {
				// call the create function for the agent
				pNewAgent.Create();
				Log.d(REGISTRY_STREAM_TAG, "Agent "+sAgentName+" created successfully.");
				// and return a pointer to it
				return pNewAgent;
			} else {
				// if creation failed, trigger a fatal error
				Log.e(REGISTRY_STREAM_TAG,"Error creating agent of type " + sAgentTypeName + ".");
					return null;
				}
			}
		}
}

这段代码定义了一个名为 CRegistry 的类,用于管理和注册代理(agents)。以下是这段代码的主要功能和组成部分的总结:

类定义

  • 包名dmcore.agents.coreagents
  • 导入java.util.HashMap, dmcore.agents.mytypedef.AgentFactory, android.util.Log

成员变量

  • AgentsRegistry:静态成员,用于存储全局唯一的 CRegistry 实例。
  • AgentsHash:私有成员,类型为 HashMap<String, CAgent>,用于存储代理名称到代理对象的映射。
  • AgentsTypeHash:私有成员,类型为 HashMap<String, AgentFactory>,用于存储代理类型名称到创建代理的工厂方法的映射。

构造函数

  • CRegistry():构造函数,用于创建 CRegistry 的实例。构造函数中记录了构造完成的日志信息。

方法

  • Clear():清空所有的代理和代理类型映射。
  • getAgentGivenName(String sAgentName):根据代理名称获取代理对象。如果找不到相应的代理,则返回 null
  • IsRegisteredAgent(String sAgentName):判断指定名称的代理是否已经被注册。
  • RegisterAgent(String sAgentName, CAgent pAgent):注册代理到 AgentsHash 中。如果代理名称已存在,则记录错误日志。
  • UnRegisterAgent(String sAgentName):从 AgentsHash 中移除指定名称的代理。如果找不到相应的代理,则记录错误日志。
  • getAgentPointer(String sAgentName):与 getAgentGivenName 功能相同,用于获取指定名称的代理对象。

  • IsRegisteredAgentType(String sAgentTypeName):判断指定类型的代理是否已经被注册。
  • RegisterAgentType(String sAgentTypeName, AgentFactory cCreateAgent):注册代理类型到 AgentsTypeHash 中。如果代理类型已存在,则记录错误日志。
  • UnRegisterAgentType(String sAgentTypeName):从 AgentsTypeHash 中移除指定类型的代理。如果找不到相应的代理类型,则记录错误日志。
  • CreateAgent(String sAgentTypeName, String sAgentName):根据代理类型名称创建一个新的代理对象。如果找不到相应的代理类型,则记录错误日志并返回 null。如果创建成功,则记录日志信息并返回代理对象。
  • CreateAgent(String sAgentTypeName, String sAgentName, String sAgentConfiguration):与 CreateAgent(String sAgentTypeName, String sAgentName) 功能类似,但额外提供代理的配置信息。

日志记录

  • Log.d:记录调试级别的日志。
  • Log.e:记录错误级别的日志。

总结

这个类主要负责管理代理对象代理类型的注册、查询、创建和删除等功能。

它提供了一种机制来动态创建和管理不同类型的代理对象,使得应用程序可以根据需要灵活地添加、删除或修改代理。

此外,它还提供了一些方法来检查代理和代理类型是否已被注册,以及获取已注册的代理对象

分析01

我怎么调用注册代理的方法进行注册呢?就是:RegisterAgent方法

因为这个类当中定义了一个静态实例AgentsRegistry

所以我可以直接调用:CRegistry.AgentsRegistry.RegisterAgent

AgentsHashAgentsTypeHash 在应用运行时会一直存在,除非你调用 Clear() 方法来清空它们。

即使调用了 Clear() 方法,这两个哈希表本身依然存在,只是它们的内容被清空了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值