77e58ea04bf4f0bc1b59d401a161c0e5f2be89a9
[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: coezbek $
17  * @version $Revision: 1.5 $ ($Date: 2006/09/02 00:03:48 $)
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                 // Catch all cases involving null:
62                 if (f1 == null)
63                         return f2 == null ? 0 : multiplier;
64
65                 if (f2 == null)
66                         return -multiplier;
67
68                 // Now we now that both f1 and f2 are != null
69                 if (isNameField) {
70                         if (f1 != null)
71                                 f1 = AuthorList.fixAuthorForAlphabetization((String) f1);
72                         if (f2 != null)
73                                 f2 = AuthorList.fixAuthorForAlphabetization((String) f2);
74                 } else if (isYearField) {
75                         /*
76                          * [ 1285977 ] Impossible to properly sort a numeric field
77                          * 
78                          * http://sourceforge.net/tracker/index.php?func=detail&aid=1285977&group_id=92314&atid=600307
79                          */
80                         f1 = Util.toFourDigitYear((String) f1);
81                         f2 = Util.toFourDigitYear((String) f2);
82                 } else if (isMonthField) {
83                         /*
84                          * [ 1535044 ] Month sorting
85                          * 
86                          * http://sourceforge.net/tracker/index.php?func=detail&aid=1535044&group_id=92314&atid=600306
87                          */
88                         f1 = new Integer(Util.getMonthNumber((String)f1));                      
89                         f2 = new Integer(Util.getMonthNumber((String)f2));
90                         // Somehow this is twisted
91                         multiplier = -multiplier;
92                 }
93
94                 int result = 0;
95                 if ((f1 instanceof Integer) && (f2 instanceof Integer)) {
96                         result = -(((Integer) f1).compareTo((Integer) f2));
97                 } else if (f2 instanceof Integer) {
98                         Integer f1AsInteger = new Integer(f1.toString());
99                         result = -((f1AsInteger).compareTo((Integer) f2));
100                 } else if (f1 instanceof Integer) {
101                         Integer f2AsInteger = new Integer(f2.toString());
102                         result = -(((Integer) f1).compareTo(f2AsInteger));
103                 } else {
104                         String ours = ((String) f1).toLowerCase(), theirs = ((String) f2).toLowerCase();
105                         result = ours.compareTo(theirs);
106                 }
107
108                 return result * multiplier;
109         }
110
111         /**
112          * Returns the field this Comparator compares by.
113          * 
114          * @return The field name.
115          */
116         public String getFieldName() {
117                 return field;
118         }
119 }