]> git.basschouten.com Git - openhab-addons.git/blob
ca0ff059b89c6b263adabd040a32f8184d434c74
[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.surepetcare.internal.dto;
14
15 import java.util.ArrayList;
16
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.openhab.binding.surepetcare.internal.SurePetcareConstants;
19
20 /**
21  * The {@link SurePetcareDeviceCurfewList} class is used to serialize a list of curfew parameters.
22  *
23  * @author Rene Scherer - Initial contribution
24  */
25 @NonNullByDefault
26 public class SurePetcareDeviceCurfewList extends ArrayList<SurePetcareDeviceCurfew> {
27
28     private static final long serialVersionUID = -6947992959305282143L;
29
30     /**
31      * Return the list element with the given index. If the list if too short, it will grow automatically to the given
32      * index.
33      *
34      * @return element with given index
35      */
36     @Override
37     public SurePetcareDeviceCurfew get(int index) {
38         while (size() <= index) {
39             // grow list to required size
40             add(new SurePetcareDeviceCurfew());
41         }
42         return super.get(index);
43     }
44
45     @Override
46     public String toString() {
47         StringBuilder builder = new StringBuilder("[");
48         for (SurePetcareDeviceCurfew c : this) {
49             builder.append("(").append(c).append(")");
50         }
51         builder.append("]]");
52         return builder.toString();
53     }
54
55     /**
56      * Creates a list of curfews with enabled ones at the front of the list and disabled ones at the back.
57      *
58      * @return new ordered list.
59      */
60     public SurePetcareDeviceCurfewList order() {
61         SurePetcareDeviceCurfewList orderedList = new SurePetcareDeviceCurfewList();
62         // remove any disabled curfews from the list
63         for (SurePetcareDeviceCurfew curfew : this) {
64             if (curfew.enabled) {
65                 orderedList.add(curfew);
66             }
67         }
68         // now fill up the list with empty disabled slots.
69         for (int i = orderedList.size(); i < SurePetcareConstants.FLAP_MAX_NUMBER_OF_CURFEWS; i++) {
70             orderedList.add(new SurePetcareDeviceCurfew());
71         }
72         return orderedList;
73     }
74
75     /**
76      * Trims the list of curfews and removes any disabled ones.
77      *
78      * @return the new compact list of curfews.
79      */
80     public SurePetcareDeviceCurfewList compact() {
81         SurePetcareDeviceCurfewList compactList = new SurePetcareDeviceCurfewList();
82         // remove any disabled curfews from the list
83         for (SurePetcareDeviceCurfew curfew : this) {
84             if (curfew.enabled) {
85                 compactList.add(curfew);
86             }
87         }
88         return compactList;
89     }
90 }