1 11 package org.eclipse.team.internal.ccvs.ui; 12 13 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 18 21 public class VersionCollator { 22 public int compare(String revision1, String revision2) { 23 if (revision1 == null && revision2 == null) return 0; 24 if (revision1 == null) return -1; 25 if (revision2 == null) return 1; 26 int[] revision1Segments = getIntSegments(revision1); 27 int[] revision2Segments = getIntSegments(revision2); 28 for (int i = 0; i < revision1Segments.length && i < revision2Segments.length; i++) { 29 int i1 = revision1Segments[i]; 30 int i2 = revision2Segments[i]; 31 if (i1 != i2) { 32 return i1 > i2 ? 1 : -1; 33 } 34 } 35 if (revision1Segments.length != revision2Segments.length) { 36 return revision1Segments.length > revision2Segments.length ? 1 : -1; 37 } 38 return 0; 39 } 40 41 int[] getIntSegments(String string) { 42 int size = string.length(); 43 if (size == 0) return new int[0]; 44 StringBuffer buffer = new StringBuffer (); 45 List list = new ArrayList (); 46 for (int i = 0; i < size; i++) { 47 char ch = string.charAt(i); 48 if (ch == '.') { 49 list.add(new Integer (buffer.toString())); 50 buffer = new StringBuffer (); 51 } else { 52 buffer.append(ch); 53 } 54 } 55 list.add(new Integer (buffer.toString())); 56 int[] result = new int[list.size()]; 57 Iterator it = list.iterator(); 58 for (int i = 0; i < result.length; i++) { 59 result[i] = ((Integer )it.next()).intValue(); 60 } 61 return result; 62 } 63 } 64 65 | Popular Tags |