gpt4 book ai didi

java - 调用等待 HashMap 更改的函数

转载 作者:太空宇宙 更新时间:2023-11-04 06:37:59 24 4
gpt4 key购买 nike

使用 API 时 Pircbot (Irc Bot API)我需要调用一个方法,该方法等待直到 hasmap 从不同的方法更改。例子:用户执行命令 !isRegistered。现在 Pircbot 检查用户是否已被 NickServ 注册。

@Override
protected void onMessage(String channel, String sender, String login, String hostname, String message) {
if (command("isRegistered", message) && checkRegistered(sender)) {
sendMessage(sender,"You are registered);
}
};

我想,一个HashMap<String, Boolean>这是执行此操作的正确方法。

private void checkRegistered(String sender) {
checkRegister.put(Sender,null);
sendMessage("NickServ", "info sender");
//Wait till onNotices changes the HashMap so that i can get an result (hasmap content: null -> true, or false) and returning this result)

//A simple Thread.sleep(); does not work, because everything runs on the main Thread. -> When sleeping the bot times out, because it can't reply to the /ping command from the server.
}

每当 pircbot 收到通知时,我都可以获取字符串

@Override
protected void onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice) {

所以我这样做了:

@Override
protected void onNotice(String sourceNick, String sourceLogin, String sourceHostname, String target, String notice) {
if (sourceLogin.equals("NickServ")) {
for (String s : checkRegister.keySet()) {
if (notice.contains("Nickname:") && notice.contains(s)) {
checkRegister.put(s, notice.contains("<< ONLINE >>"));
}
}
}
}

我现在如何检查 hasmap 是否发生了变化,例如checkRegistered(); 内从 null -> true 或从 null -> false方法?

最佳答案

您可以混合使用 Observer-Observable 和 HashMap。

一开始,客户端将自己注册为观察者或 HashMap,当映射中发生变化时,就会向所有观察者发送一条消息。

观察者示例代码:

http://javarevisited.blogspot.ca/2011/12/observer-design-pattern-java-example.html http://www.journaldev.com/1739/observer-design-pattern-in-java-example-tutorial

具体来说,对于您的问题“我现在如何检查 hasmap 是否发生更改,例如在 checkRegistered(); 方法中从 null -> true 或从 null -> false?”

客户端将收到属性值从一个值更改为另一个值的通知,这意味着消息将包含属性名称、当前值和新值。

关于java - 调用等待 HashMap 更改的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25089800/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com