1 8 package org.jahia.sqlprofiler; 9 10 import java.util.ArrayList ; 11 12 23 24 public class StringDiff { 25 26 private java.util.ArrayList leftSequences; 27 private java.util.ArrayList rightSequences; 28 private String left; 29 private String right; 30 private int sequenceCount; 31 private int sameCharCount; 32 33 public StringDiff() { 34 } 35 36 48 public class SequencePos { 49 private int startPos; 50 private int endPos; 51 public SequencePos(int startPos, int endPos) { 52 this.startPos = startPos; 53 this.endPos = endPos; 54 } 55 56 public int getStartPos() { 57 return this.startPos; 58 } 59 60 public int getEndPos() { 61 return this.endPos; 62 } 63 } 64 65 74 public void diff(String left, String right) { 75 int curLeftPos = 0; 76 int curRightPos = 0; 77 this.sameCharCount = 0; 78 this.sequenceCount = 0; 79 80 this.leftSequences = new ArrayList (); 81 this.rightSequences = new ArrayList (); 82 83 this.left = left; 84 this.right = right; 85 86 88 if (left == null) { 89 return; 90 } 91 if (right == null) { 92 return; 93 } 94 95 if (left.length() < 2) { 96 if (right.startsWith(left)) { 97 SequencePos leftSequencePos = new SequencePos(0, left.length()); 98 leftSequences.add(leftSequencePos); 99 rightSequences.add(leftSequencePos); 100 return; 101 } 102 } 103 if (right.length() < 2) { 104 if (left.startsWith(right)) { 105 SequencePos rightSequencePos = new SequencePos(0, right.length()); 106 leftSequences.add(rightSequencePos); 107 rightSequences.add(rightSequencePos); 108 return; 109 } 110 } 111 112 114 if ( 117 ( 118 ( (curLeftPos + 1) < left.length()) && 119 ( (curRightPos + 1) < right.length()) && 120 (left.charAt(curLeftPos + 1) == (right.charAt(curRightPos + 1))) 121 ) || 122 ( 123 ( (curLeftPos + 1) == left.length()) && 124 ( (curRightPos + 1) == right.length()) 125 ) 126 ) { 127 128 int leftSequenceStartPos = curLeftPos; 129 int rightSequenceStartPos = curRightPos; 130 131 while ( (curLeftPos < left.length()) && 132 (curRightPos < right.length()) && 133 (left.charAt(curLeftPos) == right.charAt(curRightPos))) { 134 System.out.print(left.charAt(curLeftPos)); 135 sameCharCount++; 136 curLeftPos++; 137 curRightPos++; 138 } 139 if (curLeftPos != 0) { 140 sequenceCount++; 141 SequencePos newLeftSequencePos = new SequencePos( 142 leftSequenceStartPos, curLeftPos); 143 SequencePos newRightSequencePos = new SequencePos( 144 rightSequenceStartPos, curRightPos); 145 leftSequences.add(newLeftSequencePos); 146 rightSequences.add(newRightSequencePos); 147 } 148 149 } 150 151 while (curLeftPos < left.length()) { 152 int tempRightPos = curRightPos; 155 while ( (tempRightPos < right.length() && 156 (left.charAt(curLeftPos) != right.charAt(tempRightPos)))) { 157 tempRightPos++; 158 } 159 if (tempRightPos == right.length()) { 162 curLeftPos++; 166 } else { 167 if ( 170 ( 171 ( (curLeftPos + 1) < left.length()) && 172 ( (tempRightPos + 1) < right.length()) && 173 (left.charAt(curLeftPos + 1) == 174 (right.charAt(tempRightPos + 1))) 175 ) || 176 ( 177 ( (curLeftPos + 1) == left.length()) && 178 ( (tempRightPos + 1) == right.length()) 179 ) 180 ) { 181 System.out.print("?"); 182 curRightPos = tempRightPos; 183 int leftSequenceStartPos = curLeftPos; 184 int rightSequenceStartPos = curRightPos; 185 while ( (curLeftPos < left.length()) && 186 (curRightPos < right.length()) && 187 (left.charAt(curLeftPos) == right.charAt(curRightPos))) { 188 System.out.print(left.charAt(curLeftPos)); 189 sameCharCount++; 190 curLeftPos++; 191 curRightPos++; 192 } 193 SequencePos newLeftSequencePos = new SequencePos( 194 leftSequenceStartPos, curLeftPos); 195 SequencePos newRightSequencePos = new SequencePos( 196 rightSequenceStartPos, curRightPos); 197 leftSequences.add(newLeftSequencePos); 198 rightSequences.add(newRightSequencePos); 199 sequenceCount++; 200 } else { 201 curLeftPos++; 202 } 203 } 204 } 205 206 210 } 211 212 218 public java.util.ArrayList getLeftSequences() { 219 return leftSequences; 220 } 221 222 228 public java.util.ArrayList getRightSequences() { 229 return rightSequences; 230 } 231 232 235 public String getLeft() { 236 return left; 237 } 238 239 242 public String getRight() { 243 return right; 244 } 245 246 249 public int getSequenceCount() { 250 return sequenceCount; 251 } 252 253 257 public int getSameCharCount() { 258 return sameCharCount; 259 } 260 261 private static void displayResults(StringDiff stringDiff) { 262 System.out.println("\nsameCharCount=" + stringDiff.getSameCharCount() + 263 " sequenceCount=" + 264 stringDiff.getSequenceCount()); 265 for (int i = 0; i < stringDiff.getLeftSequences().size(); i++) { 266 SequencePos leftSequencePos = (SequencePos) stringDiff. 267 getLeftSequences().get(i); 268 SequencePos rightSequencePos = (SequencePos) stringDiff. 269 getRightSequences().get(i); 270 System.out.println("sequence " + i + " : left=[" + 271 leftSequencePos.getStartPos() + "," + 272 leftSequencePos.getEndPos() + 273 "]=[" + 274 stringDiff.getLeft().substring(leftSequencePos. 275 getStartPos(), 276 leftSequencePos.getEndPos()) + 277 "] right=[" + rightSequencePos.getStartPos() + 278 "," + 279 rightSequencePos.getEndPos() + "]=[" + 280 stringDiff.getRight().substring(rightSequencePos. 281 getStartPos(), 282 rightSequencePos.getEndPos()) + 283 "]"); 284 } 285 286 } 287 288 292 public static void main(String [] args) { 293 StringDiff stringDiff = new StringDiff(); 294 295 stringDiff.diff("select * from jahia_fields_data where iasdfd_jahia_fields_data=10 order by id_jahia_fields_datba", 297 "select * from jahia_fkjahdflields_data where id_jahiadsfa_fields_data=23 order by id_jahia_fields_data"); 298 displayResults(stringDiff); 299 stringDiff.diff( 300 "select * from jahia_fields_data where id_jahia_fields_data=10 order by", 301 "select * from jahia_fields_data where id_jahia_fields_data=10 order by id_jahia_fields_data"); 302 displayResults(stringDiff); 303 stringDiff.diff("a", 304 "a"); 305 displayResults(stringDiff); 306 stringDiff.diff("", 307 ""); 308 displayResults(stringDiff); 309 stringDiff.diff("", 310 "a"); 311 displayResults(stringDiff); 312 stringDiff.diff("a", 313 ""); 314 displayResults(stringDiff); 315 stringDiff.diff("aa", 316 "a"); 317 displayResults(stringDiff); 318 stringDiff.diff("a", 319 "aa"); 320 displayResults(stringDiff); 321 stringDiff.diff("aa", 322 "aa"); 323 displayResults(stringDiff); 324 stringDiff.diff(null, 325 null); 326 displayResults(stringDiff); 327 stringDiff.diff("a", 328 null); 329 displayResults(stringDiff); 330 stringDiff.diff(null, 331 "a"); 332 displayResults(stringDiff); 333 334 } 335 336 }
| Popular Tags
|