/** {@inheritDoc} */ @Override public void receiveUpdate(String itemName, State newStatus) { if (itemRegistry != null) { try { GenericItem item = (GenericItem) itemRegistry.getItem(itemName); boolean isAccepted = false; if (item.getAcceptedDataTypes().contains(newStatus.getClass())) { isAccepted = true; } else { // Look for class hierarchy for (Class<? extends State> state : item.getAcceptedDataTypes()) { try { if (!state.isEnum() && state.newInstance().getClass().isAssignableFrom(newStatus.getClass())) { isAccepted = true; break; } } catch (InstantiationException e) { logger.warn("InstantiationException on {}", e.getMessage()); // Should never happen } catch (IllegalAccessException e) { logger.warn("IllegalAccessException on {}", e.getMessage()); // Should never happen } } } if (isAccepted) { item.setState(newStatus); } else { logger.debug( "Received update of a not accepted type (" + newStatus.getClass().getSimpleName() + ") for item " + itemName); } } catch (ItemNotFoundException e) { logger.debug("Received update for non-existing item: {}", e.getMessage()); } } }
private GroupItem applyGroupFunction( GenericItem baseItem, ModelGroupItem modelGroupItem, ModelGroupFunction function) { List<State> args = new ArrayList<State>(); for (String arg : modelGroupItem.getArgs()) { State state = TypeParser.parseState(baseItem.getAcceptedDataTypes(), arg); if (state == null) { logger.warn( "State '{}' is not valid for group item '{}' with base type '{}'", new Object[] {arg, modelGroupItem.getName(), modelGroupItem.getType()}); args.clear(); break; } else { args.add(state); } } GroupFunction groupFunction = null; switch (function) { case AND: if (args.size() == 2) { groupFunction = new ArithmeticGroupFunction.And(args.get(0), args.get(1)); break; } else { logger.error("Group function 'AND' requires two arguments. Using Equality instead."); } case OR: if (args.size() == 2) { groupFunction = new ArithmeticGroupFunction.Or(args.get(0), args.get(1)); break; } else { logger.error("Group function 'OR' requires two arguments. Using Equality instead."); } case NAND: if (args.size() == 2) { groupFunction = new ArithmeticGroupFunction.NAnd(args.get(0), args.get(1)); break; } else { logger.error("Group function 'NOT AND' requires two arguments. Using Equality instead."); } break; case NOR: if (args.size() == 2) { groupFunction = new ArithmeticGroupFunction.NOr(args.get(0), args.get(1)); break; } else { logger.error("Group function 'NOT OR' requires two arguments. Using Equality instead."); } case COUNT: if (args.size() == 1) { groupFunction = new ArithmeticGroupFunction.Count(args.get(0)); break; } else { logger.error("Group function 'COUNT' requires one argument. Using Equality instead."); } case AVG: groupFunction = new ArithmeticGroupFunction.Avg(); break; case SUM: groupFunction = new ArithmeticGroupFunction.Sum(); break; case MIN: groupFunction = new ArithmeticGroupFunction.Min(); break; case MAX: groupFunction = new ArithmeticGroupFunction.Max(); break; default: logger.error( "Unknown group function '" + function.getName() + "'. Using Equality instead."); } if (groupFunction == null) { groupFunction = new GroupFunction.Equality(); } return new GroupItem(modelGroupItem.getName(), baseItem, groupFunction); }