Class BitList<E>

java.lang.Object
java.util.AbstractCollection<E>
java.util.AbstractList<E>
org.apache.dubbo.rpc.cluster.router.state.BitList<E>
Type Parameters:
E -
All Implemented Interfaces:
Cloneable, Iterable<E>, Collection<E>, List<E>, SequencedCollection<E>

public class BitList<E> extends AbstractList<E> implements Cloneable
BitList based on BitMap implementation. BitList is consists of `originList`, `rootSet` and `tailList`.

originList: Initial elements of the list. This list will not be changed in modification actions (expect clear all). rootSet: A bitMap to store the indexes of originList are still exist. Most of the modification actions are operated on this bitMap. tailList: An additional list for BitList. Worked when adding totally new elements to list. These elements will be appended to the last of the BitList.

An example of BitList: originList: A B C D E (5 elements) rootSet: x v x v v 0 1 0 1 1 (5 elements) tailList: F G H (3 elements) resultList: B D E F G H (6 elements)

Since:
3.0
  • Constructor Details

    • BitList

      public BitList(List<E> originList)
    • BitList

      public BitList(List<E> originList, boolean empty)
    • BitList

      public BitList(List<E> originList, boolean empty, List<E> tailList)
    • BitList

      public BitList(List<E> originList, BitSet rootSet, List<E> tailList)
  • Method Details

    • getOriginList

      public List<E> getOriginList()
    • addIndex

      public void addIndex(int index)
    • totalSetSize

      public int totalSetSize()
    • indexExist

      public boolean indexExist(int index)
    • getByIndex

      public E getByIndex(int index)
    • and

      public BitList<E> and(BitList<E> target)
      And operation between two bitList. Return a new cloned list. TailList in source bitList will be totally saved even if it is not appeared in the target bitList.
      Parameters:
      target - target bitList
      Returns:
      this bitList only contains those elements contain in both two list and source bitList's tailList
    • or

      public BitList<E> or(BitList<E> target)
    • hasMoreElementInTailList

      public boolean hasMoreElementInTailList()
    • getTailList

      public List<E> getTailList()
    • addToTailList

      public void addToTailList(E e)
    • randomSelectOne

      public E randomSelectOne()
    • emptyList

      public static <T> BitList<T> emptyList()
    • size

      public int size()
      Specified by:
      size in interface Collection<E>
      Specified by:
      size in interface List<E>
      Specified by:
      size in class AbstractCollection<E>
    • contains

      public boolean contains(Object o)
      Specified by:
      contains in interface Collection<E>
      Specified by:
      contains in interface List<E>
      Overrides:
      contains in class AbstractCollection<E>
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Collection<E>
      Specified by:
      iterator in interface Iterable<E>
      Specified by:
      iterator in interface List<E>
      Overrides:
      iterator in class AbstractList<E>
    • add

      public boolean add(E e)
      If the element to added is appeared in originList even if it is not in rootSet, directly set its index in rootSet to true. (This may change the order of elements.)

      If the element is not contained in originList, allocate tailList and add to tailList.

      Notice: It is not recommended adding duplicated element.

      Specified by:
      add in interface Collection<E>
      Specified by:
      add in interface List<E>
      Overrides:
      add in class AbstractList<E>
    • remove

      public boolean remove(Object o)
      If the element to added is appeared in originList, directly set its index in rootSet to false. (This may change the order of elements.)

      If the element is not contained in originList, try to remove from tailList.

      Specified by:
      remove in interface Collection<E>
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class AbstractCollection<E>
    • clear

      public void clear()
      Caution: This operation will clear originList for removing references purpose. This may change the default behaviour when adding new element later.
      Specified by:
      clear in interface Collection<E>
      Specified by:
      clear in interface List<E>
      Overrides:
      clear in class AbstractList<E>
    • get

      public E get(int index)
      Specified by:
      get in interface List<E>
      Specified by:
      get in class AbstractList<E>
    • remove

      public E remove(int index)
      Specified by:
      remove in interface List<E>
      Overrides:
      remove in class AbstractList<E>
    • indexOf

      public int indexOf(Object o)
      Specified by:
      indexOf in interface List<E>
      Overrides:
      indexOf in class AbstractList<E>
    • addAll

      public boolean addAll(Collection<? extends E> c)
      Specified by:
      addAll in interface Collection<E>
      Specified by:
      addAll in interface List<E>
      Overrides:
      addAll in class AbstractCollection<E>
    • lastIndexOf

      public int lastIndexOf(Object o)
      Specified by:
      lastIndexOf in interface List<E>
      Overrides:
      lastIndexOf in class AbstractList<E>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Collection<E>
      Specified by:
      isEmpty in interface List<E>
      Overrides:
      isEmpty in class AbstractCollection<E>
    • listIterator

      public ListIterator<E> listIterator()
      Specified by:
      listIterator in interface List<E>
      Overrides:
      listIterator in class AbstractList<E>
    • listIterator

      public ListIterator<E> listIterator(int index)
      Specified by:
      listIterator in interface List<E>
      Overrides:
      listIterator in class AbstractList<E>
    • subList

      public BitList<E> subList(int fromIndex, int toIndex)
      Specified by:
      subList in interface List<E>
      Overrides:
      subList in class AbstractList<E>
    • cloneToArrayList

      public ArrayList<E> cloneToArrayList()
    • clone

      public BitList<E> clone()