1 8 9 package net.sourceforge.chaperon.model.extended; 10 11 17 public abstract class PatternList extends Pattern 18 { 19 private PatternListEntry first = null; 20 private PatternListEntry last = null; 21 private int count = 0; 22 23 28 public void addPattern(Pattern pattern) 29 { 30 if (first==null) 31 last = first = new PatternListEntry(pattern, null); 32 else 33 { 34 last.next = new PatternListEntry(pattern, last); 35 last = last.next; 36 } 37 38 count++; 39 } 40 41 48 public Pattern getPattern(int index) 49 { 50 int i = 0; 51 for (PatternListEntry entry = first; entry!=null; entry = entry.next, i++) 52 if (i==index) 53 return entry.pattern; 54 55 return null; 56 } 57 58 public PatternIterator getPattern() 59 { 60 return new PatternListEntryIterator(first); 61 } 62 63 public Pattern[] getPatternAsArray() 64 { 65 Pattern[] pattern = new Pattern[count]; 66 int i = 0; 67 for (PatternListEntry entry = first; entry!=null; entry = entry.next, i++) 68 pattern[i] = entry.pattern; 69 70 return pattern; 71 } 72 73 78 public int getPatternCount() 79 { 80 return count; 81 } 82 83 public boolean isNullable() 84 { 85 boolean nullable = true; 86 for (PatternListEntry entry = first; entry!=null; entry = entry.next) 87 nullable &= entry.pattern.isNullable(); 88 89 return nullable; 90 } 91 92 public PatternSet getFirstSet() 93 { 94 PatternSet set = new PatternSet(); 95 for (PatternListEntry entry = first; entry!=null; entry = entry.next) 96 { 97 set.addPattern(entry.pattern.getFirstSet()); 98 if (!entry.pattern.isNullable()) 99 break; 100 } 101 102 return set; 103 } 104 105 public PatternSet getLastSet() 106 { 107 PatternSet set = new PatternSet(); 108 for (PatternListEntry entry = last; entry!=null; entry = entry.previous) 109 { 110 set.addPattern(entry.pattern.getLastSet()); 111 if (!entry.pattern.isNullable()) 112 break; 113 } 114 115 return set; 116 } 117 118 public void update() 119 { 120 for (PatternListEntry entry = first; entry!=null; entry = entry.next) 121 entry.pattern.update(); 122 123 for (PatternListEntry entry = first; entry!=null; entry = entry.next) 124 { 125 for (PatternListEntry entry2 = entry.next; entry2!=null; entry2 = entry2.next) 126 { 127 for (PatternIterator i = entry.pattern.getLastSet().getPattern(); i.hasNext();) 128 { 129 Pattern lastPattern = i.next(); 130 for (PatternIterator j = entry2.pattern.getFirstSet().getPattern(); j.hasNext();) 131 { 132 Pattern firstPattern = j.next(); 133 lastPattern.addSuccessor(firstPattern); 134 } 135 } 136 137 if (!entry2.pattern.isNullable()) 138 break; 139 } 140 } 141 } 142 143 public PatternSet getAllPattern() 144 { 145 PatternSet set = new PatternSet(); 146 set.addPattern(this); 147 for (PatternListEntry entry = first; entry!=null; entry = entry.next) 148 set.addPattern(entry.pattern.getAllPattern()); 149 150 return set; 151 } 152 153 public char[] getLimits() 154 { 155 return new char[0]; 156 } 157 158 public boolean contains(char minimum, char maximum) 159 { 160 return false; 161 } 162 163 public boolean contains(char c) 164 { 165 return false; 166 } 167 168 public String getSymbol() 169 { 170 return null; 171 } 172 173 public String toString() 174 { 175 return toString(null, null); 176 } 177 178 public String toString(PatternSet previous, PatternSet next) 179 { 180 StringBuffer buffer = new StringBuffer (); 181 182 if ((previous!=null) && (previous.contains(this))) 183 { 184 if ((next!=null) && (next.contains(this))) 185 buffer.append("."+toString()+"."); 186 else 187 buffer.append(toString()+"."); 188 } 189 else if ((next!=null) && (next.contains(this))) 190 buffer.append("."+toString()); 191 192 if (count>1) 193 { 194 buffer.append("("); 195 for (PatternListEntry entry = first; entry!=null; entry = entry.next) 196 { 197 if (entry!=first) 198 buffer.append(" "); 199 200 buffer.append(entry.pattern.toString(previous, next)); 201 } 202 203 buffer.append(")"); 204 } 205 else if (count==1) 206 buffer.append(first.pattern.toString(previous, next)); 207 208 return buffer.toString(); 209 } 210 211 private class PatternListEntry 212 { 213 public final Pattern pattern; 214 public PatternListEntry previous = null; 215 public PatternListEntry next = null; 216 217 private PatternListEntry(Pattern pattern, PatternListEntry previous) 218 { 219 this.pattern = pattern; 220 this.previous = previous; 221 } 222 } 223 224 private class PatternListEntryIterator implements PatternIterator 225 { 226 private PatternListEntry entry; 227 228 private PatternListEntryIterator(PatternListEntry entry) 229 { 230 this.entry = entry; 231 } 232 233 public boolean hasNext() 234 { 235 return (entry!=null); 236 } 237 238 public Pattern next() 239 { 240 Pattern pattern = entry.pattern; 241 entry = entry.next; 242 return pattern; 243 } 244 } 245 } 246 | Popular Tags |