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

LearnLib / automatalib / 13138848026

04 Feb 2025 02:53PM UTC coverage: 92.108% (+2.2%) from 89.877%
13138848026

push

github

mtf90
[maven-release-plugin] prepare release automatalib-0.12.0

16609 of 18032 relevant lines covered (92.11%)

1.7 hits per line

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

62.67
/commons/util/src/main/java/net/automatalib/common/util/nid/DynamicList.java
1
/* Copyright (C) 2013-2025 TU Dortmund University
2
 * This file is part of AutomataLib <https://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.util.array.ArrayStorage;
23
import org.checkerframework.checker.nullness.qual.Nullable;
24

25
public class DynamicList<T extends MutableNumericID> extends AbstractList<T> {
26

27
    private final ArrayStorage<T> storage;
28
    private int size;
29

30
    public DynamicList() {
1✔
31
        this.size = 0;
1✔
32
        this.storage = new ArrayStorage<>();
1✔
33
    }
1✔
34

35
    public DynamicList(List<? extends T> initial) {
1✔
36
        this.size = initial.size();
1✔
37
        this.storage = new ArrayStorage<>(size);
1✔
38

39
        int idx = 0;
1✔
40
        for (T t : initial) {
1✔
41
            storage.set(idx, t);
1✔
42
            t.setId(idx);
1✔
43
            idx++;
1✔
44
        }
1✔
45
    }
1✔
46

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

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

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

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

74
        T last = storage.get(--size);
1✔
75

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

86
        return true;
1✔
87
    }
88

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

93
        T last = storage.get(--size);
×
94

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

105
        return elem;
×
106
    }
107

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

115
    @Override
116
    public boolean add(T elem) {
117
        storage.ensureCapacity(size + 1);
1✔
118
        storage.set(size, elem);
1✔
119
        elem.setId(size);
1✔
120
        size++;
1✔
121
        return true;
1✔
122
    }
123

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

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

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

145
            int index;
146

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

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

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

162
        };
163
    }
164

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