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

LearnLib / automatalib / 12771457454

14 Jan 2025 03:47PM UTC coverage: 91.741% (+0.1%) from 91.62%
12771457454

push

github

mtf90
cleanup type parameters of EmptyWord

2 of 2 new or added lines in 1 file covered. (100.0%)

35 existing lines in 10 files now uncovered.

16551 of 18041 relevant lines covered (91.74%)

1.7 hits per line

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

94.0
/commons/util/src/main/java/net/automatalib/common/util/array/ArrayStorage.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.array;
17

18
import java.util.AbstractList;
19
import java.util.Arrays;
20
import java.util.Collection;
21
import java.util.Iterator;
22
import java.util.List;
23
import java.util.Objects;
24
import java.util.RandomAccess;
25

26
import org.checkerframework.checker.nullness.qual.Nullable;
27

28
/**
29
 * A type-safe wrapper around a simple {@code Object[]} array. This class is mainly useful when in need for heavily
30
 * generic index-based data storage. While it is compatible with {@link List}s, it does not support
31
 * {@link List#add(Object) dynamic growth} but {@link #ensureCapacity(int) explicit memory allocation}. As a result,
32
 * elements can be {@link #set(int, Object) set} at non-contiguous positions.
33
 *
34
 * @param <T>
35
 *         the type of stored elements
36
 */
37
public final class ArrayStorage<T> extends AbstractList<T> implements RandomAccess {
38

39
    /**
40
     * The default initial capacity of the array storage.
41
     */
42
    public static final int DEFAULT_INITIAL_CAPACITY = 10;
43

44
    private int size;
45
    private T[] storage;
46

47
    /**
48
     * Constructor. Creates an array storage with a default initial capacity of
49
     * {@link ArrayStorage#DEFAULT_INITIAL_CAPACITY}.
50
     */
51
    public ArrayStorage() {
52
        this(DEFAULT_INITIAL_CAPACITY);
1✔
53
    }
1✔
54

55
    /**
56
     * Constructor. Creates an array with the specified initial capacity.
57
     *
58
     * @param size
59
     *         the initial capacity.
60
     */
61
    public ArrayStorage(int size) {
62
        this(new @Nullable Object[size]);
2✔
63
    }
2✔
64

65
    /**
66
     * Constructor. (Shallowly) copies the elements from the given collections into the array storage.
67
     *
68
     * @param collection
69
     *         the other storage whose data should be (shallowly) cloned
70
     */
71
    public ArrayStorage(Collection<? extends T> collection) {
72
        this(collection.toArray());
2✔
73
    }
2✔
74

75
    @SuppressWarnings("unchecked")
76
    private ArrayStorage(@Nullable Object[] storage) {
2✔
77
        this.size = storage.length;
2✔
78
        this.storage = (T[]) storage;
2✔
79
    }
2✔
80

81
    /**
82
     * Ensures that the storage container can store at least the given amount of elements. Note that the implementation
83
     * may allocate more memory (to speed up subsequent increments) but elements can only ever be accessed until the
84
     * requested position.
85
     *
86
     * @param minCapacity
87
     *         the minimum capacity required
88
     *
89
     * @return {@code true} if the internal storage was re-allocated, {@code false} otherwise
90
     */
91
    public boolean ensureCapacity(int minCapacity) {
92
        size = Math.max(size, minCapacity);
2✔
93

94
        if (minCapacity <= storage.length) {
2✔
95
            return false;
1✔
96
        }
97

98
        final int newCapacity = ArrayUtil.computeNewCapacity(storage.length, minCapacity, 0);
2✔
99

100
        storage = Arrays.copyOf(storage, newCapacity);
2✔
101
        return true;
2✔
102
    }
103

104
    /**
105
     * Sets all the elements in the array to the specified value.
106
     *
107
     * @param value
108
     *         the value.
109
     */
110
    public void setAll(T value) {
111
        Arrays.fill(storage, 0, size, value);
1✔
112
    }
1✔
113

114
    /**
115
     * Swaps the contents with the given storage.
116
     *
117
     * @param that
118
     *         the container to swap contents with
119
     */
120
    public void swap(ArrayStorage<T> that) {
121
        final T[] tmpStorage = this.storage;
1✔
122
        final int tmpSize = this.size;
1✔
123

124
        this.storage = that.storage;
1✔
125
        this.size = that.size;
1✔
126

127
        that.storage = tmpStorage;
1✔
128
        that.size = tmpSize;
1✔
129
    }
1✔
130

131
    @Override
132
    public T get(int index) {
133
        if (index >= size) {
2✔
134
            throw new IndexOutOfBoundsException();
2✔
135
        }
136
        return storage[index];
2✔
137
    }
138

139
    @Override
140
    public T set(int index, T element) {
141
        final T oldValue = get(index);
2✔
142
        storage[index] = element;
2✔
143
        return oldValue;
2✔
144
    }
145

146
    @Override
147
    public int size() {
148
        return size;
2✔
149
    }
150

151
    @Override
152
    @SuppressWarnings("return")
153
    public Object[] toArray() {
154
        return Arrays.copyOf(storage, size);
2✔
155
    }
156

157
    @Override
158
    public Iterator<T> iterator() {
159
        return ArrayUtil.iterator(storage, 0, size);
2✔
160
    }
161

162
    @Override
163
    public boolean equals(@Nullable Object o) {
164
        if (this == o) {
2✔
UNCOV
165
            return true;
×
166
        }
167
        if (!(o instanceof ArrayStorage)) {
2✔
UNCOV
168
            return false;
×
169
        }
170

171
        final ArrayStorage<?> that = (ArrayStorage<?>) o;
2✔
172

173
        if (this.size != that.size) {
2✔
UNCOV
174
            return false;
×
175
        }
176

177
        for (int i = 0; i < this.size; i++) {
2✔
178
            if (!Objects.equals(this.storage[i], that.storage[i])) {
2✔
179
                return false;
2✔
180
            }
181
        }
182

183
        return true;
1✔
184
    }
185

186
    @Override
187
    public int hashCode() {
188
        final int prime = 31;
2✔
189
        int result = 1;
2✔
190

191
        for (int i = 0; i < this.size; i++) {
2✔
192
            result = prime * result + Objects.hashCode(this.storage[i]);
2✔
193
        }
194

195
        return result;
2✔
196
    }
197
}
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

© 2026 Coveralls, Inc