Bug fix for array isempty checks
This commit is contained in:
parent
19a12dee44
commit
09cce76707
3 changed files with 13 additions and 3 deletions
|
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
package zutil;
|
package zutil;
|
||||||
|
|
||||||
|
import java.lang.reflect.Array;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
@ -59,7 +60,7 @@ public class ObjectUtil {
|
||||||
else if (obj instanceof List)
|
else if (obj instanceof List)
|
||||||
return ((List) obj).isEmpty();
|
return ((List) obj).isEmpty();
|
||||||
else if (obj.getClass().isArray())
|
else if (obj.getClass().isArray())
|
||||||
return ((Object[]) obj).length == 0;
|
return (Array.getLength(obj) == 0);
|
||||||
else if (obj instanceof CharSequence)
|
else if (obj instanceof CharSequence)
|
||||||
return ((CharSequence) obj).length() == 0;
|
return ((CharSequence) obj).length() == 0;
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,7 @@
|
||||||
|
|
||||||
package zutil.net.mqtt;
|
package zutil.net.mqtt;
|
||||||
|
|
||||||
|
import zutil.ObjectUtil;
|
||||||
import zutil.log.LogUtil;
|
import zutil.log.LogUtil;
|
||||||
import zutil.net.mqtt.packet.*;
|
import zutil.net.mqtt.packet.*;
|
||||||
import zutil.net.mqtt.packet.MqttPacketSubscribe.MqttSubscribePayload;
|
import zutil.net.mqtt.packet.MqttPacketSubscribe.MqttSubscribePayload;
|
||||||
|
|
@ -187,7 +188,7 @@ public class MqttBroker extends ThreadedTCPNetworkServer {
|
||||||
* @param data the data that should be published.
|
* @param data the data that should be published.
|
||||||
*/
|
*/
|
||||||
public void publish(String topicName, byte[] data) {
|
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)
|
if (globalListeners != null)
|
||||||
globalListeners.forEach(listener -> listener.dataPublished(topicName, data));
|
globalListeners.forEach(listener -> listener.dataPublished(topicName, data));
|
||||||
|
|
@ -209,7 +210,13 @@ public class MqttBroker extends ThreadedTCPNetworkServer {
|
||||||
MqttTopic topicData = createTopic(topicName);
|
MqttTopic topicData = createTopic(topicName);
|
||||||
|
|
||||||
if (topicData != null) {
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -44,6 +44,7 @@ public class ObjectUtilTest {
|
||||||
assertTrue(ObjectUtil.isEmpty(new HashMap<>(), ""));
|
assertTrue(ObjectUtil.isEmpty(new HashMap<>(), ""));
|
||||||
assertTrue(ObjectUtil.isEmpty(new Hashtable<>(), ""));
|
assertTrue(ObjectUtil.isEmpty(new Hashtable<>(), ""));
|
||||||
assertTrue(ObjectUtil.isEmpty((Object) new String[0]));
|
assertTrue(ObjectUtil.isEmpty((Object) new String[0]));
|
||||||
|
assertTrue(ObjectUtil.isEmpty(new int[0]));
|
||||||
|
|
||||||
|
|
||||||
assertFalse(ObjectUtil.isEmpty(" ", ""));
|
assertFalse(ObjectUtil.isEmpty(" ", ""));
|
||||||
|
|
@ -52,6 +53,7 @@ public class ObjectUtilTest {
|
||||||
assertFalse(ObjectUtil.isEmpty("", new StringBuffer("a")));
|
assertFalse(ObjectUtil.isEmpty("", new StringBuffer("a")));
|
||||||
assertFalse(ObjectUtil.isEmpty("", Arrays.asList(1, 2, 3)));
|
assertFalse(ObjectUtil.isEmpty("", Arrays.asList(1, 2, 3)));
|
||||||
assertFalse(ObjectUtil.isEmpty((Object) new String[]{"a"}));
|
assertFalse(ObjectUtil.isEmpty((Object) new String[]{"a"}));
|
||||||
|
assertFalse(ObjectUtil.isEmpty(new int[2]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue