aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/api/objects/GTArrayList.java
blob: 33fbb18571d30cc01ddf3054a3cbfd151443857a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package gregtech.api.objects;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Objects;

import com.google.common.collect.Collections2;

public class GTArrayList<E> extends ArrayList<E> {

    private static final long serialVersionUID = 1L;
    private int size_sS;

    private final boolean mAllowNulls;

    public GTArrayList(boolean aAllowNulls, int aCapacity) {
        super(aCapacity);
        mAllowNulls = aAllowNulls;
    }

    @SafeVarargs
    public GTArrayList(boolean aAllowNulls, E... aArray) {
        super(Arrays.asList(aArray));
        mAllowNulls = aAllowNulls;
        if (!mAllowNulls) {
            size_sS = size();
            for (int i = 0; i < size_sS; i++) if (get(i) == null) {
                remove(i--);
                size_sS = size();
            }
        }
    }

    public GTArrayList(boolean aAllowNulls, Collection<? extends E> aList) {
        super(aList);
        mAllowNulls = aAllowNulls;
        if (!mAllowNulls) {
            size_sS = size();
            for (int i = 0; i < size_sS; i++) if (get(i) == null) {
                remove(i--);
                size_sS = size();
            }
        }
    }

    @Override
    public E set(int aIndex, E aElement) {
        if (mAllowNulls || aElement != null) return super.set(aIndex, aElement);
        return null;
    }

    @Override
    public boolean add(E aElement) {
        if (mAllowNulls || aElement != null) return super.add(aElement);
        return false;
    }

    @Override
    public void add(int aIndex, E aElement) {
        if (mAllowNulls || aElement != null) super.add(aIndex, aElement);
    }

    @Override
    public boolean addAll(Collection<? extends E> aList) {
        return super.addAll(Collections2.filter(aList, Objects::nonNull));
    }

    @Override
    public boolean addAll(int aIndex, Collection<? extends E> aList) {
        return super.addAll(aIndex, Collections2.filter(aList, Objects::nonNull));
    }
}