KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > chateverywhere > InputStack


1 /* InputStack
2  * Copyright (C) 1998-2004 Alexis de Bernis <alexis@bernis.org>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  */

19
20 package org.chateverywhere;
21
22 import java.util.*;
23
24 public class InputStack
25 {
26     private int max_elements_in_stack = 30;
27     private int input_stack_pos;
28     private Vector input_stack;
29
30     public InputStack()
31     {
32         input_stack = new Vector(max_elements_in_stack);
33         input_stack_pos = 0;
34     }
35     
36     public InputStack(int max)
37     {
38         max_elements_in_stack = max;
39     }
40
41     /******************* Add an entry ********************/
42     public void add(String JavaDoc entry)
43     {
44         if(input_stack.size() == max_elements_in_stack)
45             input_stack.removeElementAt(max_elements_in_stack - 1);
46
47         input_stack.insertElementAt(entry, 0);
48         input_stack_pos = 0;
49     }
50
51
52     /**************** Call previous entry ****************/
53     public String JavaDoc up()
54     {
55         if(input_stack_pos < input_stack.size())
56             return (String JavaDoc) input_stack.elementAt(input_stack_pos++);
57
58         return (String JavaDoc) input_stack.elementAt(input_stack_pos - 1);
59     }
60
61     /****************** Call next entry ******************/
62     public String JavaDoc down()
63     {
64         if(input_stack_pos == 0) {
65             return null;
66         } else if(input_stack_pos == 1) {
67             input_stack_pos = 0;
68             return "";
69         }
70
71         return (String JavaDoc) input_stack.elementAt(--input_stack_pos - 1);
72     }
73 }
74
Popular Tags