2 * Copyright (c) 2010-2023 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 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);
76 for (RegistryChangeListener<Item> l : listeners) {
77 l.updated(put, element);
84 public @Nullable Item remove(String key) {
85 Item put = items.remove(key);
87 for (RegistryChangeListener<Item> l : listeners) {
95 public Item getItem(String name) throws ItemNotFoundException {
96 Item item = items.get(name);
98 throw new ItemNotFoundException(name);
104 public Item getItemByPattern(String name) throws ItemNotFoundException {
105 Item item = items.get(name);
107 throw new ItemNotFoundException(name);
113 public Collection<Item> getItems() {
114 return items.values();
118 public Collection<Item> getItemsOfType(String type) {
119 return items.values();
123 public Collection<Item> getItems(String pattern) {
124 return items.values();
128 public Collection<Item> getItemsByTag(String... tags) {
129 return items.values();
133 public Collection<Item> getItemsByTagAndType(String type, String... tags) {
134 return items.values();
137 @SuppressWarnings("unchecked")
139 public <T extends Item> Collection<T> getItemsByTag(Class<T> typeFilter, String... tags) {
140 return (Collection<T>) items.values();
144 public @Nullable Item remove(String itemName, boolean recursive) {
145 Item put = items.remove(itemName);
147 for (RegistryChangeListener<Item> l : listeners) {
155 public void addRegistryHook(RegistryHook<Item> hook) {
159 public void removeRegistryHook(RegistryHook<Item> hook) {