• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

LearnLib / automatalib / 6566010328

18 Oct 2023 08:07PM UTC coverage: 89.018% (+0.04%) from 88.975%
6566010328

push

github

mtf90
fix GUI tests on (headless) JDK21 builds

15287 of 17173 relevant lines covered (89.02%)

1.64 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

63.64
/commons/util/src/main/java/net/automatalib/common/util/nid/DynamicList.java
1
/* Copyright (C) 2013-2023 TU Dortmund
2
 * This file is part of AutomataLib, http://www.automatalib.net/.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
package net.automatalib.common.util.nid;
17

18
import java.util.AbstractList;
19
import java.util.Iterator;
20
import java.util.List;
21

22
import net.automatalib.common.smartcollection.ArrayWritable;
23
import net.automatalib.common.smartcollection.ResizingArrayStorage;
24
import org.checkerframework.checker.nullness.qual.Nullable;
25

26
public class DynamicList<T extends MutableNumericID> extends AbstractList<T> implements ArrayWritable<T> {
27

28
    private final ResizingArrayStorage<T> storage;
29
    private int size;
30

31
    public DynamicList() {
1✔
32
        this.size = 0;
1✔
33
        this.storage = new ResizingArrayStorage<>(MutableNumericID.class);
1✔
34
    }
1✔
35

36
    public DynamicList(List<? extends T> initial) {
1✔
37
        this.size = initial.size();
1✔
38
        this.storage = new ResizingArrayStorage<>(MutableNumericID.class, size);
1✔
39

40
        int idx = 0;
1✔
41
        for (T t : initial) {
1✔
42
            storage.array[idx] = t;
1✔
43
            t.setId(idx);
1✔
44
            idx++;
1✔
45
        }
1✔
46
    }
1✔
47

48
    @Override
49
    public int size() {
50
        return size;
1✔
51
    }
52

53
    @Override
54
    public boolean isEmpty() {
55
        return size == 0;
1✔
56
    }
57

58
    @Override
59
    public boolean remove(@Nullable Object elem) {
60
        return remove(elem, null);
×
61
    }
62

63
    @SuppressWarnings("nullness") // setting 'null' is fine, because we also decrease the size
64
    public boolean remove(@Nullable Object elem, @Nullable IDChangeNotifier<T> tracker) {
65
        if (!(elem instanceof MutableNumericID)) {
1✔
66
            return false;
×
67
        }
68
        MutableNumericID idElem = (MutableNumericID) elem;
1✔
69
        int idx = idElem.getId();
1✔
70
        T myElem = safeGet(idx);
1✔
71
        if (elem != myElem) {
1✔
72
            return false;
×
73
        }
74

75
        T last = storage.array[--size];
1✔
76

77
        if (idx != size) {
1✔
78
            storage.array[idx] = last;
1✔
79
            last.setId(idx);
1✔
80
            if (tracker != null) {
1✔
81
                tracker.notifyListeners(last, idx, size);
1✔
82
            }
83
        }
84
        storage.array[size] = null;
1✔
85
        myElem.setId(-1);
1✔
86

87
        return true;
1✔
88
    }
89

90
    @SuppressWarnings("nullness") // setting 'null' is fine, because we also decrease the size
91
    public T remove(int index, IDChangeNotifier<T> tracker) {
92
        T elem = get(index);
×
93

94
        T last = storage.array[--size];
×
95

96
        if (index != size) {
×
97
            storage.array[index] = last;
×
98
            last.setId(index);
×
99
            if (tracker != null) {
×
100
                tracker.notifyListeners(last, index, size);
×
101
            }
102
        }
103
        storage.array[size] = null;
×
104
        elem.setId(-1);
×
105

106
        return elem;
×
107
    }
108

109
    public @Nullable T safeGet(int index) {
110
        if (index < 0 || index >= size) {
1✔
111
            return null;
×
112
        }
113
        return storage.array[index];
1✔
114
    }
115

116
    @Override
117
    public boolean add(T elem) {
118
        storage.ensureCapacity(size + 1);
1✔
119
        storage.array[size] = elem;
1✔
120
        elem.setId(size);
1✔
121
        size++;
1✔
122
        return true;
1✔
123
    }
124

125
    @Override
126
    public T get(int index) {
127
        if (index < 0 || index >= size) {
1✔
128
            throw new IndexOutOfBoundsException("Invalid index " + index);
1✔
129
        }
130
        return storage.array[index];
1✔
131
    }
132

133
    @SuppressWarnings("nullness") // setting 'null' is fine, because we also decrease the size
134
    @Override
135
    public void clear() {
136
        for (int i = 0; i < size; i++) {
1✔
137
            storage.array[i] = null;
1✔
138
        }
139
        size = 0;
1✔
140
    }
1✔
141

142
    @Override
143
    public Iterator<T> iterator() {
144
        return new Iterator<T>() {
1✔
145

146
            int index;
147

148
            @Override
149
            public boolean hasNext() {
150
                return index < size;
1✔
151
            }
152

153
            @Override
154
            public T next() {
155
                return get(index++);
1✔
156
            }
157

158
            @Override
159
            public void remove() {
160
                DynamicList.this.remove(--index);
×
161
            }
×
162

163
        };
164
    }
165

166
    public void swap(int a, int b) {
167
        if (a == b) {
×
168
            return;
×
169
        }
170
        if (a < 0 || a >= size) {
×
171
            throw new IndexOutOfBoundsException("Invalid index " + a);
×
172
        }
173
        if (b < 0 || b >= size) {
×
174
            throw new IndexOutOfBoundsException("Invalid index " + b);
×
175
        }
176
        T tmp = storage.array[a];
×
177
        storage.array[a] = storage.array[b];
×
178
        storage.array[b] = tmp;
×
179
        storage.array[a].setId(a);
×
180
        storage.array[b].setId(b);
×
181
    }
×
182

183
    @Override
184
    public void writeToArray(int offset, @Nullable Object[] array, int tgtOfs, int num) {
185
        System.arraycopy(storage.array, offset, array, tgtOfs, num);
1✔
186
    }
1✔
187

188
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc