KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > debug > internal > core > sourcelookup > SourceLocatorMementoComparator


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 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.debug.internal.core.sourcelookup;
12
13 import java.util.Comparator JavaDoc;
14
15 /**
16  * Comparator for source locator mementors. Ignores whitespace differences.
17  *
18  * @since 3.0
19  */

20 public class SourceLocatorMementoComparator implements Comparator JavaDoc {
21     /* (non-Javadoc)
22      * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
23      */

24     public int compare(Object JavaDoc o1, Object JavaDoc o2) {
25         String JavaDoc m1 = (String JavaDoc)o1;
26         String JavaDoc m2 = (String JavaDoc)o2;
27         int i1 = 0, i2 = 0;
28         while (i1 < m1.length()) {
29             i1 = skipWhitespace(m1, i1);
30             i2 = skipWhitespace(m2, i2);
31             if (i1 < m1.length() && i2 < m2.length()) {
32                 if (m1.charAt(i1) != m2.charAt(i2)) {
33                     return -1;
34                 }
35                 i1++;
36                 i2++;
37             } else {
38                 if (i2 < m2.length()) {
39                     return -1;
40                 }
41                 return 0;
42             }
43         }
44         return 0;
45     }
46     
47     private int skipWhitespace(String JavaDoc string, int offset) {
48         while (offset < string.length() && Character.isWhitespace(string.charAt(offset))) {
49             offset++;
50         }
51         return offset;
52     }
53 }
54
Popular Tags