📄 objectbuffer.java
字号:
/**
* Copyright (C) 2003 Manfred Andres
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
package freecs.util;
/**
* FIFO-Buffer for objects whicht automatically removes Objects retrieved via
* get(). Programmers have to verify if value has bin put to this ObjectBuffer.
* If buffer is full, put-method returns false, on success it returns true.
*/
public class ObjectBuffer {
private Object elements[];
private int nextAdd;
private int nextRemove;
private int capacity;
private int counter=0;
private boolean isEmpty,
isFull;
public ObjectBuffer (int cap) {
if (cap < 1)
this.capacity = 1;
else
this.capacity = cap;
this.nextAdd=0;
this.nextRemove=0;
this.isEmpty = true;
elements = new Object[capacity];
}
public int size () {
return counter;
}
public int capacity () {
return capacity;
}
public boolean isEmpty () {
return isEmpty;
}
public boolean isFull () {
return isFull;
}
public boolean put (Object o) {
if (this.isFull ()) return false;
elements[nextAdd] = o;
nextAdd++;
isEmpty = false;
if (nextAdd >= capacity)
nextAdd = 0;
if (nextAdd == nextRemove)
isFull = true;
counter++;
return true;
}
public boolean contains (Object o) {
for (int i = 0, j=nextRemove; i < capacity; i++, j++) {
if (j >= capacity) j = 0;
if (elements[j].equals (o)) return true;
}
return false;
}
public Object get () {
if (this.isEmpty ()) return null;
return elements[nextRemove];
}
public Object pop () {
if (this.isEmpty ()) return null;
Object retObj = elements[nextRemove];
elements[nextRemove]=null;
nextRemove++;
isFull = false;
if (nextRemove >= capacity)
nextRemove = 0;
if (nextRemove == nextAdd)
isEmpty = true;
counter--;
return (retObj);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -