1 8 9 package net.sourceforge.chaperon.common; 10 11 17 public class StringSet 18 { 19 private String [] strings = new String [0]; 20 21 public StringSet() {} 22 23 public StringSet(String [] strings) 24 { 25 addString(strings); 26 } 27 28 public boolean addString(String 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 [] newStringSet = new String [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 [] 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 72 public String getString(int index) 73 { 74 return strings[index]; 75 } 76 77 public String [] getString() 78 { 79 return strings; 80 } 81 82 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 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 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 toString() 132 { 133 StringBuffer buffer = new StringBuffer (); 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 |