⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 queue.java

📁 一个ISAG Server的模拟器
💻 JAVA
字号:
package cn.com.ffcs.lbp;


import java.util.Arrays;

/**
 * <p>
 * Title: 智能短信系统接口程序
 * </p>
 * <p>简单队列</p>
 * <p>Copyright: 2008 福建福富软件技术股份有限公司 </p>
 * <p>Company: 福建福富软件技术股份有限公司</p>
 * @author chenxin
 * @version 1.0 $Date:2007-06-28
 */
public class Queue {
    private static  Object[] items;
    private static  int first = 0;
    private static  int last = 0;
    private static  int size = 0;
    private static   boolean open = false;

    /**
     * Construct DetectOS new, empty <code>Queue</code> with the specified initial
     * capacity.
     */
    public Queue(int initialCapacity) {
        items = new Object[initialCapacity];
    }

 
    public static  void open() {
        clear();
        open = true;
    }

    public static  void close() {
        open = false;
        clear();
    }

    /**
     * Clears this queue.
     */
    private static   void clear() {
        Arrays.fill(items, null);
        first = 0;
        last = 0;
        size = 0;
    }

    /**
     * Dequeues from this queue.
     *
     * @return <code>null</code>, if this queue is empty or the element is
     *         really <code>null</code>.
     */
    public static  Object pop() {
        if (size == 0) {
            return null;
        }

        Object ret = items[first];
        items[first] = null;
        first = (first + 1) % items.length;

        size--;

        return ret;
    }

    /**
     * Enqueue into this queue.
     */
    public static   boolean push(Object obj) {
        if (!open) {
            return false;
        }

        if (size == items.length) {
            // expand queue
            final int oldLen = items.length;
            Object[] tmp = new Object[oldLen * 2];

            if (first < last) {
                System.arraycopy(items, first, tmp, 0, last - first);
            } else {
                System.arraycopy(items, first, tmp, 0, oldLen - first);
                System.arraycopy(items, 0, tmp, oldLen - first, last);
            }

            first = 0;
            last = oldLen;
            items = tmp;
        }

        items[last] = obj;
        last = (last + 1) % items.length;
        size++;
        return true;
    }

    /**
     * Returns the first element of the queue.
     *
     * @return <code>null</code>, if the queue is empty, or the element is
     *         really <code>null</code>.
     */
    public static   Object first() {
        if (!open) {
            return null;
        }

        if (size == 0) {
            return null;
        }

        return items[first];
    }

    /**
     * Returns <code>true</code> if the queue is empty.
     */
    public static  boolean isEmpty() {
        return (size == 0);
    }

    /**
     * Returns the number of elements in the queue.
     */
    public static   int size() {
        return size;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -