001package jmri.jmrix.loconet.alm;
002
003import java.util.ArrayList;
004import java.util.Iterator;
005
006import org.slf4j.Logger;
007import org.slf4j.LoggerFactory;
008
009/**
010 *
011 * @author B. Milhaupt (C) 2024
012 */
013public class LnSimple7thGenDevicesRoutes {
014    private final ArrayList<LnSimple7thGenDeviceRoutes> devicesRoutes;
015
016    /**
017     * Constructor.
018     */
019    public LnSimple7thGenDevicesRoutes() {
020        devicesRoutes = new ArrayList<>();
021    }
022
023    /**
024     * Add a LnSimple7thGenDeviceRoutes object.
025     *
026     * When the device (by device type and serial number) is not yet in the
027     * devicesRoutes list, a new device is added.
028     *
029     * When a device (by device type and serial number) exists,
030     * that device gets updated.
031     *
032     * @param dr "DeviceRoutes" to be added
033     */
034    public void add(LnSimple7thGenDeviceRoutes dr) {
035        LnSimple7thGenDeviceRoutes deviceRoutes;
036
037        if (isDeviceRoutes(dr)) {
038            deviceRoutes = getDeviceRoutes(dr);
039        } else {
040            deviceRoutes = new LnSimple7thGenDeviceRoutes(dr.getDeviceType(),
041                    dr.getSerNum());
042        }
043
044        // move all dr's routes to the new deviceRoutes object
045        deviceRoutes.setRoutes(dr.getRoutes());
046
047        // add the deviceRoutes object as a devicesRoutes.
048        devicesRoutes.add(deviceRoutes);
049    }
050
051    /**
052     * Report if device is in the list.
053     *
054     * Checks device type and serial number.
055     *
056     * @param dr LnSimple7thGenDeviceRoutes device
057     * @return true if the device is in the list, else false
058     */
059    public boolean isDeviceRoutes(LnSimple7thGenDeviceRoutes dr) {
060        for (Iterator<LnSimple7thGenDeviceRoutes> it = devicesRoutes.iterator(); it.hasNext();) {
061            LnSimple7thGenDeviceRoutes lndr = it.next();
062            if ((lndr.getDeviceType() == dr.getDeviceType()) &&
063                    (lndr.getSerNum() == dr.getSerNum())) {
064                return true;
065            }
066        }
067        return false;
068    }
069
070    /**
071     * Report if device is in the list.
072     *
073     * Checks device type and serial number.
074     *
075     * @param deviceType - integer
076     * @param serNum - integer
077     * @return true if the device is in the list, else false
078     */
079    public boolean isDeviceRoutes(int deviceType, int serNum) {
080        for (Iterator<LnSimple7thGenDeviceRoutes> it = devicesRoutes.iterator(); it.hasNext();) {
081            LnSimple7thGenDeviceRoutes lndr = it.next();
082            if ((lndr.getDeviceType() == deviceType) &&
083                    (lndr.getSerNum() == serNum)) {
084                return true;
085            }
086        }
087        return false;
088    }
089
090    /**
091     * Get LnSimple7thGenDeviceRoutes based on devType and serNum, or null.
092     *
093     * @param deviceType - integer
094     * @param serNum - integer
095     * @return LnSimple7thGenDeviceRoutes
096     */
097    public LnSimple7thGenDeviceRoutes getDeviceRoutes(int deviceType, int serNum) {
098        for (Iterator<LnSimple7thGenDeviceRoutes> it = devicesRoutes.iterator(); it.hasNext();) {
099            LnSimple7thGenDeviceRoutes lndr = it.next();
100            if ((lndr.getDeviceType() == deviceType) &&
101                    (lndr.getSerNum() == serNum)) {
102                return lndr;
103            }
104        }
105        return null;
106    }
107
108    /**
109     * Get LnSimple7thGenDeviceRoutes based on devType and serNum, or null.
110     *
111     * Data from the dr parameter is ignored except the Device Type and Serial
112     * Number.
113     *
114     * @param dr - LnSimple7thGenDeviceRoutes
115     * @return LnSimple7thGenDeviceRoutes
116     */
117    public LnSimple7thGenDeviceRoutes getDeviceRoutes(LnSimple7thGenDeviceRoutes dr) {
118        for (Iterator<LnSimple7thGenDeviceRoutes> it = devicesRoutes.iterator(); it.hasNext();) {
119            LnSimple7thGenDeviceRoutes lndr = it.next();
120            if ((lndr.getDeviceType() == dr.getDeviceType()) &&
121                    (lndr.getSerNum() == dr.getSerNum())) {
122                return lndr;
123            }
124        }
125        return null;
126    }
127
128    /**
129     * Remove device.
130     *
131     * @param devType - int
132     * @param serNum - int
133     */
134    public void removeExistingDevice(int devType, int serNum) {
135        for (Iterator<LnSimple7thGenDeviceRoutes> it = devicesRoutes.iterator(); it.hasNext();) {
136            LnSimple7thGenDeviceRoutes lndr = it.next();
137            if ((lndr.getDeviceType() == devType) &&
138                    (lndr.getSerNum() == serNum)) {
139                devicesRoutes.remove(lndr);
140                return;
141            }
142        }
143        log.warn("removeExistingDevice: device type/serNum not found.");
144    }
145
146    /**
147     * report the number of devices.
148     * @return integer
149     */
150    public int size() {
151        return devicesRoutes.size();
152    }
153
154    /**
155     * get a LnSimple7thGenDeviceRoutes by its index.
156     *
157     * @param i - index, 0 thru (size()-1)
158     * @return LnSimple7thGenDeviceRoutes
159     */
160    public LnSimple7thGenDeviceRoutes getDeviceRoutes(int i) {
161        if ((devicesRoutes.size() > i) && (i >= 0)) {
162            return devicesRoutes.get(i);
163        }
164        return null;
165    }
166    private final static Logger log = LoggerFactory.getLogger(LnSimple7thGenDevicesRoutes.class);
167}