?? queue.java
字號(hào):
package cn.com.ffcs.lbp;
import java.util.Arrays;
/**
* <p>
* Title: 智能短信系統(tǒng)接口程序
* </p>
* <p>簡(jiǎn)單隊(duì)列</p>
* <p>Copyright: 2008 福建福富軟件技術(shù)股份有限公司 </p>
* <p>Company: 福建福富軟件技術(shù)股份有限公司</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;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -