Annotate NotNull and Nullable

This commit is contained in:
Andrea Cavalli 2021-09-22 18:26:16 +02:00
parent aa661c936c
commit e4ba8dcd68
12 changed files with 59 additions and 52 deletions

View File

@ -3,6 +3,7 @@ package org.warp.commonutils.stream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import org.jetbrains.annotations.NotNull;
/** /**
* Simple {@link InputStream} implementation that exposes currently * 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; } public int read() throws IOException { return _b.hasRemaining() ? (_b.get() & 0xFF) : -1; }
@Override @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; if (!_b.hasRemaining()) return -1;
len = Math.min(len, _b.remaining()); len = Math.min(len, _b.remaining());
_b.get(bytes, off, len); _b.get(bytes, off, len);

View File

@ -26,12 +26,12 @@ public class DataInputOutputImpl implements DataInputOutput {
} }
@Override @Override
public void readFully(byte[] bytes) throws IOException { public void readFully(byte @NotNull [] bytes) throws IOException {
in.readFully(bytes); in.readFully(bytes);
} }
@Override @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); in.readFully(bytes, i, i1);
} }
@ -107,12 +107,12 @@ public class DataInputOutputImpl implements DataInputOutput {
} }
@Override @Override
public void write(byte[] bytes) throws IOException { public void write(byte @NotNull [] bytes) throws IOException {
out.write(bytes); out.write(bytes);
} }
@Override @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); out.write(bytes, i, i1);
} }

View File

@ -25,12 +25,12 @@ public class DataInputOutputStream extends DataOutputStream implements DataInput
} }
@Override @Override
public void readFully(byte[] bytes) throws IOException { public void readFully(byte @NotNull [] bytes) throws IOException {
in.readFully(bytes); in.readFully(bytes);
} }
@Override @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); in.readFully(bytes, i, i1);
} }

View File

@ -26,6 +26,7 @@
package org.warp.commonutils.stream; package org.warp.commonutils.stream;
import java.io.DataInput; import java.io.DataInput;
import org.jetbrains.annotations.NotNull;
/** /**
* A data input stream lets an application read primitive Java data * 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 * working arrays initialized on demand by readUTF
*/ */
private byte bytearr[] = new byte[80]; private byte[] bytearr = new byte[80];
private char chararr[] = new char[80]; private char[] chararr = new char[80];
/** /**
* Reads some number of bytes from the contained input stream and * 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 SafeFilterInputStream#in
* @see java.io.InputStream#read(byte[], int, int) * @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); return in.read(b, 0, b.length);
} }
@ -138,7 +139,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn
* @see SafeFilterInputStream#in * @see SafeFilterInputStream#in
* @see java.io.InputStream#read(byte[], int, int) * @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); 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}. * @throws NullPointerException if {@code b} is {@code null}.
* @see SafeFilterInputStream#in * @see SafeFilterInputStream#in
*/ */
public final void readFully(byte b[]) { public final void readFully(byte @NotNull [] b) {
readFully(b, 0, b.length); readFully(b, 0, b.length);
} }
@ -175,7 +176,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn
* {@code b.length - off}. * {@code b.length - off}.
* @see SafeFilterInputStream#in * @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) if (len < 0)
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
int n = 0; int n = 0;
@ -199,7 +200,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn
*/ */
public final int skipBytes(int n) { public final int skipBytes(int n) {
int total = 0; int total = 0;
int cur = 0; int cur;
while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) { while ((total<n) && ((cur = (int) in.skip(n-total)) > 0)) {
total += cur; total += cur;
@ -345,7 +346,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn
return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); 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} * See the general contract of the {@code readLong}
@ -405,7 +406,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn
return Double.longBitsToDouble(readLong()); return Double.longBitsToDouble(readLong());
} }
private char lineBuffer[]; private char[] lineBuffer;
/** /**
* See the general contract of the {@code readLine} * See the general contract of the {@code readLine}
@ -435,7 +436,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn
*/ */
@Deprecated @Deprecated
public final String readLine() { public final String readLine() {
char buf[] = lineBuffer; char[] buf = lineBuffer;
if (buf == null) { if (buf == null) {
buf = lineBuffer = new char[128]; buf = lineBuffer = new char[128];
@ -489,7 +490,7 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn
* @return a Unicode string. * @return a Unicode string.
* @see SafeDataInputStream#readUTF(SafeDataInputStream) * @see SafeDataInputStream#readUTF(SafeDataInputStream)
*/ */
public final String readUTF() { public final @NotNull String readUTF() {
return readUTF(this); return readUTF(this);
} }
@ -507,17 +508,16 @@ public class SafeDataInputStream extends SafeFilterInputStream implements DataIn
* @return a Unicode string. * @return a Unicode string.
* @see SafeDataInputStream#readUnsignedShort() * @see SafeDataInputStream#readUnsignedShort()
*/ */
public static final String readUTF(SafeDataInputStream in) { public static String readUTF(SafeDataInputStream in) {
int utflen = in.readUnsignedShort(); int utflen = in.readUnsignedShort();
byte[] bytearr = null; byte[] bytearr;
char[] chararr = null; char[] chararr;
SafeDataInputStream dis = in; if (in.bytearr.length < utflen){
if (dis.bytearr.length < utflen){ in.bytearr = new byte[utflen*2];
dis.bytearr = new byte[utflen*2]; in.chararr = new char[utflen*2];
dis.chararr = new char[utflen*2];
} }
chararr = dis.chararr; chararr = in.chararr;
bytearr = dis.bytearr; bytearr = in.bytearr;
int c, char2, char3; int c, char2, char3;
int count = 0; int count = 0;

View File

@ -4,6 +4,7 @@ import java.io.Closeable;
import java.io.Flushable; import java.io.Flushable;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.Objects; import java.util.Objects;
import org.jetbrains.annotations.NotNull;
/** /**
* This abstract class is the superclass of all classes representing * This abstract class is the superclass of all classes representing
@ -62,7 +63,7 @@ public abstract class SafeOutputStream extends OutputStream implements Closeable
} }
@Override @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); Objects.checkFromIndexSize(off, len, b.length);
ensureOpen(); ensureOpen();
} }
@ -97,7 +98,7 @@ public abstract class SafeOutputStream extends OutputStream implements Closeable
* @param b the data. * @param b the data.
* @see java.io.OutputStream#write(byte[], int, int) * @see java.io.OutputStream#write(byte[], int, int)
*/ */
public void write(byte b[]) { public void write(byte @NotNull [] b) {
write(b, 0, b.length); 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 off the start offset in the data.
* @param len the number of bytes to write. * @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); Objects.checkFromIndexSize(off, len, b.length);
// len == 0 condition implicitly handled by loop bounds // len == 0 condition implicitly handled by loop bounds
for (int i = 0 ; i < len ; i++) { for (int i = 0 ; i < len ; i++) {

View File

@ -13,7 +13,7 @@ import org.jetbrains.annotations.NotNull;
public class Bytes { public class Bytes {
public final byte[] data; public final byte[] data;
public Bytes(@NotNull byte[] data) { public Bytes(byte @NotNull[] data) {
this.data = data; this.data = data;
} }

View File

@ -48,13 +48,13 @@ public class FastUtilStackSetWrapper<T> implements StackSet<T>, Collection<T> {
@NotNull @NotNull
@Override @Override
public Object[] toArray() { public Object @NotNull [] toArray() {
return linkedHashSet.toArray(); return linkedHashSet.toArray();
} }
@NotNull @NotNull
@Override @Override
public <T1> T1[] toArray(@NotNull T1[] a) { public <T1> T1 @NotNull [] toArray(@NotNull T1 @NotNull [] a) {
//noinspection SuspiciousToArrayCall //noinspection SuspiciousToArrayCall
return linkedHashSet.toArray(a); return linkedHashSet.toArray(a);
} }

View File

@ -49,13 +49,13 @@ public class FastUtilStackWrapper<T> implements Stack<T>, Collection<T> {
@NotNull @NotNull
@Override @Override
public Object[] toArray() { public Object @NotNull [] toArray() {
return collection.toArray(); return collection.toArray();
} }
@NotNull @NotNull
@Override @Override
public <T1> T1[] toArray(@NotNull T1[] a) { public <T1> T1 @NotNull [] toArray(@NotNull T1 @NotNull [] a) {
//noinspection SuspiciousToArrayCall //noinspection SuspiciousToArrayCall
return collection.toArray(a); return collection.toArray(a);
} }

View File

@ -18,7 +18,7 @@ public class FloatPriorityQueue<T> implements Queue<T> {
private final Queue<ScoredValue<T>> internalQueue; private final Queue<ScoredValue<T>> internalQueue;
public static <T> FloatPriorityQueue<T> of() { public static <T> FloatPriorityQueue<T> of() {
return new FloatPriorityQueue<T>(0); return new FloatPriorityQueue<>(0);
} }
public static <T> FloatPriorityQueue<T> of(T value, float score) { public static <T> FloatPriorityQueue<T> of(T value, float score) {
@ -33,6 +33,7 @@ public class FloatPriorityQueue<T> implements Queue<T> {
return pq; return pq;
} }
@SafeVarargs
public static <T> FloatPriorityQueue<T> of(ScoredValue<T>... values) { public static <T> FloatPriorityQueue<T> of(ScoredValue<T>... values) {
var pq = new FloatPriorityQueue<T>(values.length); var pq = new FloatPriorityQueue<T>(values.length);
for (ScoredValue<T> value : values) { for (ScoredValue<T> value : values) {
@ -64,12 +65,12 @@ public class FloatPriorityQueue<T> implements Queue<T> {
} }
public static <T> FloatPriorityQueue<T> synchronize(FloatPriorityQueue<T> queue) { public static <T> FloatPriorityQueue<T> synchronize(FloatPriorityQueue<T> queue) {
return new SynchronizedFloatPriorityQueue<T>(queue.contentValues, queue.internalQueue); return new SynchronizedFloatPriorityQueue<>(queue.contentValues, queue.internalQueue);
} }
public static <T> FloatPriorityQueue<T> synchronizedPq(int initialCapacity) { public static <T> FloatPriorityQueue<T> synchronizedPq(int initialCapacity) {
var pq = new FloatPriorityQueue<T>(initialCapacity); var pq = new FloatPriorityQueue<T>(initialCapacity);
return new SynchronizedFloatPriorityQueue<T>(pq.contentValues, pq.internalQueue); return new SynchronizedFloatPriorityQueue<>(pq.contentValues, pq.internalQueue);
} }
@Override @Override
@ -95,7 +96,7 @@ public class FloatPriorityQueue<T> implements Queue<T> {
public Iterator<T> iterator() { public Iterator<T> iterator() {
assert contentValues.size() == internalQueue.size(); assert contentValues.size() == internalQueue.size();
var it = internalQueue.iterator(); var it = internalQueue.iterator();
return new Iterator<T>() { return new Iterator<>() {
@Override @Override
public boolean hasNext() { public boolean hasNext() {
assert contentValues.size() == internalQueue.size(); assert contentValues.size() == internalQueue.size();
@ -120,14 +121,15 @@ public class FloatPriorityQueue<T> implements Queue<T> {
@NotNull @NotNull
@Override @Override
public Object[] toArray() { public Object @NotNull [] toArray() {
assert contentValues.size() == internalQueue.size(); assert contentValues.size() == internalQueue.size();
return internalQueue.stream().map(FloatPriorityQueue::getValueOrNull).toArray(Object[]::new); return internalQueue.stream().map(FloatPriorityQueue::getValueOrNull).toArray(Object[]::new);
} }
@SuppressWarnings({"SuspiciousToArrayCall", "unchecked"})
@NotNull @NotNull
@Override @Override
public <T1> T1[] toArray(@NotNull T1[] a) { public <T1> T1 @NotNull [] toArray(@NotNull T1 @NotNull [] a) {
assert contentValues.size() == internalQueue.size(); assert contentValues.size() == internalQueue.size();
return internalQueue return internalQueue
.stream() .stream()
@ -286,7 +288,7 @@ public class FloatPriorityQueue<T> implements Queue<T> {
} }
public <U extends T> FloatPriorityQueueView<U> view() { public <U extends T> FloatPriorityQueueView<U> view() {
return new FloatPriorityQueueView<U>(this); return new FloatPriorityQueueView<>(this);
} }
private static class SynchronizedFloatPriorityQueue<T> extends FloatPriorityQueue<T> { private static class SynchronizedFloatPriorityQueue<T> extends FloatPriorityQueue<T> {
@ -313,7 +315,7 @@ public class FloatPriorityQueue<T> implements Queue<T> {
@Override @Override
public synchronized @NotNull Iterator<T> iterator() { public synchronized @NotNull Iterator<T> iterator() {
var it = super.iterator(); var it = super.iterator();
return new Iterator<T>() { return new Iterator<>() {
@Override @Override
public boolean hasNext() { public boolean hasNext() {
synchronized (SynchronizedFloatPriorityQueue.this) { synchronized (SynchronizedFloatPriorityQueue.this) {
@ -331,12 +333,13 @@ public class FloatPriorityQueue<T> implements Queue<T> {
} }
@Override @Override
public synchronized @NotNull Object[] toArray() { public synchronized @NotNull Object @NotNull [] toArray() {
return super.toArray(); return super.toArray();
} }
@SuppressWarnings("SuspiciousToArrayCall")
@Override @Override
public synchronized <T1> @NotNull T1[] toArray(@NotNull T1[] a) { public synchronized <T1> @NotNull T1 @NotNull [] toArray(@NotNull T1 @NotNull [] a) {
return super.toArray(a); return super.toArray(a);
} }
@ -356,6 +359,7 @@ public class FloatPriorityQueue<T> implements Queue<T> {
return super.addTop(t); return super.addTop(t);
} }
@Deprecated
@Override @Override
public synchronized boolean add(T t) { public synchronized boolean add(T t) {
return super.add(t); return super.add(t);

View File

@ -65,13 +65,13 @@ public class FloatPriorityQueueView<T> implements Queue<T> {
@NotNull @NotNull
@Override @Override
public Object[] toArray() { public Object @NotNull [] toArray() {
return queue.toArray(); return queue.toArray();
} }
@NotNull @NotNull
@Override @Override
public <T1> T1[] toArray(@NotNull T1[] a) { public <T1> T1 @NotNull [] toArray(@NotNull T1 @NotNull [] a) {
//noinspection SuspiciousToArrayCall //noinspection SuspiciousToArrayCall
return queue.toArray(a); return queue.toArray(a);
} }

View File

@ -38,7 +38,7 @@ public class ImmutableLinkedSet<E> implements Set<E> {
@Override @Override
public Iterator<E> iterator() { public Iterator<E> iterator() {
var it = set.iterator(); var it = set.iterator();
return new Iterator<E>() { return new Iterator<>() {
@Override @Override
public boolean hasNext() { public boolean hasNext() {
return it.hasNext(); return it.hasNext();
@ -58,13 +58,13 @@ public class ImmutableLinkedSet<E> implements Set<E> {
@NotNull @NotNull
@Override @Override
public Object[] toArray() { public Object @NotNull [] toArray() {
return set.toArray(); return set.toArray();
} }
@NotNull @NotNull
@Override @Override
public <T> T[] toArray(@NotNull T[] a) { public <T> T @NotNull [] toArray(@NotNull T @NotNull [] a) {
//noinspection SuspiciousToArrayCall //noinspection SuspiciousToArrayCall
return set.toArray(a); return set.toArray(a);
} }
@ -118,7 +118,7 @@ public class ImmutableLinkedSet<E> implements Set<E> {
@Override @Override
public Spliterator<E> spliterator() { public Spliterator<E> spliterator() {
var spl = set.spliterator(); var spl = set.spliterator();
return new Spliterator<E>() { return new Spliterator<>() {
@Override @Override
public boolean tryAdvance(Consumer<? super E> action) { public boolean tryAdvance(Consumer<? super E> action) {
return spl.tryAdvance(action); return spl.tryAdvance(action);

View File

@ -21,6 +21,7 @@ import java.util.Locale;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import org.jetbrains.annotations.NotNull;
/** /**
* A default {@link ThreadFactory} implementation that accepts the name prefix * 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) * @see java.util.concurrent.ThreadFactory#newThread(java.lang.Runnable)
*/ */
@Override @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", final Thread t = new Thread(group, r, String.format(Locale.ROOT, "%s-%d",
this.threadNamePrefix, threadNumber.getAndIncrement()), 0); this.threadNamePrefix, threadNumber.getAndIncrement()), 0);
t.setDaemon(false); t.setDaemon(false);