Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ef7fa42a6b2337f06d6ae79413b1b291fe521827 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*******************************************************************************
 * Copyright (c) 2000, 2006 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui;

 
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Collator to compare two CVS revisions
 */
public class VersionCollator {
	public int compare(String revision1, String revision2) {
		if (revision1 == null && revision2 == null) return 0;
		if (revision1 == null) return -1;
		if (revision2 == null) return 1;
		int[] revision1Segments = getIntSegments(revision1);
		int[] revision2Segments = getIntSegments(revision2);
		for (int i = 0; i < revision1Segments.length && i < revision2Segments.length; i++) {
			int i1 = revision1Segments[i];
			int i2 = revision2Segments[i];
			if (i1 != i2) {
				return i1 > i2 ? 1 : -1;
			}
		}
		if (revision1Segments.length != revision2Segments.length) {
			return revision1Segments.length > revision2Segments.length ? 1 : -1;
		}
		return 0;
	}
	
	int[] getIntSegments(String string) {
		int size = string.length();
		if (size == 0) return new int[0];
		StringBuffer buffer = new StringBuffer();
		List list = new ArrayList();
		for (int i = 0; i < size; i++) {
			char ch = string.charAt(i);
			if (ch == '.') {
				list.add(Integer.valueOf(buffer.toString()));
				buffer = new StringBuffer();
			} else {
				buffer.append(ch);
			}
		}
		list.add(Integer.valueOf(buffer.toString()));
		int[] result = new int[list.size()];
		Iterator it = list.iterator();
		for (int i = 0; i < result.length; i++) {
			result[i] = ((Integer)it.next()).intValue();
		}
		return result;
	}
}

Back to the top