2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0.
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 package gov.nasa.jpf.util;
21 import java.util.Iterator;
22 import java.util.NoSuchElementException;
25 * object queue that uses cached link entries
27 public class LinkedObjectQueue<E> implements ObjectQueue<E> {
30 Entry next; // single linked list
31 Object obj; // referenced object
35 protected Entry first;
39 protected int maxCache;
43 class FIFOIterator implements Iterator<E> {
47 public boolean hasNext() {
54 throw new NoSuchElementException();
63 public void remove() {
64 throw new UnsupportedOperationException("arbitrary remove from queue not supported");
68 public LinkedObjectQueue (){
72 public LinkedObjectQueue (int maxCache){
73 this.maxCache = maxCache;
82 public boolean add(E obj) {
85 if (nFree > 0){ // reuse a cached Entry object
110 public boolean offer( E obj){
115 public boolean isEmpty(){
140 if (nFree < maxCache){
142 e.next = (nFree++ > 0) ? free : null;
152 public E remove() throws NoSuchElementException {
154 throw new NoSuchElementException();
161 public Iterator<E> iterator(){
162 return new FIFOIterator();
166 public void process( Processor<E> proc) {
167 for (Entry e = first; e != null; ) {
168 proc.process( (E)e.obj);
170 e.obj = null; // avoid memory leaks
172 if (nFree < maxCache){
173 // recycle to save some allocation and a lot of shortliving garbage
175 e.next = (nFree++ > 0) ? free : null;
187 public void clear () {
192 // don't reset nFree and free since we limit the memory size of our cache
193 // and the Entry object do not reference anything