1 36 package org.columba.ristretto.imap; 37 38 import java.util.Collections ; 39 import java.util.Iterator ; 40 import java.util.LinkedList ; 41 import java.util.List ; 42 43 48 public class SequenceSet { 49 50 private final static SequenceSet all; 51 52 static { 53 all = new SequenceSet(); 54 all.addAll(); 55 } 56 57 private List set; 58 59 63 public SequenceSet() { 64 set = new LinkedList (); 65 } 66 67 72 public SequenceSet(int a) { 73 this(); 74 add(a); 75 } 76 77 83 public SequenceSet(int a, int b) { 84 this(); 85 add(a,b); 86 } 87 88 93 public SequenceSet(Integer [] s) { 94 this(); 95 for( int i=0; i<s.length; i++) { 96 add( s[i].intValue()); 97 } 98 } 99 100 101 108 public SequenceSet(int[] s, int offset, int length) { 109 this(); 110 for( int i=offset; i<offset + length; i++) { 111 add( s[i]); 112 } 113 } 114 115 120 public SequenceSet(List l) { 121 this(); 122 Iterator it = l.iterator(); 123 while( it.hasNext()) { 124 add(((Integer )it.next()).intValue()); 125 } 126 } 127 128 133 public static SequenceSet getAll() { 134 return all; 135 } 136 137 142 public void add(int a) { 143 set.add(new SequenceEntry(a)); 144 } 145 146 152 public void add(int a, int b) { 153 set.add(new SequenceEntry(a, b)); 154 } 155 156 161 public void addOpenRange(int a) { 162 set.add(new SequenceEntry(a, SequenceEntry.STAR)); 163 } 164 165 169 public void addAll() { 170 set.add(new SequenceEntry(1,SequenceEntry.STAR)); 171 } 172 173 178 public void pack() { 179 if( set.size() <= 1 ) return; 180 Collections.sort( set ); 181 Iterator it = set.iterator(); 182 183 SequenceEntry a = (SequenceEntry) it.next(); 184 SequenceEntry b; 185 186 while( it.hasNext() ) { 187 b = (SequenceEntry) it.next(); 188 if( a.canMergeWith(b)) { 189 a.merge(b); 190 it.remove(); 191 } else { 192 a = b; 193 } 194 } 195 } 196 197 198 201 public String toString() { 202 pack(); 203 StringBuffer result = new StringBuffer (); 204 Iterator it = set.iterator(); 205 result.append( it.next().toString()); 206 while( it.hasNext() ) { 207 result.append(','); 208 result.append(it.next().toString()); 209 } 210 return result.toString(); 211 } 212 213 220 public int getLength(int exists) { 221 int length = 0; 222 223 Iterator it = set.iterator(); 224 while( it.hasNext() ) { 225 SequenceEntry entry = (SequenceEntry) it.next(); 226 227 switch( entry.getType() ) { 228 case SequenceEntry.ALL : { 229 return exists; 230 } 231 232 case SequenceEntry.OPEN_RANGE : { 233 length += exists - entry.getA() + 1; 234 break; 235 } 236 237 case SequenceEntry.RANGE : { 238 length += entry.getB() - entry.getA() + 1; 239 break; 240 } 241 242 case SequenceEntry.SINGLE : { 243 length ++; 244 break; 245 } 246 } 247 } 248 249 return length; 250 } 251 252 259 public int[] toArray(int exists) { 260 pack(); 261 int[] result = new int[getLength(exists)]; 262 int pos=0; 263 int index=1; 264 265 Iterator it = set.iterator(); 266 while( it.hasNext() ) { 267 SequenceEntry entry = (SequenceEntry) it.next(); 268 269 switch( entry.getType() ) { 270 case SequenceEntry.ALL : { 271 for( int i=0; i<exists; i++) { 272 result[i]=i+1; 273 } 274 } 275 276 case SequenceEntry.OPEN_RANGE : { 277 for( index=entry.getA(); index <= exists ; index++ ) { 278 result[pos++] = index; 279 } 280 break; 281 } 282 283 case SequenceEntry.RANGE : { 284 for( index=entry.getA(); index <= entry.getB() ; index++ ) { 285 result[pos++] = index; 286 } 287 break; 288 } 289 290 case SequenceEntry.SINGLE : { 291 result[pos++] = entry.getA() == SequenceEntry.STAR ? exists: entry.getA(); 292 break; 293 } 294 } 295 } 296 297 298 return result; 299 } 300 } 301 | Popular Tags |