Skip to content

Commit 82e99e7

Browse files
committed
Add @flow and @contract annotations to tag methods
1 parent 2b8cb10 commit 82e99e7

2 files changed

Lines changed: 25 additions & 8 deletions

File tree

‎src/main/java/org/glavo/nbt/tag/ArrayTag.java‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package org.glavo.nbt.tag;
1717

1818
import org.glavo.nbt.internal.ArrayAccessor;
19+
import org.intellij.lang.annotations.Flow;
1920
import org.jetbrains.annotations.Contract;
2021
import org.jetbrains.annotations.MustBeInvokedByOverriders;
2122
import org.jetbrains.annotations.Nullable;
@@ -160,6 +161,8 @@ public final A getArray() {
160161
}
161162

162163
@Override
164+
@Contract(pure = true)
165+
@Flow(sourceIsContainer = true)
163166
public T getTag(int index) throws IndexOutOfBoundsException {
164167
Objects.checkIndex(index, size);
165168

@@ -187,6 +190,7 @@ public T getTag(int index) throws IndexOutOfBoundsException {
187190
/// @see IntArrayTag#get(int)
188191
/// @see LongArrayTag#get(int)
189192
@Contract(pure = true)
193+
@Flow(sourceIsContainer = true)
190194
public final E getValue(int index) throws IndexOutOfBoundsException {
191195
Objects.checkIndex(index, size);
192196
return accessor().get(values, index);
@@ -198,6 +202,7 @@ public final E getValue(int index) throws IndexOutOfBoundsException {
198202
///
199203
/// Each call returns a new buffer, but the underlying implementation may share the same array.
200204
@Contract(value = "-> new", pure = true)
205+
@Flow(sourceIsContainer = true, targetIsContainer = true)
201206
public final B getBuffer() {
202207
return accessor().getReadOnlyView(values, 0, size);
203208
}
@@ -217,7 +222,7 @@ final void setArrayWithoutClone(A array, int size) {
217222
///
218223
/// @throws IndexOutOfBoundsException if the index is out of bounds.
219224
@Contract(mutates = "this")
220-
public abstract void set(int index, E value) throws IndexOutOfBoundsException;
225+
public abstract void set(int index, @Flow(targetIsContainer = true) E value) throws IndexOutOfBoundsException;
221226

222227
/// Sets all values of the tag from an array.
223228
///
@@ -226,7 +231,8 @@ final void setArrayWithoutClone(A array, int size) {
226231
/// Calling this method will clear the current array, all subtags will be removed.
227232
@Contract(mutates = "this")
228233
@MustBeInvokedByOverriders
229-
public void setAll(A array) {
234+
public void setAll(@Flow(sourceIsContainer = true, targetIsContainer = true)
235+
A array) {
230236
clear();
231237

232238
int newSize = accessor().getLength(array);
@@ -243,7 +249,8 @@ public void setAll(A array) {
243249
///
244250
/// Calling this method will clear the current array, all subtags will be removed.
245251
@Contract(mutates = "this,param1")
246-
public final void setAll(B buffer) {
252+
public final void setAll(@Flow(sourceIsContainer = true, targetIsContainer = true)
253+
B buffer) {
247254
clear();
248255

249256
if (buffer.hasRemaining()) {
@@ -254,11 +261,13 @@ public final void setAll(B buffer) {
254261

255262
/// Appends the specified value to the end of this array.
256263
@Contract(mutates = "this")
257-
public abstract void add(E value);
264+
public abstract void add(@Flow(targetIsContainer = true)
265+
E value);
258266

259267
@Override
260268
@Contract(mutates = "this,param1")
261-
public final void addTag(T tag) throws IllegalArgumentException {
269+
public final void addTag(@Flow(targetIsContainer = true)
270+
T tag) throws IllegalArgumentException {
262271
if (tag.getParentTag() != null) {
263272
if (tag.getParentTag() == this) {
264273
int index = tag.getIndex();

‎src/main/java/org/glavo/nbt/tag/ParentTag.java‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.glavo.nbt.NBTParent;
1919
import org.glavo.nbt.internal.ArrayAccessor;
20+
import org.intellij.lang.annotations.Flow;
2021
import org.jetbrains.annotations.ApiStatus;
2122
import org.jetbrains.annotations.Contract;
2223
import org.jetbrains.annotations.UnknownNullability;
@@ -118,19 +119,23 @@ protected final void updateIndexes(int startIndex) {
118119

119120
/// Returns `true` if this tag has no subtags, `false` otherwise.
120121
@Override
122+
@Contract(pure = true)
121123
public final boolean isEmpty() {
122124
return size == 0;
123125
}
124126

125127
/// Returns the number of subtags in this tag.
126128
@Override
129+
@Contract(pure = true)
127130
public final int size() {
128131
return size;
129132
}
130133

131134
/// Returns the subtag at the given index.
132135
///
133136
/// @throws IndexOutOfBoundsException if the index is out of bounds.
137+
@Contract(pure = true)
138+
@Flow(sourceIsContainer = true)
134139
public T getTag(int index) throws IndexOutOfBoundsException {
135140
Objects.checkIndex(index, size);
136141
@SuppressWarnings("unchecked")
@@ -144,12 +149,14 @@ public T getTag(int index) throws IndexOutOfBoundsException {
144149
///
145150
/// If the `tag` is already a child of another tag, removes it from old parent and adds it to this tag.
146151
@Contract(mutates = "this,param1")
147-
public abstract void addTag(T tag) throws IllegalArgumentException;
152+
public abstract void addTag(@Flow(targetIsContainer = true)
153+
T tag) throws IllegalArgumentException;
148154

149155
/// Adds all `tags` to this tag.
150156
///
151157
/// @see #addTag(Tag)
152-
public final void addTags(Iterable<? extends T> tags) throws IllegalArgumentException {
158+
public final void addTags(@Flow(sourceIsContainer = true, targetIsContainer = true)
159+
Iterable<? extends T> tags) throws IllegalArgumentException {
153160
if (this == tags) {
154161
return;
155162
}
@@ -163,7 +170,8 @@ public final void addTags(Iterable<? extends T> tags) throws IllegalArgumentExce
163170
///
164171
/// @see #addTag(Tag)
165172
@SafeVarargs
166-
public final void addTags(T... tags) throws IllegalArgumentException {
173+
public final void addTags(@Flow(sourceIsContainer = true, targetIsContainer = true)
174+
T... tags) throws IllegalArgumentException {
167175
for (T tag : tags) {
168176
this.addTag(tag);
169177
}

0 commit comments

Comments
 (0)