001: /**
002: * com.mckoi.util.StringListBucket 14 Apr 2000
003: *
004: * Mckoi SQL Database ( http://www.mckoi.com/database )
005: * Copyright (C) 2000, 2001, 2002 Diehl and Associates, Inc.
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: * Version 2 as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License Version 2 for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * Version 2 along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: * Change Log:
021: *
022: *
023: */package com.mckoi.util;
024:
025: import java.util.Vector;
026:
027: /**
028: * A utility container class for holding a list of strings. This method
029: * provides a convenient way of exporting and importing the list as a string
030: * itself. This is useful if we need to represent a variable array of
031: * strings.
032: *
033: * @author Tobias Downer
034: */
035:
036: public class StringListBucket {
037:
038: /**
039: * The String List.
040: */
041: private Vector string_list;
042:
043: /**
044: * Constructs the bucket.
045: */
046: public StringListBucket() {
047: string_list = new Vector();
048: }
049:
050: public StringListBucket(String list) {
051: this ();
052: fromString(list);
053: }
054:
055: /**
056: * Returns the number of string elements in the list.
057: */
058: public int size() {
059: return string_list.size();
060: }
061:
062: /**
063: * Clears the list of all string elements.
064: */
065: public void clear() {
066: string_list.clear();
067: }
068:
069: /**
070: * Adds a string to the end of the list.
071: */
072: public void add(String element) {
073: string_list.addElement(element);
074: }
075:
076: /**
077: * Adds a string to the given index of the list.
078: */
079: public void add(String element, int index) {
080: string_list.insertElementAt(element, index);
081: }
082:
083: /**
084: * Returns the string at the given index of the list.
085: */
086: public String get(int index) {
087: return (String) string_list.elementAt(index);
088: }
089:
090: /**
091: * Removes the string at the given index of the list.
092: */
093: public void remove(int index) {
094: string_list.removeElementAt(index);
095: }
096:
097: /**
098: * Returns true if the list contains the given element string.
099: */
100: public boolean contains(String element) {
101: return string_list.contains(element);
102: }
103:
104: /**
105: * Returns the index of the given string in the bucket, or -1 if not found.
106: */
107: public int indexOfVar(String element) {
108: return string_list.indexOf(element);
109: }
110:
111: /**
112: * Returns the bucket as a StringBuffer. This can be exported to a file
113: * or to a database, etc.
114: */
115: public StringBuffer toStringBuffer() {
116: StringBuffer buffer = new StringBuffer();
117: buffer.append("||");
118: for (int i = 0; i < size(); ++i) {
119: String str = get(i);
120: buffer.append(str);
121: buffer.append("||");
122: }
123: return buffer;
124: }
125:
126: public String toString() {
127: return toStringBuffer().toString();
128: }
129:
130: /**
131: * Imports from a String into this bucket. This is used to transform a
132: * previously exported bucket via 'toStringBuffer()'.
133: */
134: public void fromString(String list) {
135: clear();
136: if (list != null && list.length() > 2) {
137: int last = 2;
138: int i = list.indexOf("||", 2);
139: while (i != -1) {
140: String entry = list.substring(last, i);
141: add(entry);
142: last = i + 2;
143: i = list.indexOf("||", last);
144: }
145: }
146: }
147:
148: }
|