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.unifi.internal.api.dto;
15 import java.util.HashMap;
16 import java.util.HashSet;
17 import java.util.List;
19 import java.util.Objects;
21 import java.util.function.Consumer;
22 import java.util.stream.Collectors;
23 import java.util.stream.Stream;
25 import org.eclipse.jdt.annotation.NonNullByDefault;
26 import org.eclipse.jdt.annotation.Nullable;
28 import com.google.gson.JsonObject;
31 * Data object to keep track of all port data, including all port_override data (both for ports and additional data) on
34 * @author Hilbrand Bouwkamp - Initial contribution
37 public class UniFiSwitchPorts {
40 * Port data grouped by port id.
42 private final Map<Integer, UniFiPortTuple> ports = new HashMap<>();
44 * Additional none port specific override data. Keep track to send to device when updating override data.
46 private final Set<JsonObject> otherOverrides = new HashSet<>();
49 * Return port data for the given port
51 * @param portIdx port to get the data for
52 * @return Return port data for the given port
54 public @Nullable UniFiPortTuple getPort(final int portIdx) {
55 return ports.get(portIdx);
59 * Return port data for the given port or if none exists set a new data object and return it.
61 * @param portIdx port to get the data for
62 * @return Return port data for the given port or if none exists set a new data object and return it.
64 public UniFiPortTuple computeIfAbsent(final int portIdx) {
65 final UniFiPortTuple tuple = ports.computeIfAbsent(portIdx, t -> new UniFiPortTuple());
67 // This should never happen because ports can never contain a null value, and computeIfAbsent should never
68 // return null. However to satisfy the compiler a check for null was added.
69 throw new IllegalStateException("UniFiPortTuple is null for portIdx " + portIdx);
75 * @return Returns the list of PoE Ports.
77 public List<UniFiPortTuple> getPoePorts() {
78 return ports.values().stream().filter(e -> e.getTable().isPortPoe()).collect(Collectors.toList());
82 * Returns the override data as list with JSON objects after calling the updateMethod on the data for the given
84 * The update method changes the data in the internal structure.
86 * @param portIdx port to call updateMethod for
87 * @param updateMethod method to call to update data for a specific port
88 * @return Returns a list of JSON objects of all override data
90 public List<JsonObject> updatedList(final int portIdx, final Consumer<UnfiPortOverrideJsonObject> updateMethod) {
91 @SuppressWarnings("null")
92 final List<UnfiPortOverrideJsonObject> updatedList = ports.entrySet().stream()
93 .map(e -> e.getValue().getJsonElement()).filter(Objects::nonNull).collect(Collectors.toList());
95 updatedList.stream().filter(p -> p.getPortIdx() == portIdx).findAny().ifPresent(updateMethod::accept);
98 .concat(otherOverrides.stream(), updatedList.stream().map(UnfiPortOverrideJsonObject::getJsonObject))
99 .collect(Collectors.toList());
103 * Set the port override object. If it's for a specific port set bind it to the port data, otherwise store it as
106 * @param jsonObject JSON object to set
108 public void setOverride(final JsonObject jsonObject) {
109 if (UnfiPortOverrideJsonObject.hasPortIdx(jsonObject)) {
110 final UnfiPortOverrideJsonObject po = new UnfiPortOverrideJsonObject(jsonObject);
111 final UniFiPortTuple tuple = ports.get(po.getPortIdx());
114 otherOverrides.add(jsonObject);
116 tuple.setJsonElement(po);
119 otherOverrides.add(jsonObject);