From e4ba8dcd682b23d8747d00809c39c024de90f267 Mon Sep 17 00:00:00 2001 From: Andrea Cavalli Date: Wed, 22 Sep 2021 18:26:16 +0200 Subject: [PATCH] Annotate NotNull and Nullable --- .../stream/ByteBufferBackedInputStream.java | 3 +- .../stream/DataInputOutputImpl.java | 8 ++-- .../stream/DataInputOutputStream.java | 4 +- .../stream/SafeDataInputStream.java | 40 +++++++++---------- .../commonutils/stream/SafeOutputStream.java | 7 ++-- .../java/org/warp/commonutils/type/Bytes.java | 2 +- .../type/FastUtilStackSetWrapper.java | 4 +- .../type/FastUtilStackWrapper.java | 4 +- .../commonutils/type/FloatPriorityQueue.java | 24 ++++++----- .../type/FloatPriorityQueueView.java | 4 +- .../commonutils/type/ImmutableLinkedSet.java | 8 ++-- .../type/ShortNamedThreadFactory.java | 3 +- 12 files changed, 59 insertions(+), 52 deletions(-) diff --git a/src/main/java/org/warp/commonutils/stream/ByteBufferBackedInputStream.java b/src/main/java/org/warp/commonutils/stream/ByteBufferBackedInputStream.java index 92952f5..3f4bc37 100644 --- a/src/main/java/org/warp/commonutils/stream/ByteBufferBackedInputStream.java +++ b/src/main/java/org/warp/commonutils/stream/ByteBufferBackedInputStream.java @@ -3,6 +3,7 @@ package org.warp.commonutils.stream; import java.io.IOException; import java.io.InputStream; import java.nio.ByteBuffer; +import org.jetbrains.annotations.NotNull; /** * Simple {@link InputStream} implementation that exposes currently @@ -19,7 +20,7 @@ public class ByteBufferBackedInputStream extends InputStream { public int read() throws IOException { return _b.hasRemaining() ? (_b.get() & 0xFF) : -1; } @Override - public int read(byte[] bytes, int off, int len) throws IOException { + public int read(byte @NotNull [] bytes, int off, int len) throws IOException { if (!_b.hasRemaining()) return -1; len = Math.min(len, _b.remaining()); _b.get(bytes, off, len); diff --git a/src/main/java/org/warp/commonutils/stream/DataInputOutputImpl.java b/src/main/java/org/warp/commonutils/stream/DataInputOutputImpl.java index c567214..9acf2fe 100644 --- a/src/main/java/org/warp/commonutils/stream/DataInputOutputImpl.java +++ b/src/main/java/org/warp/commonutils/stream/DataInputOutputImpl.java @@ -26,12 +26,12 @@ public class DataInputOutputImpl implements DataInputOutput { } @Override - public void readFully(byte[] bytes) throws IOException { + public void readFully(byte @NotNull [] bytes) throws IOException { in.readFully(bytes); } @Override - public void readFully(byte[] bytes, int i, int i1) throws IOException { + public void readFully(byte @NotNull [] bytes, int i, int i1) throws IOException { in.readFully(bytes, i, i1); } @@ -107,12 +107,12 @@ public class DataInputOutputImpl implements DataInputOutput { } @Override - public void write(byte[] bytes) throws IOException { + public void write(byte @NotNull [] bytes) throws IOException { out.write(bytes); } @Override - public void write(byte[] bytes, int i, int i1) throws IOException { + public void write(byte @NotNull [] bytes, int i, int i1) throws IOException { out.write(bytes, i, i1); } diff --git a/src/main/java/org/warp/commonutils/stream/DataInputOutputStream.java b/src/main/java/org/warp/commonutils/stream/DataInputOutputStream.java index 5a17e7f..16a1a3a 100644 --- a/src/main/java/org/warp/commonutils/stream/DataInputOutputStream.java +++ b/src/main/java/org/warp/commonutils/stream/DataInputOutputStream.java @@ -25,12 +25,12 @@ public class DataInputOutputStream extends DataOutputStream implements DataInput } @Override - public void readFully(byte[] bytes) throws IOException { + public void readFully(byte @NotNull [] bytes) throws IOException { in.readFully(bytes); } @Override - public void readFully(byte[] bytes, int i, int i1) throws IOException { + public void readFully(byte @NotNull [] bytes, int i, int i1) throws IOException { in.readFully(bytes, i, i1); } diff --git a/src/main/java/org/warp/commonutils/stream/SafeDataInputStream.java b/src/main/java/org/warp/commonutils/stream/SafeDataInputStream.java index 3e84e5c..e5a3936 100644 --- a/src/main/java/org/warp/commonutils/stream/SafeDataInputStream.java +++ b/src/main/java/org/warp/commonutils/stream/SafeDataInputStream.java @@ -26,6 +26,7 @@ package org.warp.commonutils.stream; import java.io.DataInput; +import org.jetbrains.annotations.NotNull; /** * A data input stream lets an application read primitive Java data @@ -56,8 +57,8 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn /** * working arrays initialized on demand by readUTF */ - private byte bytearr[] = new byte[80]; - private char chararr[] = new char[80]; + private byte[] bytearr = new byte[80]; + private char[] chararr = new char[80]; /** * Reads some number of bytes from the contained input stream and @@ -93,7 +94,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn * @see SafeFilterInputStream#in * @see java.io.InputStream#read(byte[], int, int) */ - public final int read(byte b[]) { + public final int read(byte[] b) { return in.read(b, 0, b.length); } @@ -138,7 +139,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn * @see SafeFilterInputStream#in * @see java.io.InputStream#read(byte[], int, int) */ - public final int read(byte b[], int off, int len) { + public final int read(byte[] b, int off, int len) { return in.read(b, off, len); } @@ -154,7 +155,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn * @throws NullPointerException if {@code b} is {@code null}. * @see SafeFilterInputStream#in */ - public final void readFully(byte b[]) { + public final void readFully(byte @NotNull [] b) { readFully(b, 0, b.length); } @@ -175,7 +176,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn * {@code b.length - off}. * @see SafeFilterInputStream#in */ - public final void readFully(byte b[], int off, int len) { + public final void readFully(byte @NotNull [] b, int off, int len) { if (len < 0) throw new IndexOutOfBoundsException(); int n = 0; @@ -199,7 +200,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn */ public final int skipBytes(int n) { int total = 0; - int cur = 0; + int cur; while ((total 0)) { total += cur; @@ -345,7 +346,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); } - private byte readBuffer[] = new byte[8]; + private final byte[] readBuffer = new byte[8]; /** * See the general contract of the {@code readLong} @@ -405,7 +406,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn return Double.longBitsToDouble(readLong()); } - private char lineBuffer[]; + private char[] lineBuffer; /** * See the general contract of the {@code readLine} @@ -435,7 +436,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn */ @Deprecated public final String readLine() { - char buf[] = lineBuffer; + char[] buf = lineBuffer; if (buf == null) { buf = lineBuffer = new char[128]; @@ -489,7 +490,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn * @return a Unicode string. * @see SafeDataInputStream#readUTF(SafeDataInputStream) */ - public final String readUTF() { + public final @NotNull String readUTF() { return readUTF(this); } @@ -507,17 +508,16 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn * @return a Unicode string. * @see SafeDataInputStream#readUnsignedShort() */ - public static final String readUTF(SafeDataInputStream in) { + public static String readUTF(SafeDataInputStream in) { int utflen = in.readUnsignedShort(); - byte[] bytearr = null; - char[] chararr = null; - SafeDataInputStream dis = in; - if (dis.bytearr.length < utflen){ - dis.bytearr = new byte[utflen*2]; - dis.chararr = new char[utflen*2]; + byte[] bytearr; + char[] chararr; + if (in.bytearr.length < utflen){ + in.bytearr = new byte[utflen*2]; + in.chararr = new char[utflen*2]; } - chararr = dis.chararr; - bytearr = dis.bytearr; + chararr = in.chararr; + bytearr = in.bytearr; int c, char2, char3; int count = 0; diff --git a/src/main/java/org/warp/commonutils/stream/SafeOutputStream.java b/src/main/java/org/warp/commonutils/stream/SafeOutputStream.java index f0190d1..0fa9867 100644 --- a/src/main/java/org/warp/commonutils/stream/SafeOutputStream.java +++ b/src/main/java/org/warp/commonutils/stream/SafeOutputStream.java @@ -4,6 +4,7 @@ import java.io.Closeable; import java.io.Flushable; import java.io.OutputStream; import java.util.Objects; +import org.jetbrains.annotations.NotNull; /** * This abstract class is the superclass of all classes representing @@ -62,7 +63,7 @@ public abstract class SafeOutputStream extends OutputStream implements Closeable } @Override - public void write(byte b[], int off, int len) { + public void write(byte @NotNull [] b, int off, int len) { Objects.checkFromIndexSize(off, len, b.length); ensureOpen(); } @@ -97,7 +98,7 @@ public abstract class SafeOutputStream extends OutputStream implements Closeable * @param b the data. * @see java.io.OutputStream#write(byte[], int, int) */ - public void write(byte b[]) { + public void write(byte @NotNull [] b) { write(b, 0, b.length); } @@ -126,7 +127,7 @@ public abstract class SafeOutputStream extends OutputStream implements Closeable * @param off the start offset in the data. * @param len the number of bytes to write. */ - public void write(byte b[], int off, int len) { + public void write(byte[] b, int off, int len) { Objects.checkFromIndexSize(off, len, b.length); // len == 0 condition implicitly handled by loop bounds for (int i = 0 ; i < len ; i++) { diff --git a/src/main/java/org/warp/commonutils/type/Bytes.java b/src/main/java/org/warp/commonutils/type/Bytes.java index 15b6505..83740cd 100644 --- a/src/main/java/org/warp/commonutils/type/Bytes.java +++ b/src/main/java/org/warp/commonutils/type/Bytes.java @@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull; public class Bytes { public final byte[] data; - public Bytes(@NotNull byte[] data) { + public Bytes(byte @NotNull[] data) { this.data = data; } diff --git a/src/main/java/org/warp/commonutils/type/FastUtilStackSetWrapper.java b/src/main/java/org/warp/commonutils/type/FastUtilStackSetWrapper.java index 5c5d857..15bc4df 100644 --- a/src/main/java/org/warp/commonutils/type/FastUtilStackSetWrapper.java +++ b/src/main/java/org/warp/commonutils/type/FastUtilStackSetWrapper.java @@ -48,13 +48,13 @@ public class FastUtilStackSetWrapper implements StackSet, Collection { @NotNull @Override - public Object[] toArray() { + public Object @NotNull [] toArray() { return linkedHashSet.toArray(); } @NotNull @Override - public T1[] toArray(@NotNull T1[] a) { + public T1 @NotNull [] toArray(@NotNull T1 @NotNull [] a) { //noinspection SuspiciousToArrayCall return linkedHashSet.toArray(a); } diff --git a/src/main/java/org/warp/commonutils/type/FastUtilStackWrapper.java b/src/main/java/org/warp/commonutils/type/FastUtilStackWrapper.java index ac39fd2..6fc8888 100644 --- a/src/main/java/org/warp/commonutils/type/FastUtilStackWrapper.java +++ b/src/main/java/org/warp/commonutils/type/FastUtilStackWrapper.java @@ -49,13 +49,13 @@ public class FastUtilStackWrapper implements Stack, Collection { @NotNull @Override - public Object[] toArray() { + public Object @NotNull [] toArray() { return collection.toArray(); } @NotNull @Override - public T1[] toArray(@NotNull T1[] a) { + public T1 @NotNull [] toArray(@NotNull T1 @NotNull [] a) { //noinspection SuspiciousToArrayCall return collection.toArray(a); } diff --git a/src/main/java/org/warp/commonutils/type/FloatPriorityQueue.java b/src/main/java/org/warp/commonutils/type/FloatPriorityQueue.java index fc064ec..586e6c7 100644 --- a/src/main/java/org/warp/commonutils/type/FloatPriorityQueue.java +++ b/src/main/java/org/warp/commonutils/type/FloatPriorityQueue.java @@ -18,7 +18,7 @@ public class FloatPriorityQueue implements Queue { private final Queue> internalQueue; public static FloatPriorityQueue of() { - return new FloatPriorityQueue(0); + return new FloatPriorityQueue<>(0); } public static FloatPriorityQueue of(T value, float score) { @@ -33,6 +33,7 @@ public class FloatPriorityQueue implements Queue { return pq; } + @SafeVarargs public static FloatPriorityQueue of(ScoredValue... values) { var pq = new FloatPriorityQueue(values.length); for (ScoredValue value : values) { @@ -64,12 +65,12 @@ public class FloatPriorityQueue implements Queue { } public static FloatPriorityQueue synchronize(FloatPriorityQueue queue) { - return new SynchronizedFloatPriorityQueue(queue.contentValues, queue.internalQueue); + return new SynchronizedFloatPriorityQueue<>(queue.contentValues, queue.internalQueue); } public static FloatPriorityQueue synchronizedPq(int initialCapacity) { var pq = new FloatPriorityQueue(initialCapacity); - return new SynchronizedFloatPriorityQueue(pq.contentValues, pq.internalQueue); + return new SynchronizedFloatPriorityQueue<>(pq.contentValues, pq.internalQueue); } @Override @@ -95,7 +96,7 @@ public class FloatPriorityQueue implements Queue { public Iterator iterator() { assert contentValues.size() == internalQueue.size(); var it = internalQueue.iterator(); - return new Iterator() { + return new Iterator<>() { @Override public boolean hasNext() { assert contentValues.size() == internalQueue.size(); @@ -120,14 +121,15 @@ public class FloatPriorityQueue implements Queue { @NotNull @Override - public Object[] toArray() { + public Object @NotNull [] toArray() { assert contentValues.size() == internalQueue.size(); return internalQueue.stream().map(FloatPriorityQueue::getValueOrNull).toArray(Object[]::new); } + @SuppressWarnings({"SuspiciousToArrayCall", "unchecked"}) @NotNull @Override - public T1[] toArray(@NotNull T1[] a) { + public T1 @NotNull [] toArray(@NotNull T1 @NotNull [] a) { assert contentValues.size() == internalQueue.size(); return internalQueue .stream() @@ -286,7 +288,7 @@ public class FloatPriorityQueue implements Queue { } public FloatPriorityQueueView view() { - return new FloatPriorityQueueView(this); + return new FloatPriorityQueueView<>(this); } private static class SynchronizedFloatPriorityQueue extends FloatPriorityQueue { @@ -313,7 +315,7 @@ public class FloatPriorityQueue implements Queue { @Override public synchronized @NotNull Iterator iterator() { var it = super.iterator(); - return new Iterator() { + return new Iterator<>() { @Override public boolean hasNext() { synchronized (SynchronizedFloatPriorityQueue.this) { @@ -331,12 +333,13 @@ public class FloatPriorityQueue implements Queue { } @Override - public synchronized @NotNull Object[] toArray() { + public synchronized @NotNull Object @NotNull [] toArray() { return super.toArray(); } + @SuppressWarnings("SuspiciousToArrayCall") @Override - public synchronized @NotNull T1[] toArray(@NotNull T1[] a) { + public synchronized @NotNull T1 @NotNull [] toArray(@NotNull T1 @NotNull [] a) { return super.toArray(a); } @@ -356,6 +359,7 @@ public class FloatPriorityQueue implements Queue { return super.addTop(t); } + @Deprecated @Override public synchronized boolean add(T t) { return super.add(t); diff --git a/src/main/java/org/warp/commonutils/type/FloatPriorityQueueView.java b/src/main/java/org/warp/commonutils/type/FloatPriorityQueueView.java index 2abb12f..60572c9 100644 --- a/src/main/java/org/warp/commonutils/type/FloatPriorityQueueView.java +++ b/src/main/java/org/warp/commonutils/type/FloatPriorityQueueView.java @@ -65,13 +65,13 @@ public class FloatPriorityQueueView implements Queue { @NotNull @Override - public Object[] toArray() { + public Object @NotNull [] toArray() { return queue.toArray(); } @NotNull @Override - public T1[] toArray(@NotNull T1[] a) { + public T1 @NotNull [] toArray(@NotNull T1 @NotNull [] a) { //noinspection SuspiciousToArrayCall return queue.toArray(a); } diff --git a/src/main/java/org/warp/commonutils/type/ImmutableLinkedSet.java b/src/main/java/org/warp/commonutils/type/ImmutableLinkedSet.java index eea5fb6..1501c00 100644 --- a/src/main/java/org/warp/commonutils/type/ImmutableLinkedSet.java +++ b/src/main/java/org/warp/commonutils/type/ImmutableLinkedSet.java @@ -38,7 +38,7 @@ public class ImmutableLinkedSet implements Set { @Override public Iterator iterator() { var it = set.iterator(); - return new Iterator() { + return new Iterator<>() { @Override public boolean hasNext() { return it.hasNext(); @@ -58,13 +58,13 @@ public class ImmutableLinkedSet implements Set { @NotNull @Override - public Object[] toArray() { + public Object @NotNull [] toArray() { return set.toArray(); } @NotNull @Override - public T[] toArray(@NotNull T[] a) { + public T @NotNull [] toArray(@NotNull T @NotNull [] a) { //noinspection SuspiciousToArrayCall return set.toArray(a); } @@ -118,7 +118,7 @@ public class ImmutableLinkedSet implements Set { @Override public Spliterator spliterator() { var spl = set.spliterator(); - return new Spliterator() { + return new Spliterator<>() { @Override public boolean tryAdvance(Consumer action) { return spl.tryAdvance(action); diff --git a/src/main/java/org/warp/commonutils/type/ShortNamedThreadFactory.java b/src/main/java/org/warp/commonutils/type/ShortNamedThreadFactory.java index 595c423..54e55c1 100644 --- a/src/main/java/org/warp/commonutils/type/ShortNamedThreadFactory.java +++ b/src/main/java/org/warp/commonutils/type/ShortNamedThreadFactory.java @@ -21,6 +21,7 @@ import java.util.Locale; import java.util.concurrent.Executors; import java.util.concurrent.ThreadFactory; import java.util.concurrent.atomic.AtomicInteger; +import org.jetbrains.annotations.NotNull; /** * A default {@link ThreadFactory} implementation that accepts the name prefix @@ -65,7 +66,7 @@ public class ShortNamedThreadFactory implements ThreadFactory { * @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable) */ @Override - public Thread newThread(Runnable r) { + public Thread newThread(@NotNull Runnable r) { final Thread t = new Thread(group, r, String.format(Locale.ROOT, "%s-%d", this.threadNamePrefix, threadNumber.getAndIncrement()), 0); t.setDaemon(false);