Skip to content

Commit

Permalink
[hue] fix NPEs
Browse files Browse the repository at this point in the history
...which were introduced by eclipse-archived#5318.

Signed-off-by: Simon Kaufmann <[email protected]>
  • Loading branch information
Simon Kaufmann committed Apr 9, 2018
1 parent 13e77d3 commit 85770bf
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ public AlertMode getAlertMode() {
* @return current color mode
*/
public ColorMode getColorMode() {
if (colormode == null) {
return null;
}
return ColorMode.valueOf(colormode.toUpperCase());
}

Expand All @@ -167,6 +170,9 @@ public ColorMode getColorMode() {
* @return current active effect
*/
public Effect getEffect() {
if (effect == null) {
return null;
}
return Effect.valueOf(effect.toUpperCase());
}

Expand Down

0 comments on commit 85770bf

Please sign in to comment.