blob: faa35a60fe08e1cb45de24965f0cbdf1b89a0959 (
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
|
/*******************************************************************************
* Copyright (c) 2004, 2007 Boeing.
* 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:
* Boeing - initial API and implementation
*******************************************************************************/
package org.eclipse.osee.orcs.utility;
import java.util.Comparator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.osee.framework.core.data.Named;
public class NameComparator implements Comparator<Named> {
private static final int NUMBER_STRING_LIMIT = 19;
private static final Pattern numberPattern = Pattern.compile("[+-]?\\d+");
private final Matcher numberMatcher = numberPattern.matcher("");
private SortOrder orderType = SortOrder.ASCENDING;
public NameComparator(SortOrder orderType) {
this.orderType = orderType;
}
private String getName(Named name) {
String nameString = name != null ? name.getName() : "";
return nameString != null ? nameString : "";
}
@Override
public int compare(Named o1, Named o2) {
String name1 = getName(o1);
String name2 = getName(o2);
numberMatcher.reset(name1);
if (numberMatcher.matches()) {
numberMatcher.reset(name2);
if (numberMatcher.matches()) {
if ((name1.length() < NUMBER_STRING_LIMIT) && (name2.length() < NUMBER_STRING_LIMIT)) {
if (orderType.isAscending()) {
return Long.valueOf(name1).compareTo(Long.valueOf(name2));
} else {
return Long.valueOf(name2).compareTo(Long.valueOf(name1));
}
}
}
}
if (orderType.isAscending()) {
return name1.compareTo(name2);
} else {
return name2.compareTo(name1);
}
}
}
|