2 * Copyright (c) 2010-2020 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.io.hueemulation.internal.rest.mocks;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.List;
19 import java.util.TreeMap;
20 import java.util.stream.Stream;
22 import org.eclipse.jdt.annotation.NonNullByDefault;
23 import org.eclipse.jdt.annotation.Nullable;
24 import org.openhab.core.common.registry.RegistryChangeListener;
25 import org.openhab.core.items.Item;
26 import org.openhab.core.items.ItemNotFoundException;
27 import org.openhab.core.items.ItemRegistry;
28 import org.openhab.core.items.RegistryHook;
31 * @author David Graeff - Initial contribution
34 public class DummyItemRegistry implements ItemRegistry {
35 Map<String, Item> items = new TreeMap<>();
36 List<RegistryChangeListener<Item>> listeners = new ArrayList<>();
39 public void addRegistryChangeListener(RegistryChangeListener<Item> listener) {
40 listeners.add(listener);
44 public Collection<Item> getAll() {
45 return items.values();
49 public Stream<Item> stream() {
50 return items.values().stream();
54 public @Nullable Item get(String key) {
55 return items.get(key);
59 public void removeRegistryChangeListener(RegistryChangeListener<Item> listener) {
60 listeners.remove(listener);
64 public Item add(Item element) {
65 Item put = items.put(element.getUID(), element);
66 for (RegistryChangeListener<Item> l : listeners) {
73 public @Nullable Item update(Item element) {
74 Item put = items.put(element.getUID(), element);
75 for (RegistryChangeListener<Item> l : listeners) {
76 l.updated(put, element);
82 public @Nullable Item remove(String key) {
83 Item put = items.remove(key);
84 for (RegistryChangeListener<Item> l : listeners) {
91 public Item getItem(String name) throws ItemNotFoundException {
92 Item item = items.get(name);
94 throw new ItemNotFoundException(name);
100 public Item getItemByPattern(String name) throws ItemNotFoundException {
101 Item item = items.get(name);
103 throw new ItemNotFoundException(name);
109 public Collection<Item> getItems() {
110 return items.values();
114 public Collection<Item> getItemsOfType(String type) {
115 return items.values();
119 public Collection<Item> getItems(String pattern) {
120 return items.values();
124 public Collection<Item> getItemsByTag(String... tags) {
125 return items.values();
129 public Collection<Item> getItemsByTagAndType(String type, String... tags) {
130 return items.values();
133 @SuppressWarnings("unchecked")
135 public <T extends Item> Collection<T> getItemsByTag(Class<T> typeFilter, String... tags) {
136 return (Collection<T>) items.values();
140 public @Nullable Item remove(String itemName, boolean recursive) {
141 Item put = items.remove(itemName);
142 for (RegistryChangeListener<Item> l : listeners) {
149 public void addRegistryHook(RegistryHook<Item> hook) {
153 public void removeRegistryHook(RegistryHook<Item> hook) {