KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Ring


1 /*
2  * Ring.java
3  *
4  * Copyright (C) 1998-2002 Peter Graves
5  * $Id: Ring.java,v 1.1.1.1 2002/09/24 16:09:20 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import java.util.ArrayList JavaDoc;
25
26 public class Ring
27 {
28     private final int capacity;
29     private final ArrayList JavaDoc list;
30     private int index;
31     private int indexOfNextPop = -1;
32     private String JavaDoc lastPop;
33
34     public Ring(int capacity)
35     {
36         this.capacity = capacity;
37         list = new ArrayList JavaDoc(capacity);
38     }
39
40     public synchronized final int size()
41     {
42         return list.size();
43     }
44
45     public String JavaDoc get(int i)
46     {
47         if (i >= 0 && i < list.size())
48             return (String JavaDoc) list.get(i);
49         else
50             return null;
51     }
52
53     public synchronized void appendToCurrent(String JavaDoc s)
54     {
55         if (list.size() == 0)
56             list.add(s);
57         else {
58             String JavaDoc existing = (String JavaDoc) list.get(list.size() - 1);
59             list.set(list.size() - 1, existing.concat(s));
60         }
61     }
62
63     public synchronized void appendNew(String JavaDoc s)
64     {
65         final int size = list.size();
66         // See if we already have the string in question.
67
for (int i = size-1; i >= 0; i--) {
68             String JavaDoc existing = (String JavaDoc) list.get(i);
69             if (existing.equals(s)) {
70                 // Found it! If it's not already the last element, promote it.
71
if (i != size-1) {
72                     list.remove(i);
73                     list.add(s);
74                 }
75                 return;
76             }
77         }
78         if (size < capacity)
79             list.add(s);
80         else {
81             for (int i = 1; i < size; i++)
82                 list.set(i-1, list.get(i));
83             list.set(capacity-1, s);
84         }
85     }
86
87     public synchronized String JavaDoc peek()
88     {
89         if (list.size() == 0)
90             return null;
91         return (String JavaDoc) list.get(list.size() - 1);
92     }
93
94     public synchronized String JavaDoc pop()
95     {
96         indexOfNextPop = list.size() - 2;
97         if (list.size() == 0)
98             return null;
99         return lastPop = (String JavaDoc) list.get(list.size() - 1);
100     }
101
102     public synchronized String JavaDoc popNext()
103     {
104         if (indexOfNextPop < 0)
105             return null;
106         Debug.assertTrue(indexOfNextPop < list.size());
107         lastPop = (String JavaDoc) list.get(indexOfNextPop);
108         if (--indexOfNextPop < 0)
109             indexOfNextPop = list.size() - 1;
110         return lastPop;
111     }
112
113     protected synchronized void promoteLast()
114     {
115         if (lastPop != null)
116             promote(lastPop);
117     }
118
119     private void promote(String JavaDoc s)
120     {
121         for (int i = list.size()-1; i >= 0; i--) {
122             String JavaDoc existing = (String JavaDoc) list.get(i);
123             if (existing.equals(s)) {
124                 if (i != list.size()-1) {
125                     list.remove(i);
126                     list.add(s);
127                 }
128                 break;
129             }
130         }
131     }
132 }
133
Popular Tags