Change null check to empty lists
This commit is contained in:
@@ -28,6 +28,8 @@ import org.eclipse.moquette.spi.ISessionsStore;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import static org.eclipse.moquette.spi.impl.Utils.defaultGet;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class MemoryStorageService implements IMessagesStore, ISessionsStore {
|
||||
@@ -81,20 +83,21 @@ public class MemoryStorageService implements IMessagesStore, ISessionsStore {
|
||||
@Override
|
||||
public void storePublishForFuture(PublishEvent evt) {
|
||||
LOG.debug("storePublishForFuture store evt {}", evt);
|
||||
List<PublishEvent> storedEvents;
|
||||
// List<PublishEvent> storedEvents;
|
||||
String clientID = evt.getClientID();
|
||||
if (!m_persistentMessageStore.containsKey(clientID)) {
|
||||
storedEvents = new ArrayList<PublishEvent>();
|
||||
} else {
|
||||
storedEvents = m_persistentMessageStore.get(clientID);
|
||||
}
|
||||
// if (!m_persistentMessageStore.containsKey(clientID)) {
|
||||
// storedEvents = new ArrayList<PublishEvent>();
|
||||
// } else {
|
||||
// storedEvents = m_persistentMessageStore.get(clientID);
|
||||
// }
|
||||
List<PublishEvent> storedEvents = defaultGet(m_persistentMessageStore, clientID, new ArrayList<PublishEvent>());
|
||||
storedEvents.add(evt);
|
||||
m_persistentMessageStore.put(clientID, storedEvents);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<PublishEvent> listMessagesInSession(String clientID) {
|
||||
return new ArrayList<>(m_persistentMessageStore.get(clientID));
|
||||
return new ArrayList<>(defaultGet(m_persistentMessageStore, clientID, Collections.<PublishEvent>emptyList()));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -242,7 +242,7 @@ class ProtocolProcessor implements EventHandler<ValueEvent> {
|
||||
private void republishStoredInSession(String clientID) {
|
||||
LOG.trace("republishStoredInSession invoked");
|
||||
List<PublishEvent> publishedEvents = m_messagesStore.listMessagesInSession(clientID);
|
||||
if (publishedEvents == null) {
|
||||
if (publishedEvents.isEmpty()) {
|
||||
LOG.info("No stored messages for client <{}>", clientID);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
package org.eclipse.moquette.spi.impl;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Utility static methods, like Map get with default value, or elvis operator.
|
||||
*/
|
||||
public class Utils {
|
||||
public static <T, K> T defaultGet(Map<K, T> map, K key, T defaultValue) {
|
||||
T value = map.get(key);
|
||||
if (value != null) {
|
||||
return value;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,8 @@ import org.eclipse.moquette.spi.impl.storage.StoredPublishEvent;
|
||||
import org.eclipse.moquette.spi.impl.subscriptions.Subscription;
|
||||
import org.eclipse.moquette.proto.messages.AbstractMessage;
|
||||
import static org.eclipse.moquette.server.Server.STORAGE_FILE_PATH;
|
||||
import static org.eclipse.moquette.spi.impl.Utils.defaultGet;
|
||||
|
||||
import org.mapdb.DB;
|
||||
import org.mapdb.DBMaker;
|
||||
import org.slf4j.Logger;
|
||||
@@ -126,11 +128,9 @@ public class MapDBPersistentStore implements IMessagesStore, ISessionsStore {
|
||||
|
||||
@Override
|
||||
public List<PublishEvent> listMessagesInSession(String clientID) {
|
||||
List<StoredPublishEvent> storedEvts = m_persistentMessageStore.get(clientID);
|
||||
if (storedEvts == null) {
|
||||
return null;
|
||||
}
|
||||
List<PublishEvent> liveEvts = new ArrayList<PublishEvent>();
|
||||
List<PublishEvent> liveEvts = new ArrayList<>();
|
||||
List<StoredPublishEvent> storedEvts = defaultGet(m_persistentMessageStore, clientID, Collections.<StoredPublishEvent>emptyList());
|
||||
|
||||
for (StoredPublishEvent storedEvt : storedEvts) {
|
||||
liveEvts.add(convertFromStored(storedEvt));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user