1 package net.sf.openrocket.motor;
3 import java.text.Collator;
4 import java.util.Comparator;
5 import java.util.Locale;
6 import java.util.regex.Matcher;
7 import java.util.regex.Pattern;
10 * Compares two motors designations. The motors are ordered first
11 * by their motor class, second by their average thrust and lastly by any
12 * extra modifiers at the end of the designation.
14 * @author Sampo Niskanen <sampo.niskanen@iki.fi>
16 public class DesignationComparator implements Comparator<String> {
17 private static final Collator COLLATOR;
19 COLLATOR = Collator.getInstance(Locale.US);
20 COLLATOR.setStrength(Collator.PRIMARY);
23 private Pattern pattern =
24 Pattern.compile("^([0-9][0-9]+|1/([1-8]))?([a-zA-Z])([0-9]+)(.*?)$");
27 public int compare(String o1, String o2) {
31 m1 = pattern.matcher(o1);
32 m2 = pattern.matcher(o2);
34 if (m1.find() && m2.find()) {
36 String o1Class = m1.group(3);
37 int o1Thrust = Integer.parseInt(m1.group(4));
38 String o1Extra = m1.group(5);
40 String o2Class = m2.group(3);
41 int o2Thrust = Integer.parseInt(m2.group(4));
42 String o2Extra = m2.group(5);
45 if (o1Class.equalsIgnoreCase("A") && o2Class.equalsIgnoreCase("A")) {
46 // 1/2A and 1/4A comparison
47 String sub1 = m1.group(2);
48 String sub2 = m2.group(2);
50 if (sub1 != null || sub2 != null) {
55 value = -COLLATOR.compare(sub1,sub2);
60 value = COLLATOR.compare(o1Class,o2Class);
65 if (o1Thrust != o2Thrust)
66 return o1Thrust - o2Thrust;
69 return COLLATOR.compare(o1Extra, o2Extra);
73 // Not understandable designation, simply compare strings
74 return COLLATOR.compare(o1, o2);