<commit_before>/**
 * Copyright (c) 2015-2016 by the respective copyright holders.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.core.items.internal;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.smarthome.core.common.registry.RegistryChangeListener;
import org.openhab.core.compat1x.internal.ItemMapper;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemNotUniqueException;
import org.openhab.core.items.ItemRegistryChangeListener;
import org.openhab.core.types.State;
import org.openhab.model.sitemap.LinkableWidget;
import org.openhab.model.sitemap.Sitemap;
import org.openhab.model.sitemap.Widget;
import org.openhab.ui.items.ItemUIRegistry;

public class ItemUIRegistryDelegate
        implements ItemUIRegistry, RegistryChangeListener<org.eclipse.smarthome.core.items.Item> {

    private org.eclipse.smarthome.ui.items.ItemUIRegistry itemUIRegistry;
    private Set<ItemRegistryChangeListener> listeners = new HashSet<>();

    protected void setItemUIRegistry(org.eclipse.smarthome.ui.items.ItemUIRegistry itemUIRegistry) {
        this.itemUIRegistry = itemUIRegistry;
        itemUIRegistry.addRegistryChangeListener(this);
    }

    protected void unsetItemUIRegistry(org.eclipse.smarthome.core.items.ItemRegistry itemUIRegistry) {
        this.itemUIRegistry = null;
    }

    @Override
    public Item getItem(String name) throws ItemNotFoundException {
        org.eclipse.smarthome.core.items.Item eshItem;
        try {
            eshItem = itemUIRegistry.getItem(name);
        } catch (org.eclipse.smarthome.core.items.ItemNotFoundException e) {
            throw new ItemNotFoundException(name);
        }
        return ItemMapper.mapToOpenHABItem(eshItem);
    }

    @Override
    public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
        org.eclipse.smarthome.core.items.Item eshItem;
        try {
            eshItem = itemUIRegistry.getItemByPattern(name);
        } catch (org.eclipse.smarthome.core.items.ItemNotFoundException e) {
            throw new ItemNotFoundException(name);
        } catch (org.eclipse.smarthome.core.items.ItemNotUniqueException e) {
            throw new ItemNotUniqueException(name, null);
        }
        return ItemMapper.mapToOpenHABItem(eshItem);
    }

    @Override
    public Collection<Item> getItems() {
        Collection<org.eclipse.smarthome.core.items.Item> eshItems = itemUIRegistry.getItems();
        Collection<Item> ohItems = new HashSet<Item>(eshItems.size());

        for (org.eclipse.smarthome.core.items.Item eshItem : eshItems) {
            ohItems.add(ItemMapper.mapToOpenHABItem(eshItem));
        }
        return ohItems;
    }

    @Override
    public Collection<Item> getItems(String pattern) {
        Collection<org.eclipse.smarthome.core.items.Item> eshItems = itemUIRegistry.getItems(pattern);
        Collection<Item> ohItems = new HashSet<Item>(eshItems.size());

        for (org.eclipse.smarthome.core.items.Item eshItem : eshItems) {
            ohItems.add(ItemMapper.mapToOpenHABItem(eshItem));
        }
        return ohItems;
    }

    @Override
    public boolean isValidItemName(String itemName) {
        return itemUIRegistry.isValidItemName(itemName);
    }

    @Override
    public void addItemRegistryChangeListener(ItemRegistryChangeListener listener) {
        this.listeners.add(listener);
    }

    @Override
    public void removeItemRegistryChangeListener(ItemRegistryChangeListener listener) {
        this.listeners.remove(listener);
    }

    @Override
    public void added(org.eclipse.smarthome.core.items.Item element) {
        Item ohItem = ItemMapper.mapToOpenHABItem(element);
        for (ItemRegistryChangeListener listener : listeners) {
            listener.itemAdded(ohItem);
        }
    }<commit_msg>adapt to ESH changes of ItemRegistry (#47)<commit_after>/**
 * Copyright (c) 2015-2016 by the respective copyright holders.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package org.openhab.core.items.internal;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.eclipse.emf.common.util.EList;
import org.eclipse.smarthome.core.common.registry.RegistryChangeListener;
import org.eclipse.smarthome.core.items.ItemUtil;
import org.openhab.core.compat1x.internal.ItemMapper;
import org.openhab.core.items.Item;
import org.openhab.core.items.ItemNotFoundException;
import org.openhab.core.items.ItemNotUniqueException;
import org.openhab.core.items.ItemRegistryChangeListener;
import org.openhab.core.types.State;
import org.openhab.model.sitemap.LinkableWidget;
import org.openhab.model.sitemap.Sitemap;
import org.openhab.model.sitemap.Widget;
import org.openhab.ui.items.ItemUIRegistry;

public class ItemUIRegistryDelegate
        implements ItemUIRegistry, RegistryChangeListener<org.eclipse.smarthome.core.items.Item> {

    private org.eclipse.smarthome.ui.items.ItemUIRegistry itemUIRegistry;
    private Set<ItemRegistryChangeListener> listeners = new HashSet<>();

    protected void setItemUIRegistry(org.eclipse.smarthome.ui.items.ItemUIRegistry itemUIRegistry) {
        this.itemUIRegistry = itemUIRegistry;
        itemUIRegistry.addRegistryChangeListener(this);
    }

    protected void unsetItemUIRegistry(org.eclipse.smarthome.core.items.ItemRegistry itemUIRegistry) {
        this.itemUIRegistry = null;
    }

    @Override
    public Item getItem(String name) throws ItemNotFoundException {
        org.eclipse.smarthome.core.items.Item eshItem;
        try {
            eshItem = itemUIRegistry.getItem(name);
        } catch (org.eclipse.smarthome.core.items.ItemNotFoundException e) {
            throw new ItemNotFoundException(name);
        }
        return ItemMapper.mapToOpenHABItem(eshItem);
    }

    @Override
    public Item getItemByPattern(String name) throws ItemNotFoundException, ItemNotUniqueException {
        org.eclipse.smarthome.core.items.Item eshItem;
        try {
            eshItem = itemUIRegistry.getItemByPattern(name);
        } catch (org.eclipse.smarthome.core.items.ItemNotFoundException e) {
            throw new ItemNotFoundException(name);
        } catch (org.eclipse.smarthome.core.items.ItemNotUniqueException e) {
            throw new ItemNotUniqueException(name, null);
        }
        return ItemMapper.mapToOpenHABItem(eshItem);
    }

    @Override
    public Collection<Item> getItems() {
        Collection<org.eclipse.smarthome.core.items.Item> eshItems = itemUIRegistry.getItems();
        Collection<Item> ohItems = new HashSet<Item>(eshItems.size());

        for (org.eclipse.smarthome.core.items.Item eshItem : eshItems) {
            ohItems.add(ItemMapper.mapToOpenHABItem(eshItem));
        }
        return ohItems;
    }

    @Override
    public Collection<Item> getItems(String pattern) {
        Collection<org.eclipse.smarthome.core.items.Item> eshItems = itemUIRegistry.getItems(pattern);
        Collection<Item> ohItems = new HashSet<Item>(eshItems.size());

        for (org.eclipse.smarthome.core.items.Item eshItem : eshItems) {
            ohItems.add(ItemMapper.mapToOpenHABItem(eshItem));
        }
        return ohItems;
    }

    @Override
    public boolean isValidItemName(String itemName) {
        return ItemUtil.isValidItemName(itemName);
    }

    @Override
    public void addItemRegistryChangeListener(ItemRegistryChangeListener listener) {
        this.listeners.add(listener);
    }

    @Override
    public void removeItemRegistryChangeListener(ItemRegistryChangeListener listener) {
        this.listeners.remove(listener);
    }

    @Override
    public void added(org.eclipse.smarthome.core.items.Item element) {
        Item ohItem = ItemMapper.mapToOpenHABItem(element);
        for (ItemRegistryChangeListener listener : listeners) {
            listener.itemAdded(ohItem);
        }
    }
{'fraction_non_alphanumeric': 0.0934723374007819, 'fraction_numerical': 0.0033171425186589265, 'mean_word_length': 3.5241157556270095, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '23614', 'n_tokens_mistral': 2446, 'n_tokens_neox': 2470, 'n_words': 438}