1 36 package org.columba.ristretto.imap; 37 38 43 public class SequenceEntry implements Comparable { 44 47 public static final int SINGLE = 0; 48 51 public static final int RANGE = 1; 52 55 public static final int OPEN_RANGE = 2; 56 59 public static final int ALL = 3; 60 61 64 public static final int STAR = -1; 65 66 67 private int a; 70 private int b; 71 private int type; 72 73 77 public SequenceEntry(int a) { 78 this.a = a; 79 this.b = a; 80 type = SINGLE; 81 } 82 83 89 public SequenceEntry(int a, int b) { 90 if( a == b ) { 91 this.a = a; 92 this.b = a; 93 type = SINGLE; 94 95 return; 96 } 97 98 if( a == STAR || b == STAR) { 99 type = OPEN_RANGE; 100 this.a = Math.max(a,b); 101 this.b = this.a; 102 103 if( this.a == 1) { 104 type = ALL; 105 } 106 } else { 107 this.a = Math.min(a,b); 108 this.b = Math.max(a,b); 109 110 type = RANGE; 111 } 112 } 113 114 120 public boolean canMergeWith(SequenceEntry s ) { 121 if( s.type == ALL || this.type == ALL ) return true; 122 123 if( this.type == OPEN_RANGE && (s.b >= this.b || s.b == STAR)) return true; 125 if( s.type == OPEN_RANGE && (this.b >= s.b || this.b == STAR)) return true; 126 127 if( s.b == STAR || this.b == STAR ) return false; 130 131 if( this.type == RANGE && ( this.a <= s.a && this.b >= s.a-1 )) return true; 133 if( this.type == RANGE && ( this.a <= s.b-1 && this.b >= s.b )) return true; 134 if( s.type == RANGE && ( s.a <= this.a && s.b >= this.a-1 )) return true; 135 if( s.type == RANGE && ( s.a <= this.b-1 && s.b >= this.b )) return true; 136 137 return (this.a - s.a) * (this.a - s.a) <= 1 || (this.b - s.b) * (this.b - s.b) <= 1; 138 } 139 140 147 public void merge(SequenceEntry s) { 148 if( s.type == ALL || this.type == ALL ) { 149 this.type = ALL; 150 return; 151 } 152 153 if( this.type == OPEN_RANGE && s.b >= this.a - 1) { 154 this.type = OPEN_RANGE; 155 this.a = this.b = Math.min(s.a,this.a); 156 if( this.a == 1) type = ALL; 157 } 158 159 if( s.type == OPEN_RANGE && this.b >= s.a - 1) { 160 this.type = OPEN_RANGE; 161 this.a = this.b = Math.min(s.a,this.a); 162 if( this.a == 1) type = ALL; 163 return; 164 } 165 166 if( this.type == RANGE && ( this.a <= s.a || this.b >= s.b )) { 167 this.a = s.a<this.a?s.a:this.a; 168 this.b = s.b>this.b?s.b:this.b; 169 return; 170 } 171 172 if( s.type == RANGE && ( s.a <= this.a || s.b >= this.b )) { 173 this.type = RANGE; 174 this.a = s.a<this.a?s.a:this.a; 175 this.b = s.b>this.b?s.b:this.b; 176 return; 177 } 178 179 this.type = RANGE; 180 this.a = s.a<this.a?s.a:this.a; 181 this.b = s.b>this.b?s.b:this.b; 182 } 183 184 187 public int compareTo(Object o) { 188 SequenceEntry s = (SequenceEntry) o; 189 190 if( type == ALL ) return -1; 193 if( s.type == ALL ) return 1; 194 195 199 if( s.a == STAR ) return -1; 202 if( a == STAR ) return 1; 203 204 206 return a<s.a?-1:1; 207 } 208 211 public String toString() { 212 switch( type ) { 213 case ALL : return "1:*"; 214 215 case SINGLE : { 216 if( a == STAR ) { 217 return "*"; 218 } else { 219 return Integer.toString(a); 220 } 221 } 222 223 case RANGE : return Integer.toString(a) + ':' + Integer.toString(b); 224 225 case OPEN_RANGE : return Integer.toString(a) + ":*"; 226 } 227 228 return null; 229 } 230 233 public int getA() { 234 return a; 235 } 236 239 public int getB() { 240 return b; 241 } 242 245 public int getType() { 246 return type; 247 } 248 } 249 | Popular Tags |