61fb2088614cbfdc47a7a3844cd967800e9df687
[debian/jabref.git] / src / java / net / sf / jabref / FieldComparator.java
1 package net.sf.jabref;
2
3 import java.util.Comparator;
4
5 /**
6  * 
7  * A comparator for BibtexEntry fields
8  * 
9  * Initial Version:
10  * 
11  * @author alver
12  * @version Date: Oct 13, 2005 Time: 10:10:04 PM To
13  * 
14  * Current Version:
15  * 
16  * @author $Author: mortenalver $
17  * @version $Revision: 2165 $ ($Date: 2007-07-07 17:21:54 +0200 (Sat, 07 Jul 2007) $)
18  * 
19  * TODO: Testcases
20  * 
21  */
22 public class FieldComparator implements Comparator {
23
24         String field;
25
26         boolean isNameField, isTypeHeader, isYearField, isMonthField;
27
28         int multiplier;
29
30         public FieldComparator(String field) {
31                 this(field, false);
32         }
33
34         public FieldComparator(String field, boolean reversed) {
35                 this.field = field;
36                 multiplier = reversed ? -1 : 1;
37                 isTypeHeader = field.equals(GUIGlobals.TYPE_HEADER);
38
39                 isNameField = (field.equals("author") || field.equals("editor"));
40                 isYearField = field.equals("year");
41                 isMonthField = field.equals("month");
42         }
43
44         public int compare(Object o1, Object o2) {
45                 BibtexEntry e1 = (BibtexEntry) o1, e2 = (BibtexEntry) o2;
46
47                 Object f1, f2;
48
49                 if (isTypeHeader) {
50                         // Sort by type.
51                         f1 = e1.getType().getName();
52                         f2 = e2.getType().getName();
53                 } else {
54
55                         // If the field is author or editor, we rearrange names so they are
56                         // sorted according to last name.
57                         f1 = e1.getField(field);
58                         f2 = e2.getField(field);
59                 }
60
61                 /*
62                  * [ 1598777 ] Month sorting
63                  * 
64                  * http://sourceforge.net/tracker/index.php?func=detail&aid=1598777&group_id=92314&atid=600306
65                  */
66                 int localMultiplier = multiplier;
67                 if (isMonthField)
68                         localMultiplier = -localMultiplier;
69                 
70                 // Catch all cases involving null:
71                 if (f1 == null)
72                         return f2 == null ? 0 : localMultiplier;
73
74                 if (f2 == null)
75                         return -localMultiplier;
76
77                 // Now we now that both f1 and f2 are != null
78                 if (isNameField) {
79                         if (f1 != null)
80                                 f1 = AuthorList.fixAuthorForAlphabetization((String) f1);
81                         if (f2 != null)
82                                 f2 = AuthorList.fixAuthorForAlphabetization((String) f2);
83                 } else if (isYearField) {
84                         /*
85                          * [ 1285977 ] Impossible to properly sort a numeric field
86                          * 
87                          * http://sourceforge.net/tracker/index.php?func=detail&aid=1285977&group_id=92314&atid=600307
88                          */
89                         f1 = Util.toFourDigitYear((String) f1);
90                         f2 = Util.toFourDigitYear((String) f2);
91                 } else if (isMonthField) {
92                         /*
93                          * [ 1535044 ] Month sorting
94                          * 
95                          * http://sourceforge.net/tracker/index.php?func=detail&aid=1535044&group_id=92314&atid=600306
96                          */
97                         f1 = new Integer(Util.getMonthNumber((String)f1));                      
98                         f2 = new Integer(Util.getMonthNumber((String)f2));
99                 }
100
101                 int result = 0;
102                 if ((f1 instanceof Integer) && (f2 instanceof Integer)) {
103                         result = -(((Integer) f1).compareTo((Integer) f2));
104                 } else if (f2 instanceof Integer) {
105                         Integer f1AsInteger = new Integer(f1.toString());
106                         result = -((f1AsInteger).compareTo((Integer) f2));
107                 } else if (f1 instanceof Integer) {
108                         Integer f2AsInteger = new Integer(f2.toString());
109                         result = -(((Integer) f1).compareTo(f2AsInteger));
110                 } else {
111                         String ours = ((String) f1).toLowerCase(), theirs = ((String) f2).toLowerCase();
112             result = ours.compareTo(theirs);
113                 }
114
115                 return result * localMultiplier;
116         }
117
118         /**
119          * Returns the field this Comparator compares by.
120          * 
121          * @return The field name.
122          */
123         public String getFieldName() {
124                 return field;
125         }
126 }