RingBuffer.java

1
package com.renomad.minum.utils;
2
3
import java.lang.reflect.Array;
4
import java.util.Iterator;
5
import java.util.List;
6
import java.util.NoSuchElementException;
7
8
public class RingBuffer<T> implements Iterable<T>{
9
10
    /**
11
     * The size of the inner array supporting this class.  Also,
12
     * the most items our RingBuffer can contain.
13
     */
14
    private final int limit;
15
16
    /**
17
     * The last index of the array - just the limit minus 1.
18
     */
19
    private final int lastIndex;
20
21
    /**
22
     * An instance of the class for this generic collection, used to
23
     * help build data structures.
24
     */
25
    private final T[] innerArray;
26
27
    /**
28
     * Index of where we place the next item
29
     */
30
    private int nextIndex;
31
32
    /**
33
     * Build a {@link RingBuffer}
34
     * @param limit the maximum size of the buffer
35
     */
36
    public RingBuffer(int limit, Class<T> clazz) {
37
        this.limit = limit;
38 1 1. <init> : Replaced integer subtraction with addition → KILLED
        this.lastIndex = limit - 1;
39
        @SuppressWarnings("unchecked")
40
        final T[] t = (T[]) Array.newInstance(clazz, limit);
41
        this.innerArray = t;
42
        this.nextIndex = 0;
43
    }
44
45
    /**
46
     * This will move the index forward and
47
     * wrap around at the end.
48
     */
49
    private int incrementIndex(int index) {
50 1 1. incrementIndex : negated conditional → KILLED
        if (index == lastIndex) {
51
            return 0;
52
        } else {
53 2 1. incrementIndex : Replaced integer addition with subtraction → KILLED
2. incrementIndex : replaced int return with 0 for com/renomad/minum/utils/RingBuffer::incrementIndex → KILLED
            return index + 1;
54
        }
55
    }
56
57
    public void add(T item) {
58
        innerArray[nextIndex] = item;
59
        nextIndex = incrementIndex(nextIndex);
60
    }
61
62
    public int getLimit() {
63 1 1. getLimit : replaced int return with 0 for com/renomad/minum/utils/RingBuffer::getLimit → KILLED
        return limit;
64
    }
65
66
    /**
67
     * Returns true if the data in the "myList" parameter is found
68
     * within the RingBuffer.
69
     */
70
    public boolean contains(List<T> myList) {
71 2 1. contains : negated conditional → KILLED
2. contains : negated conditional → KILLED
        if (myList == null || myList.isEmpty()) {
72
            throw new UtilsException("expected a valid non-empty list to search for in the RingBuffer");
73
        }
74
        int myListIndex = 0;
75
        int myListLength = myList.size();
76
        for (var value : this) {
77 1 1. contains : negated conditional → KILLED
            if (myList.get(myListIndex).equals(value)) {
78 1 1. contains : Changed increment from 1 to -1 → KILLED
                myListIndex += 1;
79
            } else {
80
                myListIndex = 0;
81
            }
82
83 1 1. contains : negated conditional → KILLED
            if (myListIndex == myListLength) {
84 1 1. contains : replaced boolean return with false for com/renomad/minum/utils/RingBuffer::contains → KILLED
                return true;
85
            }
86
87
        }
88 1 1. contains : replaced boolean return with true for com/renomad/minum/utils/RingBuffer::contains → KILLED
        return false;
89
    }
90
91
    /**
92
     * Returns true if the data in the "myList" parameter is found
93
     * within the RingBuffer at the index provided.
94
     */
95
    public boolean containsAt(List<T> myList, int index) {
96 2 1. containsAt : negated conditional → KILLED
2. containsAt : negated conditional → KILLED
        if (myList == null || myList.isEmpty()) {
97
            throw new UtilsException("expected a valid non-empty list to search for in the RingBuffer");
98
        }
99 4 1. containsAt : changed conditional boundary → KILLED
2. containsAt : changed conditional boundary → KILLED
3. containsAt : negated conditional → KILLED
4. containsAt : negated conditional → KILLED
        if (index > lastIndex || index < 0) {
100
            throw new UtilsException("expected an index greater than zero and less-than-or-equal to the last index of the buffer (the limit minus one)");
101
        }
102
        int i = 0;
103
        int myListIndex = 0;
104 1 1. containsAt : Replaced integer subtraction with addition → KILLED
        int myListLastIndex = myList.size() - 1;
105
        boolean comparing = false;
106
        var iterator = this.iterator();
107
        while (true) {
108
            var value = iterator.next();
109 1 1. containsAt : negated conditional → KILLED
            if (i == index) {
110
                comparing = true;
111
            }
112
113 1 1. containsAt : negated conditional → KILLED
            if (comparing) {
114 1 1. containsAt : negated conditional → KILLED
                if (! myList.get(myListIndex).equals(value)) {
115 1 1. containsAt : replaced boolean return with true for com/renomad/minum/utils/RingBuffer::containsAt → KILLED
                    return false;
116
                }
117 1 1. containsAt : negated conditional → KILLED
                if (myListIndex == myListLastIndex) {
118 1 1. containsAt : replaced boolean return with false for com/renomad/minum/utils/RingBuffer::containsAt → KILLED
                    return true;
119
                }
120 1 1. containsAt : Changed increment from 1 to -1 → KILLED
                myListIndex += 1;
121
            }
122
123 1 1. containsAt : Changed increment from 1 to -1 → KILLED
            i += 1;
124
        }
125
    }
126
127
    @Override
128
    public Iterator<T> iterator() {
129 1 1. iterator : replaced return value with null for com/renomad/minum/utils/RingBuffer::iterator → KILLED
        return new Iterator<>() {
130
131
            /**
132
             * As we loop through the buffer, we have to use a similar
133
             * technique to the "nextIndex" - wrapping around when we
134
             * hit the end
135
             */
136
            int iteratorIndex = nextIndex;
137
            /**
138
             * We'll count as we read values, stopping when we've read every slot.
139
             */
140
            int count = 0;
141
142
            @Override
143
            public boolean hasNext() {
144 3 1. hasNext : changed conditional boundary → KILLED
2. hasNext : negated conditional → KILLED
3. hasNext : replaced boolean return with true for com/renomad/minum/utils/RingBuffer$1::hasNext → KILLED
                return count < limit;
145
            }
146
147
            @Override
148
            public T next() {
149 1 1. next : negated conditional → KILLED
                if (!hasNext()) {
150
                    throw new NoSuchElementException();
151
                }
152
                var result = innerArray[iteratorIndex];
153
                iteratorIndex = incrementIndex(iteratorIndex);
154 1 1. next : Replaced integer addition with subtraction → KILLED
                count += 1;
155 1 1. next : replaced return value with null for com/renomad/minum/utils/RingBuffer$1::next → KILLED
                return result;
156
            }
157
        };
158
    }
159
160
    /**
161
     * Returns the value at the slot pointed to by the "nextIndex".  Note that
162
     * this will be null at first while the array starts to get filled.
163
     */
164
    public T atNextIndex() {
165 1 1. atNextIndex : replaced return value with null for com/renomad/minum/utils/RingBuffer::atNextIndex → KILLED
        return innerArray[nextIndex];
166
    }
167
168
}

Mutations

38

1.1
Location : <init>
Killed by : com.renomad.minum.utils.RingBufferTests.testIteratingPastLimit(com.renomad.minum.utils.RingBufferTests)
Replaced integer subtraction with addition → KILLED

50

1.1
Location : incrementIndex
Killed by : com.renomad.minum.utils.RingBufferTests.testIteratingPastLimit(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

53

1.1
Location : incrementIndex
Killed by : com.renomad.minum.utils.RingBufferTests.testIteratingPastLimit(com.renomad.minum.utils.RingBufferTests)
Replaced integer addition with subtraction → KILLED

2.2
Location : incrementIndex
Killed by : com.renomad.minum.utils.RingBufferTests.testIteratingPastLimit(com.renomad.minum.utils.RingBufferTests)
replaced int return with 0 for com/renomad/minum/utils/RingBuffer::incrementIndex → KILLED

63

1.1
Location : getLimit
Killed by : com.renomad.minum.web.RequestTests.testReadingEmptyStreamingMultipart(com.renomad.minum.web.RequestTests)
replaced int return with 0 for com/renomad/minum/utils/RingBuffer::getLimit → KILLED

71

1.1
Location : contains
Killed by : com.renomad.minum.utils.RingBufferTests.testContains(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

2.2
Location : contains
Killed by : com.renomad.minum.utils.RingBufferTests.testContains(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

77

1.1
Location : contains
Killed by : com.renomad.minum.utils.RingBufferTests.testContains(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

78

1.1
Location : contains
Killed by : com.renomad.minum.utils.RingBufferTests.testContains(com.renomad.minum.utils.RingBufferTests)
Changed increment from 1 to -1 → KILLED

83

1.1
Location : contains
Killed by : com.renomad.minum.utils.RingBufferTests.testContains(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

84

1.1
Location : contains
Killed by : com.renomad.minum.utils.RingBufferTests.testContains(com.renomad.minum.utils.RingBufferTests)
replaced boolean return with false for com/renomad/minum/utils/RingBuffer::contains → KILLED

88

1.1
Location : contains
Killed by : com.renomad.minum.utils.RingBufferTests.testContains(com.renomad.minum.utils.RingBufferTests)
replaced boolean return with true for com/renomad/minum/utils/RingBuffer::contains → KILLED

96

1.1
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

2.2
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

99

1.1
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
changed conditional boundary → KILLED

2.2
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
changed conditional boundary → KILLED

3.3
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

4.4
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

104

1.1
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
Replaced integer subtraction with addition → KILLED

109

1.1
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

113

1.1
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

114

1.1
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

115

1.1
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
replaced boolean return with true for com/renomad/minum/utils/RingBuffer::containsAt → KILLED

117

1.1
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

118

1.1
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
replaced boolean return with false for com/renomad/minum/utils/RingBuffer::containsAt → KILLED

120

1.1
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
Changed increment from 1 to -1 → KILLED

123

1.1
Location : containsAt
Killed by : com.renomad.minum.utils.RingBufferTests.testContainsAt(com.renomad.minum.utils.RingBufferTests)
Changed increment from 1 to -1 → KILLED

129

1.1
Location : iterator
Killed by : com.renomad.minum.utils.RingBufferTests.testIteratingPastLimit(com.renomad.minum.utils.RingBufferTests)
replaced return value with null for com/renomad/minum/utils/RingBuffer::iterator → KILLED

144

1.1
Location : hasNext
Killed by : com.renomad.minum.utils.RingBufferTests.testIteratingPastLimit(com.renomad.minum.utils.RingBufferTests)
changed conditional boundary → KILLED

2.2
Location : hasNext
Killed by : com.renomad.minum.utils.RingBufferTests.testIteratingPastLimit(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

3.3
Location : hasNext
Killed by : com.renomad.minum.utils.RingBufferTests.testIteratingPastLimit(com.renomad.minum.utils.RingBufferTests)
replaced boolean return with true for com/renomad/minum/utils/RingBuffer$1::hasNext → KILLED

149

1.1
Location : next
Killed by : com.renomad.minum.utils.RingBufferTests.testIteratingPastLimit(com.renomad.minum.utils.RingBufferTests)
negated conditional → KILLED

154

1.1
Location : next
Killed by : com.renomad.minum.utils.RingBufferTests.testIteratingPastLimit(com.renomad.minum.utils.RingBufferTests)
Replaced integer addition with subtraction → KILLED

155

1.1
Location : next
Killed by : com.renomad.minum.utils.RingBufferTests.testIteratingPastLimit(com.renomad.minum.utils.RingBufferTests)
replaced return value with null for com/renomad/minum/utils/RingBuffer$1::next → KILLED

165

1.1
Location : atNextIndex
Killed by : com.renomad.minum.web.RequestTests.test_Request_getMultipartForm_EdgeCase_PlayingWithClose(com.renomad.minum.web.RequestTests)
replaced return value with null for com/renomad/minum/utils/RingBuffer::atNextIndex → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0