From 8b0cd9e36bdaabdc3a5378eba3d600a546737186 Mon Sep 17 00:00:00 2001 From: zzz Date: Sun, 29 Jan 2023 13:27:51 -0500 Subject: [PATCH] Util: Add ArraySet.get() and new constructor --- core/java/src/net/i2p/util/ArraySet.java | 31 ++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/core/java/src/net/i2p/util/ArraySet.java b/core/java/src/net/i2p/util/ArraySet.java index 3ddab5afd..f3c26af31 100644 --- a/core/java/src/net/i2p/util/ArraySet.java +++ b/core/java/src/net/i2p/util/ArraySet.java @@ -85,6 +85,26 @@ public class ArraySet extends AbstractSet implements Set { addAll(c); } + + /** + * A fixed capacity of arr.length. + * Adds over capacity will throw a SetFullException. + * arr must not contain duplicates, no checks are done. + * arr may contain nulls but they must be at the end. + * + * @since 0.9.58 + */ + public ArraySet(E[] arr) { + _entries = arr; + int i; + for (i = 0; i < arr.length; i++) { + if (arr[i] == null) + break; + } + _size = i; + _throwOnFull = true; + } + /** * Adds over capacity will throw a SetFullException. * @@ -209,6 +229,17 @@ public class ArraySet extends AbstractSet implements Set { return _size; } + /** + * @throws IndexOutOfBoundsException + * @since 0.9.58 + */ + @SuppressWarnings("unchecked") + public E get(int index) { + if (index < 0 || index > _size - 1) + throw new IndexOutOfBoundsException(); + return (E) _entries[index]; + } + /** * Supports remove. * Supports comodification checks.