Bug fix for array isempty checks

This commit is contained in:
Ziver Koc 2024-09-07 00:13:34 +02:00
parent 19a12dee44
commit 09cce76707
3 changed files with 13 additions and 3 deletions

View file

@ -24,6 +24,7 @@
package zutil;
import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;
@ -59,7 +60,7 @@ public class ObjectUtil {
else if (obj instanceof List)
return ((List) obj).isEmpty();
else if (obj.getClass().isArray())
return ((Object[]) obj).length == 0;
return (Array.getLength(obj) == 0);
else if (obj instanceof CharSequence)
return ((CharSequence) obj).length() == 0;

View file

@ -24,6 +24,7 @@
package zutil.net.mqtt;
import zutil.ObjectUtil;
import zutil.log.LogUtil;
import zutil.net.mqtt.packet.*;
import zutil.net.mqtt.packet.MqttPacketSubscribe.MqttSubscribePayload;
@ -187,7 +188,7 @@ public class MqttBroker extends ThreadedTCPNetworkServer {
* @param data the data that should be published.
*/
public void publish(String topicName, byte[] data) {
logger.finer("Data has been published to topic: " + topicName);
logger.finest("Data has been published to topic: " + topicName);
if (globalListeners != null)
globalListeners.forEach(listener -> listener.dataPublished(topicName, data));
@ -209,7 +210,13 @@ public class MqttBroker extends ThreadedTCPNetworkServer {
MqttTopic topicData = createTopic(topicName);
if (topicData != null) {
topicData.retainedPayload = data;
if (ObjectUtil.isEmpty(data)) {
logger.finer("Retained message removed for topic: " + topicName);
topicData.retainedPayload = null;
} else {
logger.finer("New message retained for topic: " + topicName);
topicData.retainedPayload = data;
}
}
}

View file

@ -44,6 +44,7 @@ public class ObjectUtilTest {
assertTrue(ObjectUtil.isEmpty(new HashMap<>(), ""));
assertTrue(ObjectUtil.isEmpty(new Hashtable<>(), ""));
assertTrue(ObjectUtil.isEmpty((Object) new String[0]));
assertTrue(ObjectUtil.isEmpty(new int[0]));
assertFalse(ObjectUtil.isEmpty(" ", ""));
@ -52,6 +53,7 @@ public class ObjectUtilTest {
assertFalse(ObjectUtil.isEmpty("", new StringBuffer("a")));
assertFalse(ObjectUtil.isEmpty("", Arrays.asList(1, 2, 3)));
assertFalse(ObjectUtil.isEmpty((Object) new String[]{"a"}));
assertFalse(ObjectUtil.isEmpty(new int[2]));
}
@Test