KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > chaperon > common > StringSet


1 /*
2  * Copyright (C) Chaperon. All rights reserved.
3  * -------------------------------------------------------------------------
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE file.
7  */

8
9 package net.sourceforge.chaperon.common;
10
11 /**
12  * This class represents a set of strings.
13  *
14  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
15  * @version CVS $Id: StringSet.java,v 1.2 2003/12/14 09:53:56 benedikta Exp $
16  */

17 public class StringSet
18 {
19   private String JavaDoc[] strings = new String JavaDoc[0];
20
21   public StringSet() {}
22
23   public StringSet(String JavaDoc[] strings)
24   {
25     addString(strings);
26   }
27
28   public boolean addString(String JavaDoc string)
29   {
30     if (string==null)
31       return true;
32
33     for (int i = 0; i<strings.length; i++)
34       if (strings[i].equals(string))
35         return false;
36
37     String JavaDoc[] newStringSet = new String JavaDoc[strings.length+1];
38     System.arraycopy(strings, 0, newStringSet, 0, strings.length);
39     newStringSet[strings.length] = string;
40     strings = newStringSet;
41
42     return true;
43   }
44
45   public boolean addString(StringSet set)
46   {
47     boolean modified = false;
48
49     for (int i = 0; i<set.strings.length; i++)
50       modified |= addString(set.strings[i]);
51
52     return modified;
53   }
54
55   public boolean addString(String JavaDoc[] set)
56   {
57     boolean modified = false;
58
59     for (int i = 0; i<set.length; i++)
60       modified |= addString(set[i]);
61
62     return modified;
63   }
64
65   /**
66    * Returns a string given by an index.
67    *
68    * @param index Index of the string
69    *
70    * @return String
71    */

72   public String JavaDoc getString(int index)
73   {
74     return strings[index];
75   }
76
77   public String JavaDoc[] getString()
78   {
79     return strings;
80   }
81
82   /**
83    * Return the count of string in this list.
84    *
85    * @return Count of string.
86    */

87   public int getStringCount()
88   {
89     return strings.length;
90   }
91
92   public boolean isEmpty()
93   {
94     return strings.length==0;
95   }
96
97   public boolean contains(String JavaDoc string)
98   {
99     for (int i = 0; i<strings.length; i++)
100       if (strings[i]==string)
101         return true;
102
103     return false;
104   }
105
106   public boolean equals(Object JavaDoc o)
107   {
108     if (o instanceof StringSet)
109     {
110       StringSet set = (StringSet)o;
111
112       if (set.strings.length!=strings.length)
113         return false;
114
115       for (int i = 0; i<strings.length; i++)
116         for (int j = 0; j<=set.strings.length; j++)
117         {
118           if (j==strings.length)
119             return false;
120
121           if (strings[i].equals(set.strings[j]))
122             break;
123         }
124
125       return true;
126     }
127
128     return false;
129   }
130
131   public String JavaDoc toString()
132   {
133     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
134
135     buffer.append("[");
136
137     for (int i = 0; i<strings.length; i++)
138     {
139       buffer.append(strings[i]);
140
141       if (i<(strings.length-1))
142         buffer.append(",");
143     }
144
145     buffer.append("]");
146
147     return buffer.toString();
148   }
149 }
150
Popular Tags