001: /*
002: * Ring.java
003: *
004: * Copyright (C) 1998-2002 Peter Graves
005: * $Id: Ring.java,v 1.1.1.1 2002/09/24 16:09:20 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import java.util.ArrayList;
025:
026: public class Ring {
027: private final int capacity;
028: private final ArrayList list;
029: private int index;
030: private int indexOfNextPop = -1;
031: private String lastPop;
032:
033: public Ring(int capacity) {
034: this .capacity = capacity;
035: list = new ArrayList(capacity);
036: }
037:
038: public synchronized final int size() {
039: return list.size();
040: }
041:
042: public String get(int i) {
043: if (i >= 0 && i < list.size())
044: return (String) list.get(i);
045: else
046: return null;
047: }
048:
049: public synchronized void appendToCurrent(String s) {
050: if (list.size() == 0)
051: list.add(s);
052: else {
053: String existing = (String) list.get(list.size() - 1);
054: list.set(list.size() - 1, existing.concat(s));
055: }
056: }
057:
058: public synchronized void appendNew(String s) {
059: final int size = list.size();
060: // See if we already have the string in question.
061: for (int i = size - 1; i >= 0; i--) {
062: String existing = (String) list.get(i);
063: if (existing.equals(s)) {
064: // Found it! If it's not already the last element, promote it.
065: if (i != size - 1) {
066: list.remove(i);
067: list.add(s);
068: }
069: return;
070: }
071: }
072: if (size < capacity)
073: list.add(s);
074: else {
075: for (int i = 1; i < size; i++)
076: list.set(i - 1, list.get(i));
077: list.set(capacity - 1, s);
078: }
079: }
080:
081: public synchronized String peek() {
082: if (list.size() == 0)
083: return null;
084: return (String) list.get(list.size() - 1);
085: }
086:
087: public synchronized String pop() {
088: indexOfNextPop = list.size() - 2;
089: if (list.size() == 0)
090: return null;
091: return lastPop = (String) list.get(list.size() - 1);
092: }
093:
094: public synchronized String popNext() {
095: if (indexOfNextPop < 0)
096: return null;
097: Debug.assertTrue(indexOfNextPop < list.size());
098: lastPop = (String) list.get(indexOfNextPop);
099: if (--indexOfNextPop < 0)
100: indexOfNextPop = list.size() - 1;
101: return lastPop;
102: }
103:
104: protected synchronized void promoteLast() {
105: if (lastPop != null)
106: promote(lastPop);
107: }
108:
109: private void promote(String s) {
110: for (int i = list.size() - 1; i >= 0; i--) {
111: String existing = (String) list.get(i);
112: if (existing.equals(s)) {
113: if (i != list.size() - 1) {
114: list.remove(i);
115: list.add(s);
116: }
117: break;
118: }
119: }
120: }
121: }
|