Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.NoSuchElementException;
- /**
- * @author I_Support_KKK
- * @since 1.8
- * @version 1.1
- *
- * StrictArray is a high-performance, compact, non-duplicate array
- * utility which, as its name implies, is strict on its range which
- * needs to be supplied. To allocate a new StrictArray, the method
- * "newStrictArray" will need to be invoked.
- */
- public final class StrictArray<T> {
- private final Object[] array;
- private int current;
- private StrictArray(final int size) {
- if (size <= 0) {
- throw new IllegalArgumentException("invalid size");
- }
- this.array = new Object[size];
- }
- public static <T> StrictArray<T> newStrictArray(final int size) {
- return new StrictArray<>(size);
- }
- public void add(final T obj) {
- if (this.contains(obj)) {
- return;
- }
- if (this.current > this.array.length) {
- throw new ArrayStoreException("array is full");
- }
- this.array[this.current++] = obj;
- }
- public void remove(final T obj) {
- this.array[this.indexOf(obj)] = null;
- }
- @SuppressWarnings("unchecked")
- public T get(final int index) {
- return (T) this.array[index];
- }
- public int indexOf(final T obj) {
- if (!this.contains(obj)) {
- throw new NoSuchElementException(String.format("element '%s' doesn't exist", obj.toString()));
- }
- for (int index = 0; index < this.array.length; index++) {
- if (this.array[index] == obj) {
- return index;
- }
- }
- return -1;
- }
- public boolean contains(final T obj) {
- return this.indexOf(obj) >= 0;
- }
- public boolean isEmpty() {
- return this.array.length == 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement