KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > filesys > util > StringList


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.filesys.util;
18
19 import java.util.Vector JavaDoc;
20
21 /**
22  * String List Class
23  */

24 public class StringList
25 {
26
27     // List of strings
28

29     private Vector JavaDoc<String JavaDoc> m_list;
30
31     /**
32      * Default constructor
33      */

34     public StringList()
35     {
36         m_list = new Vector JavaDoc<String JavaDoc>();
37     }
38
39     /**
40      * Return the number of strings in the list
41      *
42      * @return int
43      */

44     public final int numberOfStrings()
45     {
46         return m_list.size();
47     }
48
49     /**
50      * Add a string to the list
51      *
52      * @param str String
53      */

54     public final void addString(String JavaDoc str)
55     {
56         m_list.add(str);
57     }
58
59     /**
60      * Add a list of strings to this list
61      *
62      * @param list StringList
63      */

64     public final void addStrings(StringList list)
65     {
66         if (list != null && list.numberOfStrings() > 0)
67             for (int i = 0; i < list.numberOfStrings(); m_list.add(list.getStringAt(i++)))
68                 ;
69     }
70
71     /**
72      * Return the string at the specified index
73      *
74      * @param idx int
75      * @return String
76      */

77     public final String JavaDoc getStringAt(int idx)
78     {
79         if (idx < 0 || idx >= m_list.size())
80             return null;
81         return (String JavaDoc) m_list.elementAt(idx);
82     }
83
84     /**
85      * Check if the list contains the specified string
86      *
87      * @param str String
88      * @return boolean
89      */

90     public final boolean containsString(String JavaDoc str)
91     {
92         return m_list.contains(str);
93     }
94
95     /**
96      * Return the index of the specified string, or -1 if not in the list
97      *
98      * @param str String
99      * @return int
100      */

101     public final int findString(String JavaDoc str)
102     {
103         return m_list.indexOf(str);
104     }
105
106     /**
107      * Remove the specified string from the list
108      *
109      * @param str String
110      * @return boolean
111      */

112     public final boolean removeString(String JavaDoc str)
113     {
114         return m_list.removeElement(str);
115     }
116
117     /**
118      * Remove the string at the specified index within the list
119      *
120      * @param idx int
121      * @return String
122      */

123     public final String JavaDoc removeStringAt(int idx)
124     {
125         if (idx < 0 || idx >= m_list.size())
126             return null;
127         String JavaDoc ret = (String JavaDoc) m_list.elementAt(idx);
128         m_list.removeElementAt(idx);
129         return ret;
130     }
131
132     /**
133      * Clear the strings from the list
134      */

135     public final void remoteAllStrings()
136     {
137         m_list.removeAllElements();
138     }
139
140     /**
141      * Return the string list as a string
142      *
143      * @return String
144      */

145     public String JavaDoc toString()
146     {
147
148         // Check if the list is empty
149

150         if (numberOfStrings() == 0)
151             return "";
152
153         // Build the string
154

155         StringBuffer JavaDoc str = new StringBuffer JavaDoc();
156
157         for (int i = 0; i < m_list.size(); i++)
158         {
159             str.append(getStringAt(i));
160             str.append(",");
161         }
162
163         // Remove the trailing comma
164

165         if (str.length() > 0)
166             str.setLength(str.length() - 1);
167
168         // Return the string
169

170         return str.toString();
171     }
172 }
173
Popular Tags