class MetadataStateDescriptionFragmentProvider implements StateDescriptionFragmentProvider {

    @Override
    public @Nullable StateDescriptionFragment getStateDescriptionFragment(String itemName, @Nullable Locale locale) {
        Metadata metadata = metadataRegistry.get(new MetadataKey(STATEDESCRIPTION_METADATA_NAMESPACE, itemName));

        if (metadata != null) {
            try {
                StateDescriptionFragmentBuilder builder = StateDescriptionFragmentBuilder.create();
                if (metadata.getConfiguration().containsKey("pattern")) {
                    builder.withPattern((String) metadata.getConfiguration().get("pattern"));
                }
                if (metadata.getConfiguration().containsKey("min")) {
                    builder.withMinimum(getBigDecimal(metadata.getConfiguration().get("min")));
                }
                if (metadata.getConfiguration().containsKey("max")) {
                    builder.withMaximum(getBigDecimal(metadata.getConfiguration().get("max")));
                }
                if (metadata.getConfiguration().containsKey("step")) {
                    builder.withStep(getBigDecimal(metadata.getConfiguration().get("step")));
                }
                if (metadata.getConfiguration().containsKey("readOnly")) {
                    builder.withReadOnly(getBoolean(metadata.getConfiguration().get("readOnly")));
                }
                if (metadata.getConfiguration().containsKey("options")) {
                    List<StateOption> stateOptions = Stream
                            .of(metadata.getConfiguration().get("options").toString().split(",")).map(o -> {
                                return (o.contains("="))
                                        ? new StateOption(o.split("=")[0].trim(), o.split("=")[1].trim())
                                        : new StateOption(o.trim(), null);
                            }).collect(Collectors.toList());
                    builder.withOptions(stateOptions);
                }

                return builder.build();
            } catch (Exception e) {
                logger.warn("Unable to parse the stateDescription from metadata for item {}, ignoring it", itemName);
            }
        }

        return null;
    }

    @Activate public  MetadataStateDescriptionFragmentProvider(final @Reference MetadataRegistry metadataRegistry,
            Map<String, Object> properties);

}