SortedArraySet.java

net.minecraft.util.SortedArraySet

信息

  • 全限定名:net.minecraft.util.SortedArraySet
  • 类型:public class
  • 包:net.minecraft.util
  • 源码路径:src/main/java/net/minecraft/util/SortedArraySet.java
  • 起始行号:L12
  • 继承:AbstractSet
  • 职责:

    TODO

字段/常量

  • DEFAULT_INITIAL_CAPACITY

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

      TODO

  • comparator

    • 类型: Comparator<T>
    • 修饰符: private final
    • 源码定位: L14
    • 说明:

      TODO

  • contents

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

      TODO

  • size

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

      TODO

内部类/嵌套类型

  • net.minecraft.util.SortedArraySet.ArrayIterator
    • 类型: class
    • 修饰符: private
    • 源码定位: L190
    • 说明:

      TODO

构造器

private SortedArraySet(int initialCapacity, Comparator<T> comparator) @ L18

  • 构造器名:SortedArraySet
  • 源码定位:L18
  • 修饰符:private

参数:

  • initialCapacity: int
  • comparator: Comparator

说明:

TODO

方法

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

public static <T extends Comparable<T>> SortedArraySet<T> create() @ L27

  • 方法名:create
  • 源码定位:L27
  • 返回类型:<T extends Comparable> SortedArraySet
  • 修饰符:public static

参数:

说明:

TODO

public static <T extends Comparable<T>> SortedArraySet<T> create(int initialCapacity) @ L31

  • 方法名:create
  • 源码定位:L31
  • 返回类型:<T extends Comparable> SortedArraySet
  • 修饰符:public static

参数:

  • initialCapacity: int

说明:

TODO

public static <T> SortedArraySet<T> create(Comparator<T> comparator) @ L35

  • 方法名:create
  • 源码定位:L35
  • 返回类型: SortedArraySet
  • 修饰符:public static

参数:

  • comparator: Comparator

说明:

TODO

public static <T> SortedArraySet<T> create(Comparator<T> comparator, int initialCapacity) @ L39

  • 方法名:create
  • 源码定位:L39
  • 返回类型: SortedArraySet
  • 修饰符:public static

参数:

  • comparator: Comparator
  • initialCapacity: int

说明:

TODO

private static <T> T[] castRawArray(Object[] array) @ L43

  • 方法名:castRawArray
  • 源码定位:L43
  • 返回类型: T[]
  • 修饰符:private static

参数:

  • array: Object[]

说明:

TODO

private int findIndex(T t) @ L47

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

参数:

  • t: T

说明:

TODO

private static int getInsertionPosition(int position) @ L51

  • 方法名:getInsertionPosition
  • 源码定位:L51
  • 返回类型:int
  • 修饰符:private static

参数:

  • position: int

说明:

TODO

public boolean add(T t) @ L55

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

参数:

  • t: T

说明:

TODO

private void grow(int capacity) @ L67

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

参数:

  • capacity: int

说明:

TODO

private void addInternal(T t, int pos) @ L81

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

参数:

  • t: T
  • pos: int

说明:

TODO

private void removeInternal(int position) @ L91

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

参数:

  • position: int

说明:

TODO

private T getInternal(int position) @ L100

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

参数:

  • position: int

说明:

TODO

public T addOrGet(T t) @ L104

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

参数:

  • t: T

说明:

TODO

public boolean remove(Object o) @ L114

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

参数:

  • o: Object

说明:

TODO

public T get(T t) @ L125

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

参数:

  • t: T

说明:

TODO

public T first() @ L130

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

参数:

说明:

TODO

public T last() @ L134

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

参数:

说明:

TODO

public boolean contains(Object o) @ L138

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

参数:

  • o: Object

说明:

TODO

public Iterator<T> iterator() @ L144

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

参数:

说明:

TODO

public int size() @ L149

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

参数:

说明:

TODO

public Object[] toArray() @ L154

  • 方法名:toArray
  • 源码定位:L154
  • 返回类型:Object[]
  • 修饰符:public

参数:

说明:

TODO

public <U> U[] toArray(U[] a) @ L159

  • 方法名:toArray
  • 源码定位:L159
  • 返回类型: U[]
  • 修饰符:public

参数:

  • a: U[]

说明:

TODO

public void clear() @ L173

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

参数:

说明:

TODO

public boolean equals(Object o) @ L179

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

参数:

  • o: Object

说明:

TODO

代码

public class SortedArraySet<T> extends AbstractSet<T> {
    private static final int DEFAULT_INITIAL_CAPACITY = 10;
    private final Comparator<T> comparator;
    private T[] contents;
    private int size;
 
    private SortedArraySet(int initialCapacity, Comparator<T> comparator) {
        this.comparator = comparator;
        if (initialCapacity < 0) {
            throw new IllegalArgumentException("Initial capacity (" + initialCapacity + ") is negative");
        } else {
            this.contents = (T[])castRawArray(new Object[initialCapacity]);
        }
    }
 
    public static <T extends Comparable<T>> SortedArraySet<T> create() {
        return create(10);
    }
 
    public static <T extends Comparable<T>> SortedArraySet<T> create(int initialCapacity) {
        return new SortedArraySet<>(initialCapacity, Comparator.<T>naturalOrder());
    }
 
    public static <T> SortedArraySet<T> create(Comparator<T> comparator) {
        return create(comparator, 10);
    }
 
    public static <T> SortedArraySet<T> create(Comparator<T> comparator, int initialCapacity) {
        return new SortedArraySet<>(initialCapacity, comparator);
    }
 
    private static <T> T[] castRawArray(Object[] array) {
        return (T[])array;
    }
 
    private int findIndex(T t) {
        return Arrays.binarySearch(this.contents, 0, this.size, t, this.comparator);
    }
 
    private static int getInsertionPosition(int position) {
        return -position - 1;
    }
 
    @Override
    public boolean add(T t) {
        int position = this.findIndex(t);
        if (position >= 0) {
            return false;
        } else {
            int pos = getInsertionPosition(position);
            this.addInternal(t, pos);
            return true;
        }
    }
 
    private void grow(int capacity) {
        if (capacity > this.contents.length) {
            if (this.contents != ObjectArrays.DEFAULT_EMPTY_ARRAY) {
                capacity = Util.growByHalf(this.contents.length, capacity);
            } else if (capacity < 10) {
                capacity = 10;
            }
 
            Object[] t = new Object[capacity];
            System.arraycopy(this.contents, 0, t, 0, this.size);
            this.contents = (T[])castRawArray(t);
        }
    }
 
    private void addInternal(T t, int pos) {
        this.grow(this.size + 1);
        if (pos != this.size) {
            System.arraycopy(this.contents, pos, this.contents, pos + 1, this.size - pos);
        }
 
        this.contents[pos] = t;
        this.size++;
    }
 
    private void removeInternal(int position) {
        this.size--;
        if (position != this.size) {
            System.arraycopy(this.contents, position + 1, this.contents, position, this.size - position);
        }
 
        this.contents[this.size] = null;
    }
 
    private T getInternal(int position) {
        return this.contents[position];
    }
 
    public T addOrGet(T t) {
        int position = this.findIndex(t);
        if (position >= 0) {
            return this.getInternal(position);
        } else {
            this.addInternal(t, getInsertionPosition(position));
            return t;
        }
    }
 
    @Override
    public boolean remove(Object o) {
        int position = this.findIndex((T)o);
        if (position >= 0) {
            this.removeInternal(position);
            return true;
        } else {
            return false;
        }
    }
 
    public @Nullable T get(T t) {
        int position = this.findIndex(t);
        return position >= 0 ? this.getInternal(position) : null;
    }
 
    public T first() {
        return this.getInternal(0);
    }
 
    public T last() {
        return this.getInternal(this.size - 1);
    }
 
    @Override
    public boolean contains(Object o) {
        int result = this.findIndex((T)o);
        return result >= 0;
    }
 
    @Override
    public Iterator<T> iterator() {
        return new SortedArraySet.ArrayIterator();
    }
 
    @Override
    public int size() {
        return this.size;
    }
 
    @Override
    public Object[] toArray() {
        return Arrays.copyOf(this.contents, this.size, Object[].class);
    }
 
    @Override
    public <U> U[] toArray(U[] a) {
        if (a.length < this.size) {
            return (U[])Arrays.copyOf(this.contents, this.size, (Class<? extends T[]>)a.getClass());
        } else {
            System.arraycopy(this.contents, 0, a, 0, this.size);
            if (a.length > this.size) {
                a[this.size] = null;
            }
 
            return a;
        }
    }
 
    @Override
    public void clear() {
        Arrays.fill(this.contents, 0, this.size, null);
        this.size = 0;
    }
 
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        } else {
            return o instanceof SortedArraySet<?> that && this.comparator.equals(that.comparator)
                ? this.size == that.size && Arrays.equals(this.contents, that.contents)
                : super.equals(o);
        }
    }
 
    private class ArrayIterator implements Iterator<T> {
        private int index;
        private int last;
 
        private ArrayIterator() {
            Objects.requireNonNull(SortedArraySet.this);
            super();
            this.last = -1;
        }
 
        @Override
        public boolean hasNext() {
            return this.index < SortedArraySet.this.size;
        }
 
        @Override
        public T next() {
            if (this.index >= SortedArraySet.this.size) {
                throw new NoSuchElementException();
            } else {
                this.last = this.index++;
                return SortedArraySet.this.contents[this.last];
            }
        }
 
        @Override
        public void remove() {
            if (this.last == -1) {
                throw new IllegalStateException();
            } else {
                SortedArraySet.this.removeInternal(this.last);
                this.index--;
                this.last = -1;
            }
        }
    }
}

引用的其他类

  • LoggedChatMessage

    • 引用位置: 方法调用
    • 关联成员: System.arraycopy()
  • Util

    • 引用位置: 方法调用
    • 关联成员: Util.growByHalf()