KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > VersionCollator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui;
12
13  
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18 /**
19  * Collator to compare two CVS revisions
20  */

21 public class VersionCollator {
22     public int compare(String JavaDoc revision1, String JavaDoc 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 JavaDoc string) {
42         int size = string.length();
43         if (size == 0) return new int[0];
44         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
45         List JavaDoc list = new ArrayList JavaDoc();
46         for (int i = 0; i < size; i++) {
47             char ch = string.charAt(i);
48             if (ch == '.') {
49                 list.add(new Integer JavaDoc(buffer.toString()));
50                 buffer = new StringBuffer JavaDoc();
51             } else {
52                 buffer.append(ch);
53             }
54         }
55         list.add(new Integer JavaDoc(buffer.toString()));
56         int[] result = new int[list.size()];
57         Iterator JavaDoc it = list.iterator();
58         for (int i = 0; i < result.length; i++) {
59             result[i] = ((Integer JavaDoc)it.next()).intValue();
60         }
61         return result;
62     }
63 }
64
65
Popular Tags