AbstractResourceIconProvider implements IconProvider { @Override public @Nullable InputStream getIcon(String category, String iconSetId, @Nullable String state, Format format) { String resourceWithoutState = category.toLowerCase() + "." + format.toString().toLowerCase(); if (state == null) { return getResource(iconSetId, resourceWithoutState); } String iconState; if (state.contains(" ")) { try { String firstPart = state.substring(0, state.indexOf(" ")); Double.valueOf(firstPart); iconState = firstPart; } catch (NumberFormatException e) { iconState = state; } } else { iconState = state; } String resourceWithState = category.toLowerCase() + "-" + iconState.toLowerCase() + "." + format.toString().toLowerCase(); if (hasResource(iconSetId, resourceWithState)) { return getResource(iconSetId, resourceWithState); } else { try { Double stateAsDouble = Double.valueOf(iconState); if (stateAsDouble >= 0 && stateAsDouble <= 100) { for (int i = stateAsDouble.intValue(); i >= 0; i--) { String resourceWithNumberState = category.toLowerCase() + "-" + i + "." + format.toString().toLowerCase(); if (hasResource(iconSetId, resourceWithNumberState)) { return getResource(iconSetId, resourceWithNumberState); } } } } catch (NumberFormatException e) { } logger.debug("Use icon {} as {} is not found", resourceWithoutState, resourceWithState); return getResource(iconSetId, resourceWithoutState); } } AbstractResourceIconProvider(final TranslationProvider i18nProvider); @Override Set<IconSet> getIconSets(); @Override @Nullable Integer hasIcon(String category, String iconSetId, Format format); @Override @Nullable InputStream getIcon(String category, String iconSetId, @Nullable String state, Format format);  }
@Test public void testScanningForState() throws IOException { try (InputStream is = provider.getIcon("x", "classic", "34", Format.PNG)) { assertNotNull(is); } try (InputStream is = provider.getIcon("x", "classic", "25", Format.PNG)) { assertNull(is); } }
@Test public void testWithQuantityTypeState() throws IOException { try (InputStream is = provider.getIcon("x", "classic", "34 °C", Format.PNG)) { assertThat(new String(is.readAllBytes(), StandardCharsets.UTF_8), is("x-30.png")); } }
@Test public void testWithStringTypeState() throws IOException { try (InputStream is = provider.getIcon("x", "classic", "y z", Format.PNG)) { assertThat(new String(is.readAllBytes(), StandardCharsets.UTF_8), is("x-y z.png")); } }