From 1c2fb6ba88a889016992448e778a02b9a3dede8b Mon Sep 17 00:00:00 2001 From: "Jurgen J. Vinju" Date: Mon, 8 Jan 2024 18:47:47 +0100 Subject: [PATCH] added CF annotations --- .../collections/ShareableValuesHashSet.java | 18 +++++++++++++----- .../vallang/util/ShareableHashSet.java | 6 ++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/io/usethesource/vallang/impl/util/collections/ShareableValuesHashSet.java b/src/main/java/io/usethesource/vallang/impl/util/collections/ShareableValuesHashSet.java index 77bff52d9..6372b4440 100644 --- a/src/main/java/io/usethesource/vallang/impl/util/collections/ShareableValuesHashSet.java +++ b/src/main/java/io/usethesource/vallang/impl/util/collections/ShareableValuesHashSet.java @@ -15,6 +15,8 @@ import java.util.NoSuchElementException; import java.util.Set; +import org.checkerframework.checker.initialization.qual.UnknownInitialization; +import org.checkerframework.checker.nullness.qual.EnsuresNonNullIf; import org.checkerframework.checker.nullness.qual.Nullable; import io.usethesource.vallang.IValue; @@ -454,9 +456,9 @@ public String toString(){ } private static class SetIterator implements Iterator{ - private final Entry[] data; + private final Entry@Nullable[] data; - private Entry current; + private @Nullable Entry current; private int index; public SetIterator(Entry[] entries){ @@ -469,7 +471,7 @@ public SetIterator(Entry[] entries){ locateNext(); } - private void locateNext(){ + private void locateNext(@UnknownInitialization SetIterator this) { Entry next = current.next; if(next != null){ current = next; @@ -489,12 +491,17 @@ private void locateNext(){ index = 0; } + @EnsuresNonNullIf(expression="this.current", result=true) + @Override public boolean hasNext(){ return (current != null); } + @Override public IValue next(){ - if(!hasNext()) throw new NoSuchElementException("There are no more elements in this iteration"); + if (!hasNext()) { + throw new NoSuchElementException("There are no more elements in this iteration"); + } IValue value = current.value; locateNext(); @@ -502,7 +509,8 @@ public IValue next(){ return value; } - public void remove(){ + @Override + public void remove() { throw new UnsupportedOperationException("This iterator doesn't support removal."); } } diff --git a/src/main/java/io/usethesource/vallang/util/ShareableHashSet.java b/src/main/java/io/usethesource/vallang/util/ShareableHashSet.java index 9d9e23db9..6b314f022 100644 --- a/src/main/java/io/usethesource/vallang/util/ShareableHashSet.java +++ b/src/main/java/io/usethesource/vallang/util/ShareableHashSet.java @@ -15,6 +15,7 @@ import java.util.NoSuchElementException; import java.util.Set; +import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; /** @@ -162,8 +163,13 @@ private void ensureCapacity(){ * The value to insert. * @return Returns true if this set didn't contain the given value yet; false if it did. */ + @Override public boolean add(V value){ ensureCapacity(); + + if (value == null) { + throw new NullPointerException(); + } int hash = value.hashCode(); int position = hash & hashMask;