]> git.basschouten.com Git - openhab-addons.git/blob
e24211b2f01c3a56f70bb9c0269e386b46f5dab6
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.asuswrt.internal.structures;
14
15 import java.util.ArrayList;
16 import java.util.Iterator;
17 import java.util.List;
18
19 import org.eclipse.jdt.annotation.NonNullByDefault;
20
21 import com.google.gson.JsonObject;
22
23 /**
24  * The {@link AsuswrtInterfaceList} class stores a list of {@link AsuswrtIpInfo}.
25  *
26  * @author Christian Wild - Initial contribution
27  */
28 @NonNullByDefault
29 public class AsuswrtInterfaceList implements Iterable<AsuswrtIpInfo> {
30     private List<AsuswrtIpInfo> ipInfoList = new ArrayList<>();
31
32     public AsuswrtInterfaceList() {
33     }
34
35     @Override
36     public Iterator<AsuswrtIpInfo> iterator() {
37         return ipInfoList.iterator();
38     }
39
40     /*
41      * Setters
42      */
43
44     /**
45      * Adds an {@link AsuswrtIpInfo} to the list.
46      */
47     private void addInterface(AsuswrtIpInfo ipInfo) {
48         ipInfoList.add(ipInfo);
49     }
50
51     /**
52      * Sets the {@link AsuswrtInterfaceList} using a {@link JsonObject}.
53      */
54     public void setData(String ifName, JsonObject jsonObject) {
55         if (hasInterface(ifName)) {
56             getByName(ifName).setData(jsonObject);
57         } else {
58             addInterface(new AsuswrtIpInfo(ifName, jsonObject));
59         }
60     }
61
62     /*
63      * Getters
64      */
65
66     /**
67      * Gets {@link AsuswrtIpInfo} from the list for an interface based on its name.
68      *
69      * @param ifName the name of the interface for which the info is returned
70      */
71     public AsuswrtIpInfo getByName(String ifName) {
72         for (AsuswrtIpInfo ipInfo : ipInfoList) {
73             if (ipInfo.getName().equals(ifName)) {
74                 return ipInfo;
75             }
76         }
77         return new AsuswrtIpInfo();
78     }
79
80     /**
81      * Gets {@link AsuswrtIpInfo} from the list for an interface based on its MAC address.
82      *
83      * @param ipInfoMAC the MAC address of the interface for which the info is returned
84      */
85     public AsuswrtIpInfo getByMAC(String ipInfoMAC) {
86         for (AsuswrtIpInfo ipInfo : ipInfoList) {
87             if (ipInfo.getMAC().equals(ipInfoMAC)) {
88                 return ipInfo;
89             }
90         }
91         return new AsuswrtIpInfo();
92     }
93
94     /**
95      * Gets {@link AsuswrtIpInfo} from the list for an interface based on its IP address.
96      *
97      * @param ipAddress the IP address of the interface for which the info is returned
98      */
99     public AsuswrtIpInfo getByIP(String ipAddress) {
100         for (AsuswrtIpInfo ipInfo : ipInfoList) {
101             if (ipInfo.getIpAddress().equals(ipAddress)) {
102                 return ipInfo;
103             }
104         }
105         return new AsuswrtIpInfo();
106     }
107
108     /**
109      * Checks if an interface with the given name is in the list.
110      *
111      * @param ifName the name of the interface
112      */
113     public boolean hasInterface(String ifName) {
114         for (AsuswrtIpInfo ipInfo : ipInfoList) {
115             if (ipInfo.getName().equals(ifName)) {
116                 return true;
117             }
118         }
119         return false;
120     }
121 }