Skip to content

Commit

Permalink
Fixes .getOrDefault() not available in Android 5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianromero committed Jul 29, 2018
1 parent 56f3061 commit ee423cd
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
19 changes: 11 additions & 8 deletions src/main/java/com/adr/helloiot/MainManagerPlatform.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application.Parameters;
import javafx.scene.layout.StackPane;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
Expand Down Expand Up @@ -59,11 +57,11 @@ public void construct(StackPane root, Parameters params) {
config.put("mqtt.websockets", new MiniVarBoolean(Boolean.parseBoolean(configprops.getProperty("mqtt.websockets", "false"))));
config.put("mqtt.protocol", new MiniVarString(SSLProtocol.valueOfDefault(configprops.getProperty("mqtt.protocol", "TLSv12")).getDisplayName()));
config.put("mqtt.keystore", new MiniVarString(configprops.getProperty("mqtt.keystore", "")));
config.put("mqtt.keystorepassword", new MiniVarString(namedParams.getOrDefault("mqtt.keystorepassword", "")));
config.put("mqtt.keystorepassword", new MiniVarString(getNamedParam(namedParams, "mqtt.keystorepassword", "")));
config.put("mqtt.truststore", new MiniVarString(configprops.getProperty("mqtt.truststore", "")));
config.put("mqtt.truststorepassword", new MiniVarString(namedParams.getOrDefault("mqtt.truststorepassword", "")));
config.put("mqtt.username", new MiniVarString(namedParams.getOrDefault("mqtt.username", "")));
config.put("mqtt.password", new MiniVarString(namedParams.getOrDefault("mqtt.password", "")));
config.put("mqtt.truststorepassword", new MiniVarString(getNamedParam(namedParams, "mqtt.truststorepassword", "")));
config.put("mqtt.username", new MiniVarString(getNamedParam(namedParams, "mqtt.username", "")));
config.put("mqtt.password", new MiniVarString(getNamedParam(namedParams, "mqtt.password", "")));
config.put("mqtt.clientid", new MiniVarString(configprops.getProperty("mqtt.clientid", CryptUtils.generateID())));
config.put("mqtt.connectiontimeout", new MiniVarInt(Integer.parseInt(configprops.getProperty("mqtt.connectiontimeout", Integer.toString(MqttConnectOptions.CONNECTION_TIMEOUT_DEFAULT)))));
config.put("mqtt.keepaliveinterval", new MiniVarInt(Integer.parseInt(configprops.getProperty("mqtt.keepaliveinterval", Integer.toString(MqttConnectOptions.KEEP_ALIVE_INTERVAL_DEFAULT)))));
Expand All @@ -72,8 +70,8 @@ public void construct(StackPane root, Parameters params) {
config.put("client.broker", new MiniVarString(configprops.getProperty("client.broker", "0")));

config.put("tradfri.host", new MiniVarString(configprops.getProperty("tradfri.host", "")));
config.put("tradfri.identity", new MiniVarString(namedParams.getOrDefault("tradfri.identity", "")));
config.put("tradfri.psk", new MiniVarString(namedParams.getOrDefault("tradfri.psk", "")));
config.put("tradfri.identity", new MiniVarString(getNamedParam(namedParams, "tradfri.identity", "")));
config.put("tradfri.psk", new MiniVarString(getNamedParam(namedParams, "tradfri.psk", "")));

config.put("client.topicapp", new MiniVarString(configprops.getProperty("client.topicapp", "_LOCAL_/mainapp")));
config.put("client.topicsys", new MiniVarString(configprops.getProperty("client.topicsys", "system")));
Expand Down Expand Up @@ -114,4 +112,9 @@ public void destroy() {
helloiotapp.stopAndDestroy();
helloiotapp = null;
}

private final String getNamedParam(Map<String, String> namedparams, String key, String defaultValue) {
String value;
return ((value = namedparams.get(key)) != null) ? value : defaultValue;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public void destroy() {
}

public MiniVar readMessage(String branch) {
return statusmap.getOrDefault(getTopic() + "/" + branch, getFormat().value(null));
MiniVar value;
return ((value = statusmap.get(getTopic() + "/" + branch)) != null) ? value : getFormat().value(null);
}

public String loadMessage(String branch) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/adr/helloiot/mqtt/ManagerMQTT.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ public void registerTopicsManager(GroupManagers group, Consumer<Throwable> lost)
public void registerSubscription(String topic, Map<String, MiniVar> messageProperties) {
worktopics.add(topic);

MiniVar varqos = messageProperties.getOrDefault("mqtt.qos", MiniVarInt.NULL);
workqos.add(varqos.asInt());
MiniVar value = messageProperties.get("mqtt.qos");
workqos.add(value == null ? 0 : value.asInt());
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/adr/helloiotlib/app/EventMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public byte[] getMessage() {
}

public MiniVar getProperty(String key) {
return properties.getOrDefault(key, MiniVarInt.NULL);
MiniVar value;
return ((value = properties.get(key)) != null) ? value : MiniVarInt.NULL;
}
}

0 comments on commit ee423cd

Please sign in to comment.