1 57 58 package org.apache.commons.jrcs.diff; 59 60 import java.util.ArrayList ; 61 import java.util.Arrays ; 62 import java.util.Iterator ; 63 import java.util.List ; 64 65 74 public class Chunk 75 extends org.apache.commons.jrcs.util.ToString 76 { 77 78 protected int anchor; 79 80 protected int count; 81 82 protected List chunk; 83 84 89 public Chunk(int pos, int count) 90 { 91 this.anchor = pos; 92 this.count = (count >= 0 ? count : 0); 93 } 94 95 101 public Chunk(Object [] iseq, int pos, int count) 102 { 103 this(pos, count); 104 chunk = slice(iseq, pos, count); 105 } 106 107 115 public Chunk(Object [] iseq, int pos, int count, int offset) 116 { 117 this(offset, count); 118 chunk = slice(iseq, pos, count); 119 } 120 121 127 public Chunk(List iseq, int pos, int count) 128 { 129 this(pos, count); 130 chunk = slice(iseq, pos, count); 131 } 132 133 141 public Chunk(List iseq, int pos, int count, int offset) 142 { 143 this(offset, count); 144 chunk = slice(iseq, pos, count); 145 } 146 147 151 public int anchor() 152 { 153 return anchor; 154 } 155 156 160 public int size() 161 { 162 return count; 163 } 164 165 168 public int first() 169 { 170 return anchor(); 171 } 172 173 176 public int last() 177 { 178 return anchor() + size() - 1; 179 } 180 181 184 public int rcsfrom() 185 { 186 return anchor + 1; 187 } 188 189 192 public int rcsto() 193 { 194 return anchor + count; 195 } 196 197 201 public List chunk() 202 { 203 return chunk; 204 } 205 206 212 public boolean verify(List target) 213 { 214 if (chunk == null) 215 { 216 return true; 217 } 218 if (last() > target.size()) 219 { 220 return false; 221 } 222 for (int i = 0; i < count; i++) 223 { 224 if (!target.get(anchor + i).equals(chunk.get(i))) 225 { 226 return false; 227 } 228 } 229 return true; 230 } 231 232 236 public void applyDelete(List target) 237 { 238 for (int i = last(); i >= first(); i--) 239 { 240 target.remove(i); 241 } 242 } 243 244 249 public void applyAdd(int start, List target) 250 { 251 Iterator i = chunk.iterator(); 252 while (i.hasNext()) 253 { 254 target.add(start++, i.next()); 255 } 256 } 257 258 262 public void toString(StringBuffer s) 263 { 264 toString(s, "", ""); 265 } 266 267 274 public StringBuffer toString(StringBuffer s, String prefix, String postfix) 275 { 276 if (chunk != null) 277 { 278 Iterator i = chunk.iterator(); 279 while (i.hasNext()) 280 { 281 s.append(prefix); 282 s.append(i.next()); 283 s.append(postfix); 284 } 285 } 286 return s; 287 } 288 289 296 public static List slice(List seq, int pos, int count) 297 { 298 if (count <= 0) 299 { 300 return new ArrayList (seq.subList(pos, pos)); 301 } 302 else 303 { 304 return new ArrayList (seq.subList(pos, pos + count)); 305 } 306 } 307 308 315 public static List slice(Object [] seq, int pos, int count) 316 { 317 return slice(Arrays.asList(seq), pos, count); 318 } 319 320 323 public String rangeString() 324 { 325 StringBuffer result = new StringBuffer (); 326 rangeString(result); 327 return result.toString(); 328 } 329 330 334 public void rangeString(StringBuffer s) 335 { 336 rangeString(s, ","); 337 } 338 339 344 public void rangeString(StringBuffer s, String separ) 345 { 346 if (size() <= 1) 347 { 348 s.append(Integer.toString(rcsfrom())); 349 } 350 else 351 { 352 s.append(Integer.toString(rcsfrom())); 353 s.append(separ); 354 s.append(Integer.toString(rcsto())); 355 } 356 } 357 } | Popular Tags |