From 85770bf2f5eeb848220bcfbfdb56aac687f040e9 Mon Sep 17 00:00:00 2001 From: Simon Kaufmann Date: Mon, 9 Apr 2018 11:46:42 +0200 Subject: [PATCH] [hue] fix NPEs ...which were introduced by #5318. Signed-off-by: Simon Kaufmann --- .../binding/hue/handler/HueBridgeHandler.java | 38 ++++--------------- .../binding/hue/internal/ErrorResponse.java | 9 +++++ .../binding/hue/internal/HueBridge.java | 4 +- .../smarthome/binding/hue/internal/State.java | 6 +++ 4 files changed, 25 insertions(+), 32 deletions(-) diff --git a/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/handler/HueBridgeHandler.java b/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/handler/HueBridgeHandler.java index 12698606fe0..cae3fc1b2e7 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/handler/HueBridgeHandler.java +++ b/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/handler/HueBridgeHandler.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; @@ -493,42 +494,19 @@ private void notifyLightStatusListeners(final FullLight fullLight, final String } /** - * Because the State can produce NPEs on getColorMode() and getEffect(), at first we check for the common - * properties which are set for every light type. If they equal, we additionally try to check the colorMode. If we - * get an NPE, - * the light does not support color mode and the common properties equality is our result: true. Otherwise if no NPE - * occurs - * the equality of colorMode is our result. + * Compare to states for equality. * * @param state1 Reference state * @param state2 State which is checked for equality. - * @return True if the available informations of both states are equal. + * @return {@code true} if the available information of both states are equal. */ private boolean isEqual(State state1, State state2) { - boolean commonStateIsEqual = state1.getAlertMode().equals(state2.getAlertMode()) - && state1.isOn() == state2.isOn() && state1.getBrightness() == state2.getBrightness() + return state1.getAlertMode().equals(state2.getAlertMode()) && state1.isOn() == state2.isOn() + && state1.getBrightness() == state2.getBrightness() && state1.getColorTemperature() == state2.getColorTemperature() && state1.getHue() == state2.getHue() - && state1.getSaturation() == state2.getSaturation() && state1.isReachable() == state2.isReachable(); - if (!commonStateIsEqual) { - return false; - } - - boolean colorModeIsEqual = true; - boolean effectIsEqual = true; - - if (state1.getColorMode() == null) { - logger.trace("Light does not support color mode."); - } else { - colorModeIsEqual = state1.getColorMode().equals(state2.getColorMode()); - } - - if (state1.getEffect() == null) { - logger.trace("Light does not support effect."); - } else { - effectIsEqual = state1.getEffect().equals(state2.getEffect()); - } - - return colorModeIsEqual && effectIsEqual; + && state1.getSaturation() == state2.getSaturation() && state1.isReachable() == state2.isReachable() + && Objects.equals(state1.getColorMode(), state2.getColorMode()) + && Objects.equals(state1.getEffect(), state2.getEffect()); } @Override diff --git a/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/ErrorResponse.java b/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/ErrorResponse.java index 3ae90cfc7d2..54c2886dd21 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/ErrorResponse.java +++ b/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/ErrorResponse.java @@ -35,14 +35,23 @@ public class Error { private Error error; public Integer getType() { + if (error == null) { + return null; + } return error.type; } public String getAddress() { + if (error == null) { + return null; + } return error.address; } public String getDescription() { + if (error == null) { + return null; + } return error.description; } } diff --git a/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/HueBridge.java b/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/HueBridge.java index 2d4595e3b84..b41dc2233a4 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/HueBridge.java +++ b/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/HueBridge.java @@ -873,10 +873,10 @@ private void handleErrors(Result result) throws IOException, ApiException { } for (ErrorResponse error : errors) { - if (error == null) { + if (error.getType() == null) { continue; } - + switch (error.getType()) { case 1: username = null; diff --git a/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/State.java b/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/State.java index 2cb25d2861f..72f49299777 100644 --- a/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/State.java +++ b/extensions/binding/org.eclipse.smarthome.binding.hue/src/main/java/org/eclipse/smarthome/binding/hue/internal/State.java @@ -158,6 +158,9 @@ public AlertMode getAlertMode() { * @return current color mode */ public ColorMode getColorMode() { + if (colormode == null) { + return null; + } return ColorMode.valueOf(colormode.toUpperCase()); } @@ -167,6 +170,9 @@ public ColorMode getColorMode() { * @return current active effect */ public Effect getEffect() { + if (effect == null) { + return null; + } return Effect.valueOf(effect.toUpperCase()); }