KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mckoi > util > StringListBucket


1 /**
2  * com.mckoi.util.StringListBucket 14 Apr 2000
3  *
4  * Mckoi SQL Database ( http://www.mckoi.com/database )
5  * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
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  * Version 2 as published by the Free Software Foundation.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License Version 2 for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * Version 2 along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  *
20  * Change Log:
21  *
22  *
23  */

24
25 package com.mckoi.util;
26
27 import java.util.Vector JavaDoc;
28
29 /**
30  * A utility container class for holding a list of strings. This method
31  * provides a convenient way of exporting and importing the list as a string
32  * itself. This is useful if we need to represent a variable array of
33  * strings.
34  *
35  * @author Tobias Downer
36  */

37
38 public class StringListBucket {
39
40   /**
41    * The String List.
42    */

43   private Vector JavaDoc string_list;
44
45   /**
46    * Constructs the bucket.
47    */

48   public StringListBucket() {
49     string_list = new Vector JavaDoc();
50   }
51
52   public StringListBucket(String JavaDoc list) {
53     this();
54     fromString(list);
55   }
56
57
58   /**
59    * Returns the number of string elements in the list.
60    */

61   public int size() {
62     return string_list.size();
63   }
64
65   /**
66    * Clears the list of all string elements.
67    */

68   public void clear() {
69     string_list.clear();
70   }
71
72   /**
73    * Adds a string to the end of the list.
74    */

75   public void add(String JavaDoc element) {
76     string_list.addElement(element);
77   }
78
79   /**
80    * Adds a string to the given index of the list.
81    */

82   public void add(String JavaDoc element, int index) {
83     string_list.insertElementAt(element, index);
84   }
85
86   /**
87    * Returns the string at the given index of the list.
88    */

89   public String JavaDoc get(int index) {
90     return (String JavaDoc) string_list.elementAt(index);
91   }
92
93   /**
94    * Removes the string at the given index of the list.
95    */

96   public void remove(int index) {
97     string_list.removeElementAt(index);
98   }
99
100   /**
101    * Returns true if the list contains the given element string.
102    */

103   public boolean contains(String JavaDoc element) {
104     return string_list.contains(element);
105   }
106
107   /**
108    * Returns the index of the given string in the bucket, or -1 if not found.
109    */

110   public int indexOfVar(String JavaDoc element) {
111     return string_list.indexOf(element);
112   }
113
114   /**
115    * Returns the bucket as a StringBuffer. This can be exported to a file
116    * or to a database, etc.
117    */

118   public StringBuffer JavaDoc toStringBuffer() {
119     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
120     buffer.append("||");
121     for (int i = 0; i < size(); ++i) {
122       String JavaDoc str = get(i);
123       buffer.append(str);
124       buffer.append("||");
125     }
126     return buffer;
127   }
128
129   public String JavaDoc toString() {
130     return toStringBuffer().toString();
131   }
132
133   /**
134    * Imports from a String into this bucket. This is used to transform a
135    * previously exported bucket via 'toStringBuffer()'.
136    */

137   public void fromString(String JavaDoc list) {
138     clear();
139     if (list != null && list.length() > 2) {
140       int last = 2;
141       int i = list.indexOf("||", 2);
142       while (i != -1) {
143         String JavaDoc entry = list.substring(last, i);
144         add(entry);
145         last = i + 2;
146         i = list.indexOf("||", last);
147       }
148     }
149   }
150
151 }
152
Popular Tags