create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / motor / DesignationComparator.java
1 package net.sf.openrocket.motor;
2
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;
8
9 /**
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.
13  * 
14  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
15  */
16 public class DesignationComparator implements Comparator<String> {
17         private static final Collator COLLATOR;
18         static {
19                 COLLATOR = Collator.getInstance(Locale.US);
20                 COLLATOR.setStrength(Collator.PRIMARY);
21         }
22
23         private Pattern pattern = 
24                 Pattern.compile("^([0-9][0-9]+|1/([1-8]))?([a-zA-Z])([0-9]+)(.*?)$");
25         
26         @Override
27         public int compare(String o1, String o2) {
28                 int value;
29                 Matcher m1, m2;
30                 
31                 m1 = pattern.matcher(o1);
32                 m2 = pattern.matcher(o2);
33                 
34                 if (m1.find() && m2.find()) {
35
36                         String o1Class = m1.group(3);
37                         int o1Thrust = Integer.parseInt(m1.group(4));
38                         String o1Extra = m1.group(5);
39                         
40                         String o2Class = m2.group(3);
41                         int o2Thrust = Integer.parseInt(m2.group(4));
42                         String o2Extra = m2.group(5);
43                         
44                         // 1. Motor class
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);
49
50                                 if (sub1 != null || sub2 != null) {
51                                         if (sub1 == null)
52                                                 sub1 = "1";
53                                         if (sub2 == null)
54                                                 sub2 = "1";
55                                         value = -COLLATOR.compare(sub1,sub2);
56                                         if (value != 0)
57                                                 return value;
58                                 }
59                         }
60                         value = COLLATOR.compare(o1Class,o2Class);
61                         if (value != 0)
62                                 return value;
63                         
64                         // 2. Average thrust
65                         if (o1Thrust != o2Thrust)
66                                 return o1Thrust - o2Thrust;
67                         
68                         // 3. Extra modifier
69                         return COLLATOR.compare(o1Extra, o2Extra);
70                         
71                 } else {
72                         
73                         // Not understandable designation, simply compare strings
74                         return COLLATOR.compare(o1, o2);
75                 }
76         }
77 }