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.binding.homematic.internal.type;
15 import java.util.ArrayList;
16 import java.util.Collection;
17 import java.util.HashMap;
18 import java.util.List;
19 import java.util.Locale;
21 import java.util.concurrent.CopyOnWriteArrayList;
23 import org.openhab.binding.homematic.type.HomematicThingTypeExcluder;
24 import org.openhab.core.thing.ThingTypeUID;
25 import org.openhab.core.thing.binding.ThingTypeProvider;
26 import org.openhab.core.thing.type.ThingType;
27 import org.osgi.service.component.annotations.Component;
28 import org.osgi.service.component.annotations.Reference;
29 import org.osgi.service.component.annotations.ReferenceCardinality;
30 import org.osgi.service.component.annotations.ReferencePolicy;
33 * Provides all ThingTypes from all Homematic bridges. Getters will exclude
34 * ThingTypes which occur in any registered {@link HomematicThingTypeExcluder},
35 * which allows external injection of customized thing-types at runtime.
37 * @author Gerhard Riegler - Initial contribution
38 * @author Michael Reitler - Added HomematicThingTypeExcluder
40 @Component(service = { HomematicThingTypeProvider.class, ThingTypeProvider.class })
41 public class HomematicThingTypeProviderImpl implements HomematicThingTypeProvider {
42 private Map<ThingTypeUID, ThingType> thingTypesByUID = new HashMap<>();
43 protected List<HomematicThingTypeExcluder> homematicThingTypeExcluders = new CopyOnWriteArrayList<>();
45 @Reference(cardinality = ReferenceCardinality.MULTIPLE, policy = ReferencePolicy.DYNAMIC)
46 protected void addHomematicThingTypeExcluder(HomematicThingTypeExcluder homematicThingTypeExcluder) {
47 if (homematicThingTypeExcluders != null) {
48 homematicThingTypeExcluders.add(homematicThingTypeExcluder);
52 protected void removeHomematicThingTypeExcluder(HomematicThingTypeExcluder homematicThingTypeExcluder) {
53 if (homematicThingTypeExcluders != null) {
54 homematicThingTypeExcluders.remove(homematicThingTypeExcluder);
58 private Collection<ThingTypeUID> getExcludedThingTypes() {
59 Collection<ThingTypeUID> thingTypes = new ArrayList<>();
60 for (HomematicThingTypeExcluder excluder : homematicThingTypeExcluders) {
61 thingTypes.addAll(excluder.getExcludedThingTypes());
66 private boolean isThingTypeExcluded(ThingTypeUID thingType) {
67 // delegate to excluders
68 for (HomematicThingTypeExcluder excluder : homematicThingTypeExcluders) {
69 if (excluder.isThingTypeExcluded(thingType)) {
77 public Collection<ThingType> getThingTypes(Locale locale) {
78 Map<ThingTypeUID, ThingType> copy = new HashMap<>(thingTypesByUID);
79 copy.keySet().removeAll(getExcludedThingTypes());
84 public ThingType getThingType(ThingTypeUID thingTypeUID, Locale locale) {
85 return isThingTypeExcluded(thingTypeUID) ? null : thingTypesByUID.get(thingTypeUID);
89 public ThingType getInternalThingType(ThingTypeUID thingTypeUID) {
90 return thingTypesByUID.get(thingTypeUID);
94 public void addThingType(ThingType thingType) {
95 thingTypesByUID.put(thingType.getUID(), thingType);