ArrayListDeque.java

net.minecraft.util.ArrayListDeque

信息

  • 全限定名:net.minecraft.util.ArrayListDeque
  • 类型:public class
  • 包:net.minecraft.util
  • 源码路径:src/main/java/net/minecraft/util/ArrayListDeque.java
  • 起始行号:L14
  • 继承:AbstractList
  • 实现:ListAndDeque
  • 职责:

    TODO

字段/常量

  • MIN_GROWTH

    • 类型: int
    • 修饰符: private static final
    • 源码定位: L15
    • 说明:

      TODO

  • contents

    • 类型: Object[]
    • 修饰符: private
    • 源码定位: L16
    • 说明:

      TODO

  • head

    • 类型: int
    • 修饰符: private
    • 源码定位: L17
    • 说明:

      TODO

  • size

    • 类型: int
    • 修饰符: private
    • 源码定位: L18
    • 说明:

      TODO

内部类/嵌套类型

  • net.minecraft.util.ArrayListDeque.DescendingIterator

    • 类型: class
    • 修饰符: private
    • 源码定位: L289
    • 说明:

      TODO

  • net.minecraft.util.ArrayListDeque.ReversedView

    • 类型: class
    • 修饰符: private
    • 源码定位: L314
    • 说明:

      TODO

构造器

public ArrayListDeque() @ L20

  • 构造器名:ArrayListDeque
  • 源码定位:L20
  • 修饰符:public

参数:

说明:

TODO

public ArrayListDeque(int capacity) @ L24

  • 构造器名:ArrayListDeque
  • 源码定位:L24
  • 修饰符:public

参数:

  • capacity: int

说明:

TODO

方法

下面的方法块按源码顺序生成。

public int size() @ L30

  • 方法名:size
  • 源码定位:L30
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

public int capacity() @ L35

  • 方法名:capacity
  • 源码定位:L35
  • 返回类型:int
  • 修饰符:public

参数:

说明:

TODO

private int getIndex(int index) @ L40

  • 方法名:getIndex
  • 源码定位:L40
  • 返回类型:int
  • 修饰符:private

参数:

  • index: int

说明:

TODO

public T get(int index) @ L44

  • 方法名:get
  • 源码定位:L44
  • 返回类型:T
  • 修饰符:public

参数:

  • index: int

说明:

TODO

private static void verifyIndexInRange(int index, int size) @ L50

  • 方法名:verifyIndexInRange
  • 源码定位:L50
  • 返回类型:void
  • 修饰符:private static

参数:

  • index: int
  • size: int

说明:

TODO

private void verifyIndexInRange(int index) @ L56

  • 方法名:verifyIndexInRange
  • 源码定位:L56
  • 返回类型:void
  • 修饰符:private

参数:

  • index: int

说明:

TODO

private T getInner(int innerIndex) @ L60

  • 方法名:getInner
  • 源码定位:L60
  • 返回类型:T
  • 修饰符:private

参数:

  • innerIndex: int

说明:

TODO

public T set(int index, T element) @ L64

  • 方法名:set
  • 源码定位:L64
  • 返回类型:T
  • 修饰符:public

参数:

  • index: int
  • element: T

说明:

TODO

public void add(int index, T element) @ L74

  • 方法名:add
  • 源码定位:L74
  • 返回类型:void
  • 修饰符:public

参数:

  • index: int
  • element: T

说明:

TODO

private void grow() @ L104

  • 方法名:grow
  • 源码定位:L104
  • 返回类型:void
  • 修饰符:private

参数:

说明:

TODO

public T remove(int index) @ L112

  • 方法名:remove
  • 源码定位:L112
  • 返回类型:T
  • 修饰符:public

参数:

  • index: int

说明:

TODO

public boolean removeIf(Predicate<?super T> filter) @ L135

  • 方法名:removeIf
  • 源码定位:L135
  • 返回类型:boolean
  • 修饰符:public

参数:

  • filter: Predicate<?super T>

说明:

TODO

private void copyCount(Object[] newContents, int count) @ L154

  • 方法名:copyCount
  • 源码定位:L154
  • 返回类型:void
  • 修饰符:private

参数:

  • newContents: Object[]
  • count: int

说明:

TODO

public void replaceAll(UnaryOperator<T> operator) @ L160

  • 方法名:replaceAll
  • 源码定位:L160
  • 返回类型:void
  • 修饰符:public

参数:

  • operator: UnaryOperator

说明:

TODO

public void forEach(Consumer<?super T> action) @ L168

  • 方法名:forEach
  • 源码定位:L168
  • 返回类型:void
  • 修饰符:public

参数:

  • action: Consumer<?super T>

说明:

TODO

public void addFirst(T value) @ L175

  • 方法名:addFirst
  • 源码定位:L175
  • 返回类型:void
  • 修饰符:public

参数:

  • value: T

说明:

TODO

public void addLast(T value) @ L180

  • 方法名:addLast
  • 源码定位:L180
  • 返回类型:void
  • 修饰符:public

参数:

  • value: T

说明:

TODO

public boolean offerFirst(T value) @ L185

  • 方法名:offerFirst
  • 源码定位:L185
  • 返回类型:boolean
  • 修饰符:public

参数:

  • value: T

说明:

TODO

public boolean offerLast(T value) @ L191

  • 方法名:offerLast
  • 源码定位:L191
  • 返回类型:boolean
  • 修饰符:public

参数:

  • value: T

说明:

TODO

public T removeFirst() @ L197

  • 方法名:removeFirst
  • 源码定位:L197
  • 返回类型:T
  • 修饰符:public

参数:

说明:

TODO

public T removeLast() @ L206

  • 方法名:removeLast
  • 源码定位:L206
  • 返回类型:T
  • 修饰符:public

参数:

说明:

TODO

public ListAndDeque<T> reversed() @ L215

  • 方法名:reversed
  • 源码定位:L215
  • 返回类型:ListAndDeque
  • 修饰符:public

参数:

说明:

TODO

public T pollFirst() @ L220

  • 方法名:pollFirst
  • 源码定位:L220
  • 返回类型:T
  • 修饰符:public

参数:

说明:

TODO

public T pollLast() @ L225

  • 方法名:pollLast
  • 源码定位:L225
  • 返回类型:T
  • 修饰符:public

参数:

说明:

TODO

public T getFirst() @ L230

  • 方法名:getFirst
  • 源码定位:L230
  • 返回类型:T
  • 修饰符:public

参数:

说明:

TODO

public T getLast() @ L239

  • 方法名:getLast
  • 源码定位:L239
  • 返回类型:T
  • 修饰符:public

参数:

说明:

TODO

public T peekFirst() @ L248

  • 方法名:peekFirst
  • 源码定位:L248
  • 返回类型:T
  • 修饰符:public

参数:

说明:

TODO

public T peekLast() @ L253

  • 方法名:peekLast
  • 源码定位:L253
  • 返回类型:T
  • 修饰符:public

参数:

说明:

TODO

public boolean removeFirstOccurrence(Object o) @ L258

  • 方法名:removeFirstOccurrence
  • 源码定位:L258
  • 返回类型:boolean
  • 修饰符:public

参数:

  • o: Object

说明:

TODO

public boolean removeLastOccurrence(Object o) @ L271

  • 方法名:removeLastOccurrence
  • 源码定位:L271
  • 返回类型:boolean
  • 修饰符:public

参数:

  • o: Object

说明:

TODO

public Iterator<T> descendingIterator() @ L284

  • 方法名:descendingIterator
  • 源码定位:L284
  • 返回类型:Iterator
  • 修饰符:public

参数:

说明:

TODO

代码

public class ArrayListDeque<T> extends AbstractList<T> implements ListAndDeque<T> {
    private static final int MIN_GROWTH = 1;
    private @Nullable Object[] contents;
    private int head;
    private int size;
 
    public ArrayListDeque() {
        this(1);
    }
 
    public ArrayListDeque(int capacity) {
        this.contents = new Object[capacity];
        this.head = 0;
        this.size = 0;
    }
 
    @Override
    public int size() {
        return this.size;
    }
 
    @VisibleForTesting
    public int capacity() {
        return this.contents.length;
    }
 
    private int getIndex(int index) {
        return (index + this.head) % this.contents.length;
    }
 
    @Override
    public T get(int index) {
        this.verifyIndexInRange(index);
        return this.getInner(this.getIndex(index));
    }
 
    private static void verifyIndexInRange(int index, int size) {
        if (index < 0 || index >= size) {
            throw new IndexOutOfBoundsException(index);
        }
    }
 
    private void verifyIndexInRange(int index) {
        verifyIndexInRange(index, this.size);
    }
 
    private T getInner(int innerIndex) {
        return (T)this.contents[innerIndex];
    }
 
    @Override
    public T set(int index, T element) {
        this.verifyIndexInRange(index);
        Objects.requireNonNull(element);
        int innerIndex = this.getIndex(index);
        T current = this.getInner(innerIndex);
        this.contents[innerIndex] = element;
        return current;
    }
 
    @Override
    public void add(int index, T element) {
        verifyIndexInRange(index, this.size + 1);
        Objects.requireNonNull(element);
        if (this.size == this.contents.length) {
            this.grow();
        }
 
        int internalIndex = this.getIndex(index);
        if (index == this.size) {
            this.contents[internalIndex] = element;
        } else if (index == 0) {
            this.head--;
            if (this.head < 0) {
                this.head = this.head + this.contents.length;
            }
 
            this.contents[this.getIndex(0)] = element;
        } else {
            for (int i = this.size - 1; i >= index; i--) {
                this.contents[this.getIndex(i + 1)] = this.contents[this.getIndex(i)];
            }
 
            this.contents[internalIndex] = element;
        }
 
        this.modCount++;
        this.size++;
    }
 
    private void grow() {
        int newLength = this.contents.length + Math.max(this.contents.length >> 1, 1);
        Object[] newContents = new Object[newLength];
        this.copyCount(newContents, this.size);
        this.head = 0;
        this.contents = newContents;
    }
 
    @Override
    public T remove(int index) {
        this.verifyIndexInRange(index);
        int innerIndex = this.getIndex(index);
        T value = this.getInner(innerIndex);
        if (index == 0) {
            this.contents[innerIndex] = null;
            this.head++;
        } else if (index == this.size - 1) {
            this.contents[innerIndex] = null;
        } else {
            for (int i = index + 1; i < this.size; i++) {
                this.contents[this.getIndex(i - 1)] = this.get(i);
            }
 
            this.contents[this.getIndex(this.size - 1)] = null;
        }
 
        this.modCount++;
        this.size--;
        return value;
    }
 
    @Override
    public boolean removeIf(Predicate<? super T> filter) {
        int removed = 0;
 
        for (int i = 0; i < this.size; i++) {
            T value = this.get(i);
            if (filter.test(value)) {
                removed++;
            } else if (removed != 0) {
                this.contents[this.getIndex(i - removed)] = value;
                this.contents[this.getIndex(i)] = null;
            }
        }
 
        this.modCount += removed;
        this.size -= removed;
        return removed != 0;
    }
 
    private void copyCount(Object[] newContents, int count) {
        for (int i = 0; i < count; i++) {
            newContents[i] = this.get(i);
        }
    }
 
    @Override
    public void replaceAll(UnaryOperator<T> operator) {
        for (int i = 0; i < this.size; i++) {
            int index = this.getIndex(i);
            this.contents[index] = Objects.requireNonNull(operator.apply(this.getInner(i)));
        }
    }
 
    @Override
    public void forEach(Consumer<? super T> action) {
        for (int i = 0; i < this.size; i++) {
            action.accept(this.get(i));
        }
    }
 
    @Override
    public void addFirst(T value) {
        this.add(0, value);
    }
 
    @Override
    public void addLast(T value) {
        this.add(this.size, value);
    }
 
    @Override
    public boolean offerFirst(T value) {
        this.addFirst(value);
        return true;
    }
 
    @Override
    public boolean offerLast(T value) {
        this.addLast(value);
        return true;
    }
 
    @Override
    public T removeFirst() {
        if (this.size == 0) {
            throw new NoSuchElementException();
        } else {
            return this.remove(0);
        }
    }
 
    @Override
    public T removeLast() {
        if (this.size == 0) {
            throw new NoSuchElementException();
        } else {
            return this.remove(this.size - 1);
        }
    }
 
    @Override
    public ListAndDeque<T> reversed() {
        return new ArrayListDeque.ReversedView(this);
    }
 
    @Override
    public @Nullable T pollFirst() {
        return this.size == 0 ? null : this.removeFirst();
    }
 
    @Override
    public @Nullable T pollLast() {
        return this.size == 0 ? null : this.removeLast();
    }
 
    @Override
    public T getFirst() {
        if (this.size == 0) {
            throw new NoSuchElementException();
        } else {
            return this.get(0);
        }
    }
 
    @Override
    public T getLast() {
        if (this.size == 0) {
            throw new NoSuchElementException();
        } else {
            return this.get(this.size - 1);
        }
    }
 
    @Override
    public @Nullable T peekFirst() {
        return this.size == 0 ? null : this.getFirst();
    }
 
    @Override
    public @Nullable T peekLast() {
        return this.size == 0 ? null : this.getLast();
    }
 
    @Override
    public boolean removeFirstOccurrence(Object o) {
        for (int i = 0; i < this.size; i++) {
            T value = this.get(i);
            if (Objects.equals(o, value)) {
                this.remove(i);
                return true;
            }
        }
 
        return false;
    }
 
    @Override
    public boolean removeLastOccurrence(Object o) {
        for (int i = this.size - 1; i >= 0; i--) {
            T value = this.get(i);
            if (Objects.equals(o, value)) {
                this.remove(i);
                return true;
            }
        }
 
        return false;
    }
 
    @Override
    public Iterator<T> descendingIterator() {
        return new ArrayListDeque.DescendingIterator();
    }
 
    private class DescendingIterator implements Iterator<T> {
        private int index;
 
        public DescendingIterator() {
            Objects.requireNonNull(ArrayListDeque.this);
            super();
            this.index = ArrayListDeque.this.size() - 1;
        }
 
        @Override
        public boolean hasNext() {
            return this.index >= 0;
        }
 
        @Override
        public T next() {
            return ArrayListDeque.this.get(this.index--);
        }
 
        @Override
        public void remove() {
            ArrayListDeque.this.remove(this.index + 1);
        }
    }
 
    private class ReversedView extends AbstractList<T> implements ListAndDeque<T> {
        private final ArrayListDeque<T> source;
 
        public ReversedView(ArrayListDeque<T> source) {
            Objects.requireNonNull(ArrayListDeque.this);
            super();
            this.source = source;
        }
 
        @Override
        public ListAndDeque<T> reversed() {
            return this.source;
        }
 
        @Override
        public T getFirst() {
            return this.source.getLast();
        }
 
        @Override
        public T getLast() {
            return this.source.getFirst();
        }
 
        @Override
        public void addFirst(T t) {
            this.source.addLast(t);
        }
 
        @Override
        public void addLast(T t) {
            this.source.addFirst(t);
        }
 
        @Override
        public boolean offerFirst(T t) {
            return this.source.offerLast(t);
        }
 
        @Override
        public boolean offerLast(T t) {
            return this.source.offerFirst(t);
        }
 
        @Override
        public @Nullable T pollFirst() {
            return this.source.pollLast();
        }
 
        @Override
        public @Nullable T pollLast() {
            return this.source.pollFirst();
        }
 
        @Override
        public @Nullable T peekFirst() {
            return this.source.peekLast();
        }
 
        @Override
        public @Nullable T peekLast() {
            return this.source.peekFirst();
        }
 
        @Override
        public T removeFirst() {
            return this.source.removeLast();
        }
 
        @Override
        public T removeLast() {
            return this.source.removeFirst();
        }
 
        @Override
        public boolean removeFirstOccurrence(Object o) {
            return this.source.removeLastOccurrence(o);
        }
 
        @Override
        public boolean removeLastOccurrence(Object o) {
            return this.source.removeFirstOccurrence(o);
        }
 
        @Override
        public Iterator<T> descendingIterator() {
            return this.source.iterator();
        }
 
        @Override
        public int size() {
            return this.source.size();
        }
 
        @Override
        public boolean isEmpty() {
            return this.source.isEmpty();
        }
 
        @Override
        public boolean contains(Object o) {
            return this.source.contains(o);
        }
 
        @Override
        public T get(int index) {
            return this.source.get(this.reverseIndex(index));
        }
 
        @Override
        public T set(int index, T element) {
            return this.source.set(this.reverseIndex(index), element);
        }
 
        @Override
        public void add(int index, T element) {
            this.source.add(this.reverseIndex(index) + 1, element);
        }
 
        @Override
        public T remove(int index) {
            return this.source.remove(this.reverseIndex(index));
        }
 
        @Override
        public int indexOf(Object o) {
            return this.reverseIndex(this.source.lastIndexOf(o));
        }
 
        @Override
        public int lastIndexOf(Object o) {
            return this.reverseIndex(this.source.indexOf(o));
        }
 
        @Override
        public List<T> subList(int fromIndex, int toIndex) {
            return this.source.subList(this.reverseIndex(toIndex) + 1, this.reverseIndex(fromIndex) + 1).reversed();
        }
 
        @Override
        public Iterator<T> iterator() {
            return this.source.descendingIterator();
        }
 
        @Override
        public void clear() {
            this.source.clear();
        }
 
        private int reverseIndex(int index) {
            return index == -1 ? -1 : this.source.size() - 1 - index;
        }
    }
}

引用的其他类