Skip to content

Commit 315ca2d

Browse files
chore(all): add jspecify (#16)
1 parent 23d20b4 commit 315ca2d

File tree

4 files changed

+52
-46
lines changed

4 files changed

+52
-46
lines changed

collections/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ plugins {
55

66
dependencies {
77
compileOnlyApi(libs.jetbrainsAnnotations)
8+
compileOnlyApi(libs.jspecify)
89
}
910

1011
applyJarMetadata("space.vectrix.sync.collections")

collections/src/main/java/module-info.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
1+
import org.jspecify.annotations.NullMarked;
2+
13
/**
24
* Sync Collections: Thread-safe collections for highly concurrent scenarios.
35
*/
6+
@NullMarked
47
module space.vectrix.sync.collections {
8+
requires transitive org.jspecify;
59
requires transitive org.jetbrains.annotations;
610

711
exports space.vectrix.sync.collections;

collections/src/main/java/space/vectrix/sync/collections/SyncMap.java

Lines changed: 46 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@
3939
import java.util.function.BiFunction;
4040
import java.util.function.Function;
4141
import org.jetbrains.annotations.ApiStatus;
42-
import org.jetbrains.annotations.NotNull;
43-
import org.jetbrains.annotations.Nullable;
4442
import org.jetbrains.annotations.UnknownNullability;
43+
import org.jspecify.annotations.NonNull;
44+
import org.jspecify.annotations.Nullable;
4545

4646
import static java.util.Objects.requireNonNull;
4747

@@ -191,7 +191,7 @@ public class SyncMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
191191
* @return the node, or null
192192
*/
193193
@SuppressWarnings("unchecked")
194-
/* package */ static <K, V> @Nullable Node<K, V> getNode(final Node<K, V>@NotNull [] table, final int index) {
194+
/* package */ static <K, V> @Nullable Node<K, V> getNode(final Node<K, V>[] table, final int index) {
195195
return (Node<K, V>) SyncMap.NODE_ARRAY.getAcquire(table, index);
196196
}
197197

@@ -206,7 +206,7 @@ public class SyncMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
206206
* @param nextNode the new node
207207
* @return true if new node was set, otherwise false
208208
*/
209-
/* package */ static <K, V> boolean replaceNode(final Node<K, V>@NotNull [] table, final int index, final @Nullable Node<K, V> nextNode) {
209+
/* package */ static <K, V> boolean replaceNode(final Node<K, V>[] table, final int index, final @Nullable Node<K, V> nextNode) {
210210
return SyncMap.NODE_ARRAY.compareAndExchangeRelease(table, index, (Node<K, V>) null, nextNode) == null;
211211
}
212212

@@ -219,7 +219,7 @@ public class SyncMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
219219
* @param <K> the key type
220220
* @param <V> the value type
221221
*/
222-
/* package */ static <K, V> void setNode(final Node<K, V> @NotNull [] table, final int index, final @Nullable Node<K, V> node) {
222+
/* package */ static <K, V> void setNode(final Node<K, V>[] table, final int index, final @Nullable Node<K, V> node) {
223223
SyncMap.NODE_ARRAY.setRelease(table, index, node);
224224
}
225225

@@ -234,7 +234,7 @@ public class SyncMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
234234
* Represents an immutable hash table that allows fast retrieval and updates
235235
* without locking.
236236
*/
237-
/* package */ transient volatile Node<K, V>@NotNull [] immutableTable;
237+
/* package */ transient volatile Node<K, V>[] immutableTable;
238238

239239
/**
240240
* Represents a mutable hash table that allows updates of new nodes with
@@ -292,7 +292,7 @@ public class SyncMap<K, V> extends AbstractMap<K, V> implements ConcurrentMap<K,
292292
/**
293293
* Represents a view of the entries in this map.
294294
*/
295-
private transient EntrySet entrySet;
295+
private transient @Nullable EntrySet entrySet;
296296

297297
/* ------------------------- < Public Operations > ------------------------- */
298298

@@ -351,7 +351,7 @@ public boolean isEmpty() {
351351
}
352352

353353
@Override
354-
public boolean containsKey(final @NotNull Object key) {
354+
public boolean containsKey(final Object key) {
355355
requireNonNull(key, "key");
356356

357357
Node<K, V>[] table = this.immutableTable; int length = table.length;
@@ -409,7 +409,7 @@ public boolean containsKey(final @NotNull Object key) {
409409
}
410410

411411
@Override
412-
public @Nullable V get(final @NotNull Object key) {
412+
public @Nullable V get(final Object key) {
413413
requireNonNull(key, "key");
414414

415415
Node<K, V>[] table = this.immutableTable; int length = table.length;
@@ -467,7 +467,7 @@ public boolean containsKey(final @NotNull Object key) {
467467
}
468468

469469
@Override
470-
public @NotNull V getOrDefault(final @NotNull Object key, final @NotNull V defaultValue) {
470+
public V getOrDefault(final Object key, final V defaultValue) {
471471
requireNonNull(key, "key");
472472
requireNonNull(defaultValue, "defaultValue");
473473

@@ -527,7 +527,7 @@ public boolean containsKey(final @NotNull Object key) {
527527

528528
@Override
529529
@SuppressWarnings("unchecked")
530-
public @Nullable V computeIfAbsent(final @NotNull K key, final @NotNull Function<? super @NotNull K, ? extends @Nullable V> mappingFunction) {
530+
public @Nullable V computeIfAbsent(final K key, final Function<? super K, ? extends @Nullable V> mappingFunction) {
531531
requireNonNull(key, "key");
532532
requireNonNull(mappingFunction, "mappingFunction");
533533

@@ -635,7 +635,7 @@ public boolean containsKey(final @NotNull Object key) {
635635

636636
@Override
637637
@SuppressWarnings("unchecked")
638-
public @Nullable V computeIfPresent(final @NotNull K key, final @NotNull BiFunction<? super @NotNull K, ? super @NotNull V, ? extends @Nullable V> remappingFunction) {
638+
public @Nullable V computeIfPresent(final K key, final BiFunction<? super K, ? super V, ? extends @Nullable V> remappingFunction) {
639639
requireNonNull(key, "key");
640640
requireNonNull(remappingFunction, "remappingFunction");
641641

@@ -731,7 +731,7 @@ public boolean containsKey(final @NotNull Object key) {
731731

732732
@Override
733733
@SuppressWarnings("unchecked")
734-
public @Nullable V compute(final @NotNull K key, final @NotNull BiFunction<? super @NotNull K, ? super @Nullable V, ? extends @Nullable V> remappingFunction) {
734+
public @Nullable V compute(final K key, final BiFunction<? super K, ? super @Nullable V, ? extends @Nullable V> remappingFunction) {
735735
requireNonNull(key, "key");
736736
requireNonNull(remappingFunction, "remappingFunction");
737737

@@ -859,7 +859,7 @@ public boolean containsKey(final @NotNull Object key) {
859859

860860
@Override
861861
@SuppressWarnings("unchecked")
862-
public @Nullable V putIfAbsent(final @NotNull K key, final @NotNull V value) {
862+
public @Nullable V putIfAbsent(final @NonNull K key, final V value) {
863863
requireNonNull(key, "key");
864864
requireNonNull(value, "value");
865865

@@ -954,7 +954,7 @@ public boolean containsKey(final @NotNull Object key) {
954954

955955
@Override
956956
@SuppressWarnings("unchecked")
957-
public @Nullable V put(final @NotNull K key, final @NotNull V value) {
957+
public @Nullable V put(final K key, final V value) {
958958
requireNonNull(key, "key");
959959
requireNonNull(value, "value");
960960

@@ -1049,7 +1049,7 @@ public boolean containsKey(final @NotNull Object key) {
10491049
return null;
10501050
}
10511051

1052-
/* package */ void amendNode(final int hash, final @NotNull K key, final @NotNull ObjectReference reference) {
1052+
/* package */ void amendNode(final int hash, final K key, final ObjectReference reference) {
10531053
Node<K, V>[] table = this.mutableTable;
10541054

10551055
for(Node<K, V> node; ; ) {
@@ -1088,7 +1088,7 @@ public boolean containsKey(final @NotNull Object key) {
10881088

10891089
@Override
10901090
@SuppressWarnings("unchecked")
1091-
public @Nullable V remove(final @NotNull Object key) {
1091+
public @Nullable V remove(final Object key) {
10921092
requireNonNull(key, "key");
10931093

10941094
Node<K, V>[] immutable, mutable; int length;
@@ -1172,7 +1172,7 @@ public boolean containsKey(final @NotNull Object key) {
11721172
}
11731173

11741174
@Override
1175-
public boolean remove(final @NotNull Object key, final @NotNull Object value) {
1175+
public boolean remove(final Object key, final Object value) {
11761176
requireNonNull(key, "key");
11771177
requireNonNull(value, "value");
11781178

@@ -1259,7 +1259,7 @@ public boolean remove(final @NotNull Object key, final @NotNull Object value) {
12591259

12601260
@Override
12611261
@SuppressWarnings("unchecked")
1262-
public @Nullable V replace(final @NotNull K key, final @NotNull V value) {
1262+
public @Nullable V replace(final @NonNull K key, final @NonNull V value) {
12631263
requireNonNull(key, "key");
12641264
requireNonNull(value, "value");
12651265

@@ -1335,7 +1335,7 @@ public boolean remove(final @NotNull Object key, final @NotNull Object value) {
13351335
}
13361336

13371337
@Override
1338-
public boolean replace(final @NotNull K key, final @NotNull V oldValue, final @NotNull V newValue) {
1338+
public boolean replace(final @NonNull K key, final @NonNull V oldValue, final @NonNull V newValue) {
13391339
requireNonNull(key, "key");
13401340
requireNonNull(oldValue, "oldValue");
13411341
requireNonNull(newValue, "newValue");
@@ -1417,7 +1417,7 @@ public boolean replace(final @NotNull K key, final @NotNull V oldValue, final @N
14171417

14181418
@Override
14191419
@SuppressWarnings("unchecked")
1420-
public void forEach(final @NotNull BiConsumer<? super @NotNull K, ? super @NotNull V> action) {
1420+
public void forEach(final BiConsumer<? super K, ? super V> action) {
14211421
requireNonNull(action, "action");
14221422

14231423
this.promote();
@@ -1479,7 +1479,7 @@ public void clear() {
14791479
* identical to the pair currently in the map.</p>
14801480
*/
14811481
@Override
1482-
public @NotNull Set<Map.Entry<K, V>> entrySet() {
1482+
public Set<Map.Entry<K, V>> entrySet() {
14831483
if(this.entrySet != null) return this.entrySet;
14841484
return this.entrySet = new EntrySet();
14851485
}
@@ -1505,7 +1505,7 @@ public void clear() {
15051505
* @return the mutable table
15061506
*/
15071507
@SuppressWarnings({"unchecked", "rawtypes"})
1508-
/* package */ @Nullable Node<K, V>[] initialize() {
1508+
/* package */ Node<K, V>@Nullable [] initialize() {
15091509
Node<K, V>[] source, destination;
15101510

15111511
long stamp = this.stampLock.getAcquire();
@@ -1585,7 +1585,7 @@ public void clear() {
15851585
* @param node the forwarding node
15861586
* @return the next table
15871587
*/
1588-
/* package */ Node<K, V>@Nullable [] forward(final @NotNull ForwardingNode<K, V> node) {
1588+
/* package */ Node<K, V>@Nullable [] forward(final ForwardingNode<K, V> node) {
15891589
if(this.amended) {
15901590
final Node<K, V>[] next = node.nextTable;
15911591
if(next == this.transferTable) return next;
@@ -1838,7 +1838,7 @@ public void clear() {
18381838
* @return the transfer progress count
18391839
*/
18401840
@SuppressWarnings({"SynchronizationOnLocalVariableOrMethodParameter"})
1841-
/* package */ int transfer(final Node<K, V>@NotNull [] source, final Node<K, V>@NotNull [] destination, final boolean resize) {
1841+
/* package */ int transfer(final Node<K, V>[] source, final Node<K, V>[] destination, final boolean resize) {
18421842
final int capacity = source.length, nextCapacity = destination.length;
18431843

18441844
int stride;
@@ -2116,7 +2116,7 @@ public void clear() {
21162116
}
21172117
}
21182118

2119-
private Object value;
2119+
private @Nullable Object value;
21202120

21212121
/* package */ ObjectReference(final @Nullable Object value) {
21222122
this.value = value;
@@ -2134,7 +2134,7 @@ public void clear() {
21342134
}
21352135

21362136
@SuppressWarnings("unchecked")
2137-
/* package */ <V> @NotNull V valueOr(final @NotNull V defaultValue) {
2137+
/* package */ <V> V valueOr(final V defaultValue) {
21382138
final Object value;
21392139
return ((value = ObjectReference.VALUE.getAcquire(this)) != null && value != SyncMap.EXPUNGED) ? (V) value : defaultValue;
21402140
}
@@ -2179,8 +2179,8 @@ public void clear() {
21792179

21802180
/* package */ final int hash;
21812181
/* package */ final K key;
2182-
/* package */ ObjectReference reference;
2183-
/* package */ Node<K, V> next;
2182+
/* package */ @Nullable ObjectReference reference;
2183+
/* package */ @Nullable Node<K, V> next;
21842184

21852185
/* package */ Node(final int hash, final @UnknownNullability K key, final @Nullable ObjectReference reference) {
21862186
this.hash = hash;
@@ -2189,11 +2189,11 @@ public void clear() {
21892189
Node.REFERENCE.setRelease(this, reference);
21902190
}
21912191

2192-
/* package */ @NotNull ObjectReference reference() {
2192+
/* package */ ObjectReference reference() {
21932193
return (ObjectReference) Node.REFERENCE.getAcquire(this);
21942194
}
21952195

2196-
/* package */ void reference(final @NotNull ObjectReference reference) {
2196+
/* package */ void reference(final ObjectReference reference) {
21972197
Node.REFERENCE.setRelease(this, reference);
21982198
}
21992199

@@ -2206,7 +2206,7 @@ public void clear() {
22062206
Node.NEXT.setRelease(this, node);
22072207
}
22082208

2209-
/* package */ @Nullable Node<K, V> find(final int hash, final @NotNull Object key) {
2209+
/* package */ @Nullable Node<K, V> find(final int hash, final Object key) {
22102210
Node<K, V> node = this; K nodeKey;
22112211
do {
22122212
if(node.hash == hash && ((nodeKey = node.key) == key || nodeKey.equals(key))) return node;
@@ -2225,17 +2225,17 @@ public void clear() {
22252225
/* package */ static final class ForwardingNode<K, V> extends Node<K, V> {
22262226
/* package */ transient final Node<K, V>[] nextTable;
22272227

2228-
/* package */ ForwardingNode(final Node<K, V>@NotNull [] nextTable) {
2228+
/* package */ ForwardingNode(final Node<K, V>[] nextTable) {
22292229
super(SyncMap.NODE_MOVED, null, null);
22302230

22312231
this.nextTable = nextTable;
22322232
}
22332233

22342234
@Override
2235-
/* package */ @Nullable Node<K, V> find(final int hash, final @NotNull Object key) {
2235+
/* package */ @Nullable Node<K, V> find(final int hash, final Object key) {
22362236
Node<K, V> node; int length, nodeHash; K nodeKey;
22372237

2238-
for(Node<K, V>[] table = this.nextTable; table != null && (length = table.length) > 0; ) {
2238+
for(Node<K, V>[] table = this.nextTable; (length = table.length) > 0; ) {
22392239
if((node = SyncMap.getNode(table, (length - 1) & hash)) == null) {
22402240
return null;
22412241
} else if((nodeHash = node.hash) == SyncMap.NODE_MOVED) {
@@ -2266,9 +2266,9 @@ public void clear() {
22662266
*/
22672267
/* package */ final class MapEntry implements Map.Entry<K, V> {
22682268
private final K key;
2269-
private V value;
2269+
private @Nullable V value;
22702270

2271-
/* package */ MapEntry(final @NotNull K key, final @Nullable V value) {
2271+
/* package */ MapEntry(final K key, final @Nullable V value) {
22722272
this.key = key;
22732273
this.value = value;
22742274
}
@@ -2279,12 +2279,12 @@ public K getKey() {
22792279
}
22802280

22812281
@Override
2282-
public V getValue() {
2282+
public @Nullable V getValue() {
22832283
return this.value;
22842284
}
22852285

22862286
@Override
2287-
public V setValue(final @NotNull V value) {
2287+
public @Nullable V setValue(final V value) {
22882288
final V previous = SyncMap.this.put(this.key, value);
22892289
this.value = value;
22902290
return previous;
@@ -2337,7 +2337,7 @@ public void clear() {
23372337
}
23382338

23392339
@Override
2340-
public @NotNull Iterator<Map.Entry<K, V>> iterator() {
2340+
public Iterator<Map.Entry<K, V>> iterator() {
23412341
SyncMap.this.promote();
23422342
return new EntryIterator(SyncMap.this.immutableTable);
23432343
}
@@ -2348,10 +2348,10 @@ public void clear() {
23482348
* given table.
23492349
*/
23502350
/* package */ final class EntryIterator extends Traverser<K, V> implements Iterator<Map.Entry<K, V>> {
2351-
private Map.Entry<K, V> next;
2352-
private Map.Entry<K, V> current;
2351+
private Map.@Nullable Entry<K, V> next;
2352+
private Map.@Nullable Entry<K, V> current;
23532353

2354-
/* package */ EntryIterator(final Node<K, V>@NotNull [] table) {
2354+
/* package */ EntryIterator(final Node<K, V>[] table) {
23552355
super(table);
23562356

23572357
this.advanceEntry();
@@ -2363,7 +2363,7 @@ public boolean hasNext() {
23632363
}
23642364

23652365
@Override
2366-
public @NotNull Entry<K, V> next() {
2366+
public Map.Entry<K, V> next() {
23672367
final Map.Entry<K, V> current;
23682368
if((current = this.next) == null) throw new NoSuchElementException();
23692369
this.current = current;
@@ -2403,10 +2403,10 @@ private void advanceEntry() {
24032403
/* package */ static class Traverser<K, V> {
24042404
private final Node<K, V>[] table;
24052405
private final int length;
2406-
private Node<K, V> next;
2406+
private @Nullable Node<K, V> next;
24072407
private int index;
24082408

2409-
/* package */ Traverser(final Node<K, V>@NotNull [] table) {
2409+
/* package */ Traverser(final Node<K, V>[] table) {
24102410
this.table = table;
24112411
this.length = table.length;
24122412
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ junit = "6.0.0"
1010

1111
[libraries]
1212
jetbrainsAnnotations = "org.jetbrains:annotations:26.0.2-1"
13+
jspecify = "org.jspecify:jspecify:1.0.0"
1314

1415
junit-api = { module = "org.junit.jupiter:junit-jupiter-api" }
1516
junit-bom = { module = "org.junit:junit-bom", version.ref = "junit" }

0 commit comments

Comments
 (0)