首页

基于jetty-util工具包类实现有顺序序列队列类ArrayQueue实现源码示例说明

标签:jetty-util,序列队列,ArrayQueue,AbstractList,自定义有序队列     发布时间:2018-08-20   

一、前言

通过jetty-util源码包中org.mortbay.util.ArrayQueue序列队列实现类,继承java.util.AbstractList抽象序列类,同时实现java.util.Queue接口,详情参见源码说明。

二、源码示例

package org.mortbay.util;@b@@b@import java.util.AbstractList;@b@import java.util.NoSuchElementException;@b@import java.util.Queue;@b@@b@public class ArrayQueue<E> extends AbstractList<E>@b@  implements Queue<E>@b@{@b@  public final int DEFAULT_CAPACITY = 64;@b@  public final int DEFAULT_GROWTH = 32;@b@  protected Object _lock = this;@b@  protected Object[] _elements;@b@  protected int _nextE;@b@  protected int _nextSlot;@b@  protected int _size;@b@  protected int _growCapacity;@b@@b@  public ArrayQueue()@b@  {@b@    this._elements = new Object[64];@b@    this._growCapacity = 32;@b@  }@b@@b@  public ArrayQueue(int capacity)@b@  {@b@    this._elements = new Object[capacity];@b@    this._growCapacity = -1;@b@  }@b@@b@  public ArrayQueue(int initCapacity, int growBy)@b@  {@b@    this._elements = new Object[initCapacity];@b@    this._growCapacity = growBy;@b@  }@b@@b@  public ArrayQueue(int initCapacity, int growBy, Object lock)@b@  {@b@    this._elements = new Object[initCapacity];@b@    this._growCapacity = growBy;@b@    this._lock = lock;@b@  }@b@@b@  public int getCapacity()@b@  {@b@    return this._elements.length;@b@  }@b@@b@  public boolean add(E e)@b@  {@b@    if (!(offer(e)))@b@      throw new IllegalStateException("Full");@b@    return true;@b@  }@b@@b@  public boolean offer(E e)@b@  {@b@    synchronized (this._lock)@b@    {@b@      if ((this._size != this._elements.length) || (grow())) break label30;@b@      return false;@b@@b@      label30: this._size += 1;@b@      this._elements[(this._nextSlot++)] = e;@b@      if (this._nextSlot != this._elements.length) break label74;@b@      label74: this._nextSlot = 0;@b@    }@b@    return true;@b@  }@b@@b@  public void addUnsafe(E e)@b@  {@b@    if ((this._size == this._elements.length) && (!(grow())))@b@      throw new IllegalStateException("Full");@b@@b@    this._size += 1;@b@    this._elements[(this._nextSlot++)] = e;@b@    if (this._nextSlot == this._elements.length)@b@      this._nextSlot = 0;@b@  }@b@@b@  public E element()@b@  {@b@    synchronized (this._lock)@b@    {@b@      if (this._size == 0)@b@        throw new NoSuchElementException();@b@      return this._elements[this._nextE];@b@    }@b@  }@b@@b@  public E peek()@b@  {@b@    synchronized (this._lock)@b@    {@b@      if (this._size != 0) break label18;@b@      return null;@b@      label18: return this._elements[this._nextE];@b@    }@b@  }@b@@b@  public E poll()@b@  {@b@    synchronized (this._lock)@b@    {@b@      if (this._size != 0) break label18;@b@      return null;@b@      label18: Object e = this._elements[this._nextE];@b@      this._elements[this._nextE] = null;@b@      this._size -= 1;@b@      if (++this._nextE != this._elements.length) break label72;@b@      this._nextE = 0;@b@      label72: return e;@b@    }@b@  }@b@@b@  public E remove()@b@  {@b@    synchronized (this._lock)@b@    {@b@      if (this._size == 0)@b@        throw new NoSuchElementException();@b@      Object e = this._elements[this._nextE];@b@      this._elements[this._nextE] = null;@b@      this._size -= 1;@b@      if (++this._nextE == this._elements.length)@b@        this._nextE = 0;@b@      return e;@b@    }@b@  }@b@@b@  public void clear()@b@  {@b@    synchronized (this._lock)@b@    {@b@      this._size = 0;@b@      this._nextE = 0;@b@      this._nextSlot = 0;@b@    }@b@  }@b@@b@  public boolean isEmpty()@b@  {@b@    synchronized (this._lock)@b@    {@b@      return ((this._size == 0) ? 1 : false);@b@    }@b@  }@b@@b@  public int size()@b@  {@b@    return this._size;@b@  }@b@@b@  public E get(int index)@b@  {@b@    synchronized (this._lock)@b@    {@b@      if ((index < 0) || (index >= this._size))@b@        throw new IndexOutOfBoundsException("!(0<" + index + "<=" + this._size + ")");@b@      int i = (this._nextE + index) % this._elements.length;@b@      return this._elements[i];@b@    }@b@  }@b@@b@  public E getUnsafe(int index)@b@  {@b@    int i = (this._nextE + index) % this._elements.length;@b@    return this._elements[i];@b@  }@b@@b@  public E remove(int index)@b@  {@b@    synchronized (this._lock)@b@    {@b@      if ((index < 0) || (index >= this._size))@b@        throw new IndexOutOfBoundsException("!(0<" + index + "<=" + this._size + ")");@b@@b@      int i = (this._nextE + index) % this._elements.length;@b@      Object old = this._elements[i];@b@@b@      if (i < this._nextSlot)@b@      {@b@        System.arraycopy(this._elements, i + 1, this._elements, i, this._nextSlot - i);@b@        this._nextSlot -= 1;@b@        this._size -= 1;@b@      }@b@      else@b@      {@b@        System.arraycopy(this._elements, i + 1, this._elements, i, this._elements.length - i - 1);@b@        if (this._nextSlot > 0)@b@        {@b@          this._elements[(this._elements.length - 1)] = this._elements[0];@b@          System.arraycopy(this._elements, 1, this._elements, 0, this._nextSlot - 1);@b@          this._nextSlot -= 1;@b@        }@b@        else {@b@          this._nextSlot = (this._elements.length - 1);@b@        }@b@        this._size -= 1;@b@      }@b@@b@      return old;@b@    }@b@  }@b@@b@  public E set(int index, E element)@b@  {@b@    synchronized (this._lock)@b@    {@b@      if ((index < 0) || (index >= this._size))@b@        throw new IndexOutOfBoundsException("!(0<" + index + "<=" + this._size + ")");@b@@b@      int i = this._nextE + index;@b@      if (i >= this._elements.length)@b@        i -= this._elements.length;@b@      Object old = this._elements[i];@b@      this._elements[i] = element;@b@      return old;@b@    }@b@  }@b@@b@  public void add(int index, E element)@b@  {@b@    synchronized (this._lock)@b@    {@b@      if ((index < 0) || (index > this._size))@b@        throw new IndexOutOfBoundsException("!(0<" + index + "<=" + this._size + ")");@b@@b@      if ((this._size == this._elements.length) && (!(grow())))@b@        throw new IllegalStateException("Full");@b@@b@      if (index == this._size)@b@      {@b@        add(element);@b@      }@b@      else@b@      {@b@        int i = this._nextE + index;@b@        if (i >= this._elements.length)@b@          i -= this._elements.length;@b@@b@        this._size += 1;@b@        this._nextSlot += 1;@b@        if (this._nextSlot == this._elements.length)@b@          this._nextSlot = 0;@b@@b@        if (i < this._nextSlot)@b@        {@b@          System.arraycopy(this._elements, i, this._elements, i + 1, this._nextSlot - i);@b@          this._elements[i] = element;@b@        }@b@        else@b@        {@b@          if (this._nextSlot > 0)@b@          {@b@            System.arraycopy(this._elements, 0, this._elements, 1, this._nextSlot);@b@            this._elements[0] = this._elements[(this._elements.length - 1)];@b@          }@b@@b@          System.arraycopy(this._elements, i, this._elements, i + 1, this._elements.length - i - 1);@b@          this._elements[i] = element;@b@        }@b@      }@b@    }@b@  }@b@@b@  protected boolean grow()@b@  {@b@    if (this._growCapacity <= 0)@b@      return false;@b@@b@    Object[] elements = new Object[this._elements.length + this._growCapacity];@b@@b@    int split = this._elements.length - this._nextE;@b@    if (split > 0)@b@      System.arraycopy(this._elements, this._nextE, elements, 0, split);@b@    if (this._nextE != 0)@b@      System.arraycopy(this._elements, 0, elements, split, this._nextSlot);@b@@b@    this._elements = elements;@b@    this._nextE = 0;@b@    this._nextSlot = this._size;@b@    return true;@b@  }@b@}
  • <<相关内容>>
<<热门下载>>