/* * Copyright (C) 1995, 1996 By Radu Sion * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by Radu Sion (radus@cs.pub.ro) * * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * * (radus@cs.pub.ro, http://sundy.cs.pub.ro/~radus/) * */ /** * A synchronized class used in reprezenting a circular list of Objects optimized for round robin parsing.

* Written: Radu Sion
* Version: 0.22
* Source: RoundRobin.java */ public class RoundRobin extends Object { /** * The maximum number of entries allowed out of internal reasons */ public static final int MAX_ENTRIES = 32760; Object data[]; /* efective data */ int next_ref[]; /* holds data ref to next Object in data[] */ int prev_ref[]; /* holds data ref to prev Object in data[] */ int current; int entries; int maxentries; /** * @param max_entries Maximum entries in the structure */ public RoundRobin(int max_entries) { maxentries = max_entries; if ( (maxentries<=0) || (maxentries>MAX_ENTRIES) ) maxentries=MAX_ENTRIES; data = new Object[maxentries]; next_ref = new int[maxentries]; prev_ref = new int[maxentries]; current = 0; next_ref[current] = 0; prev_ref[current] = 0; entries = 0; } /** * Tests if structure is empty. */ public synchronized boolean isEmpty() { return (entries == 0); } /** * Tests if structure is full. */ public synchronized boolean isFull() { return (entries == maxentries); } /** * The number of Objects in the structure * @return The current number of entries in the structure */ public synchronized int Size() { return entries; } /** * Gets the maximum entries allowed in this structure. * This value is the value passed to the constructor or MAX_ENTRIES . */ public synchronized int getMaxSize() { return maxentries; } /** * Gets the current Object. * @return The current Object * @exception EmptyException If the structure is empty */ public synchronized Object getCurrent() throws EmptyException { if (entries == 0) throw new EmptyException(); return data[current]; } /** * Gets the next Object in the structure. (round robin parsing) * @return The next Object * @exception EmptyException If the structure is empty */ public synchronized Object getNext() throws EmptyException { if (entries == 0) throw new EmptyException(); current = next_ref[current]; return data[current]; } /** * Removes CURRENT Object. After removing, the PREVIOUS becomes current. * @exception EmptyException If the structure is empty */ public synchronized void Remove() throws EmptyException { if (entries == 0) throw new EmptyException(); data[current] = null; /* is it alright ???????????? */ entries = entries - 1; prev_ref[next_ref[current]] = prev_ref[current]; next_ref[prev_ref[current]] = next_ref[current]; current = prev_ref[current]; /* ... should it be on the prev object ? */ /* current = next_ref[current]; ... on the next object ? */ } /** * Removes the specified Object. After removing, if the Object was * the current one then the PREVIOUS becomes current, otherwise the * current remains unchanged. The Objects are compared via == * comparision. Only the first occurence is removed !!! * @param ooo The objetc to remove. * @exception EmptyException If the structure is empty */ public synchronized void Remove(Object ooo) throws EmptyException { int dummy; if (entries == 0) throw new EmptyException(); for (dummy = 0; dummy