1 57 58 package org.apache.commons.jrcs.diff; 59 60 import java.util.*; 61 62 import org.apache.commons.jrcs.diff.myers.MyersDiff; 63 import org.apache.commons.jrcs.util.ToString; 64 65 99 100 public class Diff 101 extends ToString 102 { 103 104 public static final String NL = System.getProperty("line.separator"); 105 106 107 public static final String RCS_EOL = "\n"; 108 109 110 protected final Object [] orig; 111 112 113 protected DiffAlgorithm algorithm; 114 115 120 public Diff(Object [] original) 121 { 122 this(original, null); 123 } 124 125 131 public Diff(Object [] original, DiffAlgorithm algorithm) 132 { 133 if (original == null) 134 { 135 throw new IllegalArgumentException (); 136 } 137 138 this.orig = original; 139 if (algorithm != null) 140 this.algorithm = algorithm; 141 else 142 this.algorithm = defaultAlgorithm(); 143 } 144 145 protected DiffAlgorithm defaultAlgorithm() 146 { 147 return new MyersDiff(); 148 } 149 150 157 public static Revision diff(Object [] orig, Object [] rev) 158 throws DifferentiationFailedException 159 { 160 if (orig == null || rev == null) 161 { 162 throw new IllegalArgumentException (); 163 } 164 165 return diff(orig, rev, null); 166 } 167 168 176 public static Revision diff(Object [] orig, Object [] rev, 177 DiffAlgorithm algorithm) 178 throws DifferentiationFailedException 179 { 180 if (orig == null || rev == null) 181 { 182 throw new IllegalArgumentException (); 183 } 184 185 return new Diff(orig, algorithm).diff(rev); 186 } 187 188 194 public Revision diff(Object [] rev) 195 throws DifferentiationFailedException 196 { 197 if (orig.length == 0 && rev.length == 0) 198 return new Revision(); 199 else 200 return algorithm.diff(orig, rev); 201 } 202 203 209 public static boolean compare(Object [] orig, Object [] rev) 210 { 211 if (orig.length != rev.length) 212 { 213 return false; 214 } 215 else 216 { 217 for (int i = 0; i < orig.length; i++) 218 { 219 if (!orig[i].equals(rev[i])) 220 { 221 return false; 222 } 223 } 224 return true; 225 } 226 } 227 228 234 public static String arrayToString(Object [] o) 235 { 236 return arrayToString(o, Diff.NL); 237 } 238 239 245 public static Object [] editAll(Object [] text) 246 { 247 Object [] result = new String [text.length]; 248 249 for(int i = 0; i < text.length; i++) 250 result[i] = text[i] + " <edited>"; 251 252 return result; 253 } 254 255 260 public static Object [] randomEdit(Object [] text) 261 { 262 return randomEdit(text, text.length); 263 } 264 265 271 public static Object [] randomEdit(Object [] text, long seed) 272 { 273 List result = new ArrayList(Arrays.asList(text)); 274 Random r = new Random(seed); 275 int nops = r.nextInt(10); 276 for (int i = 0; i < nops; i++) 277 { 278 boolean del = r.nextBoolean(); 279 int pos = r.nextInt(result.size() + 1); 280 int len = Math.min(result.size() - pos, 1 + r.nextInt(4)); 281 if (del && result.size() > 0) 282 { result.subList(pos, pos + len).clear(); 284 } 285 else 286 { 287 for (int k = 0; k < len; k++, pos++) 288 { 289 result.add(pos, 290 "[" + i + "] random edit[" + i + "][" + i + "]"); 291 } 292 } 293 } 294 return result.toArray(); 295 } 296 297 302 public static Object [] shuffle(Object [] text) 303 { 304 return shuffle(text, text.length); 305 } 306 307 313 public static Object [] shuffle(Object [] text, long seed) 314 { 315 List result = new ArrayList(Arrays.asList(text)); 316 Collections.shuffle(result); 317 return result.toArray(); 318 } 319 320 325 public static Object [] randomSequence(int size) 326 { 327 return randomSequence(size, size); 328 } 329 330 336 public static Object [] randomSequence(int size, long seed) 337 { 338 Integer [] result = new Integer [size]; 339 Random r = new Random(seed); 340 for(int i = 0; i < result.length; i++) 341 { 342 result[i] = new Integer (r.nextInt(size)); 343 } 344 return result; 345 } 346 347 } | Popular Tags |