Repository: jabref/jabref
Issue: Year sorting doesn't work

Clicking on the year column doesn't affect the sorting of the table.


Changed files:
src/test/java/net/sf/jabref/model/entry/YearUtilTest.java, src/main/java/net/sf/jabref/model/entry/BibEntry.java, src/main/java/net/sf/jabref/bibtex/comparator/FieldComparator.java, src/test/java/net/sf/jabref/model/entry/BibtexEntryTests.java, src/main/java/net/sf/jabref/model/entry/YearUtil.java

diff --git a/src/main/java/net/sf/jabref/bibtex/comparator/FieldComparator.java b/src/main/java/net/sf/jabref/bibtex/comparator/FieldComparator.java
index c3058ec841..43cd246174 100644
--- a/src/main/java/net/sf/jabref/bibtex/comparator/FieldComparator.java
+++ b/src/main/java/net/sf/jabref/bibtex/comparator/FieldComparator.java
@@ -22,7 +22,6 @@ import net.sf.jabref.logic.config.SaveOrderConfig;
 import net.sf.jabref.logic.util.strings.StringUtil;
 import net.sf.jabref.model.entry.AuthorList;
 import net.sf.jabref.model.entry.MonthUtil;
-import net.sf.jabref.model.entry.YearUtil;
 import net.sf.jabref.model.entry.BibEntry;
 
 import java.text.Collator;
@@ -57,8 +56,6 @@ public class FieldComparator implements Comparator<BibEntry> {
         }
     }
 
-
-
     enum FieldType {
         NAME, TYPE, YEAR, MONTH, OTHER;
     }
@@ -139,7 +136,10 @@ public class FieldComparator implements Comparator<BibEntry> {
             f1 = AuthorList.fixAuthorForAlphabetization(f1);
             f2 = AuthorList.fixAuthorForAlphabetization(f2);
         } else if (fieldType == FieldType.YEAR) {
-            return Integer.compare(YearUtil.toFourDigitYearWithInts(f1), YearUtil.toFourDigitYearWithInts(f2)) * multiplier;
+            Integer f1year = StringUtil.intValueOfWithNull(f1);
+            Integer f2year = StringUtil.intValueOfWithNull(f2);
+            int comparisonResult = Integer.compare(f1year == null ? 0 : f1year, f2year == null ? 0 : f2year);
+            return comparisonResult * multiplier;
         } else if (fieldType == FieldType.MONTH) {
             return Integer.compare(MonthUtil.getMonth(f1).number, MonthUtil.getMonth(f2).number) * multiplier;
         }
diff --git a/src/main/java/net/sf/jabref/model/entry/BibEntry.java b/src/main/java/net/sf/jabref/model/entry/BibEntry.java
index 3091ca2fd1..5ff8a809fa 100644
--- a/src/main/java/net/sf/jabref/model/entry/BibEntry.java
+++ b/src/main/java/net/sf/jabref/model/entry/BibEntry.java
@@ -497,7 +497,7 @@ public class BibEntry {
             return null;
         }
 
-        String year = YearUtil.toFourDigitYear(getField("year"));
+        String year = getField("year");
 
         if (hasField("month")) {
             MonthUtil.Month month = MonthUtil.getMonth(getField("month"));
diff --git a/src/main/java/net/sf/jabref/model/entry/YearUtil.java b/src/main/java/net/sf/jabref/model/entry/YearUtil.java
deleted file mode 100644
index 23e1f3da1d..0000000000
--- a/src/main/java/net/sf/jabref/model/entry/YearUtil.java
+++ /dev/null
@@ -1,124 +0,0 @@
-package net.sf.jabref.model.entry;
-
-import java.util.Calendar;
-
-public class YearUtil {
-
-    private static final int CURRENT_YEAR = Calendar.getInstance().get(Calendar.YEAR);
-
-    /**
-     * Will convert a two digit year using the following scheme (describe at
-     * http://www.filemaker.com/help/02-Adding%20and%20view18.html):
-     * <p/>
-     * If a two digit year is encountered they are matched against the last 69
-     * years and future 30 years.
-     * <p/>
-     * For instance if it is the year 1992 then entering 23 is taken to be 1923
-     * but if you enter 23 in 1993 then it will evaluate to 2023.
-     *
-     * @param year The year to convert to 4 digits.
-     * @return
-     */
-    public static String toFourDigitYear(String year) {
-        return YearUtil.toFourDigitYear(year, YearUtil.CURRENT_YEAR);
-    }
-
-    /**
-     * Will convert a two digit year using the following scheme (describe at
-     * http://www.filemaker.com/help/02-Adding%20and%20view18.html):
-     * <p/>
-     * If a two digit year is encountered they are matched against the last 69
-     * years and future 30 years.
-     * <p/>
-     * For instance if it is the year 1992 then entering 23 is taken to be 1923
-     * but if you enter 23 in 1993 then it will evaluate to 2023.
-     *
-     * @param year The year to convert to 4 digits.
-     * @return
-     */
-    static String toFourDigitYear(String year, int thisYear) {
-        if ((year == null) || (year.length() != 2)) {
-            return year;
-        }
-
-        Integer yearNumber = intValueOfWithNull(year);
-        if (yearNumber == null) {
-            return year;
-        }
-
-        return String.valueOf(new Year(thisYear).toFourDigitYear(yearNumber));
-    }
-
-    public static int toFourDigitYearWithInts(String year) {
-        return YearUtil.toFourDigitYearWithInts(year, YearUtil.CURRENT_YEAR);
-    }
-
-    private static int toFourDigitYearWithInts(String year, int thisYear) {
-        if ((year == null) || (year.length() != 2)) {
-            return 0;
-        }
-
-        Integer yearNumber = intValueOfWithNull(year);
-        if (yearNumber == null) {
-            return 0;
-        }
-
-        return new Year(thisYear).toFourDigitYear(yearNumber);
-    }
-
-    private static Integer intValueOfWithNull(String str) {
-        int idx = 0;
-        int end;
-        boolean sign = false;
-        char ch;
-
-        if ((str == null) || ((end = str.length()) == 0) || ((((ch = str.charAt(0)) < '0') || (ch > '9')) && (!(sign = ch == '-') || (++idx == end) || ((ch = str.charAt(idx)) < '0') || (ch > '9')))) {
-            return null;
-        }
-
-        int ival = 0;
-        for (; ; ival *= 10) {
-            ival += '0' - ch;
-            if (++idx == end) {
-                return sign ? ival : -ival;
-            }
-            if (((ch = str.charAt(idx)) < '0') || (ch > '9')) {
-                return null;
-            }
-        }
-    }
-
-    private static class Year {
-
-        private final int year;
-        private final int century;
-        private final int yearShort;
-
-        public Year(int year) {
-            this.year = year;
-            this.yearShort = this.year % 100;
-            this.century = (this.year / 100) * 100;
-        }
-
-        public int toFourDigitYear(int relativeYear) {
-            if (relativeYear == yearShort) {
-                return this.year;
-            }
-            // 20 , 90
-            // 99 > 30
-            if ((((relativeYear + 100) - yearShort) % 100) > 30) {
-                if (relativeYear < yearShort) {
-                    return century + relativeYear;
-                } else {
-                    return (century - 100) + relativeYear;
-                }
-            } else {
-                if (relativeYear < yearShort) {
-                    return century + 100 + relativeYear;
-                } else {
-                    return century + relativeYear;
-                }
-            }
-        }
-    }
-}
diff --git a/src/test/java/net/sf/jabref/model/entry/BibtexEntryTests.java b/src/test/java/net/sf/jabref/model/entry/BibtexEntryTests.java
index ac3708a80e..85389b8ba2 100644
--- a/src/test/java/net/sf/jabref/model/entry/BibtexEntryTests.java
+++ b/src/test/java/net/sf/jabref/model/entry/BibtexEntryTests.java
@@ -133,7 +133,7 @@ public class BibtexEntryTests {
                 (BibtexParser.singleFromString("@ARTICLE{HipKro03, author={bla}}")).getPublicationDate());
 
         Assert.assertEquals("2003-12",
-                (BibtexParser.singleFromString("@ARTICLE{HipKro03, year = {03}, month = #DEC# }"))
+                (BibtexParser.singleFromString("@ARTICLE{HipKro03, year = {2003}, month = #DEC# }"))
                         .getPublicationDate());
 
     }
diff --git a/src/test/java/net/sf/jabref/model/entry/YearUtilTest.java b/src/test/java/net/sf/jabref/model/entry/YearUtilTest.java
deleted file mode 100644
index bb8c967bad..0000000000
--- a/src/test/java/net/sf/jabref/model/entry/YearUtilTest.java
+++ /dev/null
@@ -1,44 +0,0 @@
-package net.sf.jabref.model.entry;
-
-import org.junit.Assert;
-import org.junit.Test;
-
-import java.text.DecimalFormat;
-import java.text.NumberFormat;
-import java.util.Calendar;
-
-public class YearUtilTest {
-
-    @Test
-    public void test2to4DigitsYear() {
-        Assert.assertEquals("1990", YearUtil.toFourDigitYear("1990"));
-        Assert.assertEquals("190", YearUtil.toFourDigitYear("190"));
-        Assert.assertEquals("1990", YearUtil.toFourDigitYear("90", 1990));
-        Assert.assertEquals("1990", YearUtil.toFourDigitYear("90", 1991));
-        Assert.assertEquals("2020", YearUtil.toFourDigitYear("20", 1990));
-        Assert.assertEquals("1921", YearUtil.toFourDigitYear("21", 1990));
-        Assert.assertEquals("1922", YearUtil.toFourDigitYear("22", 1990));
-        Assert.assertEquals("2022", YearUtil.toFourDigitYear("22", 1992));
-        Assert.assertEquals("1999", YearUtil.toFourDigitYear("99", 2001));
-        Assert.assertEquals("1931", YearUtil.toFourDigitYear("1931", 2001));
-        Assert.assertEquals("2031", YearUtil.toFourDigitYear("31", 2001));
-        Assert.assertEquals("1932", YearUtil.toFourDigitYear("32", 2001));
-        Assert.assertEquals("1944", YearUtil.toFourDigitYear("44", 2001));
-        Assert.assertEquals("2011", YearUtil.toFourDigitYear("11", 2001));
-        Assert.assertEquals("2005a", YearUtil.toFourDigitYear("2005a"));
-        Assert.assertEquals("2005b", YearUtil.toFourDigitYear("2005b"));
-
-        int thisYear = Calendar.getInstance().get(Calendar.YEAR);
-        int d2 = thisYear % 100;
-
-        NumberFormat f = new DecimalFormat("00");
-
-        for (int i = 0; i <= 30; i++) {
-            Assert.assertTrue(String.valueOf(i), thisYear <= Integer.parseInt(YearUtil.toFourDigitYear(f.format((d2 + i) % 100))));
-        }
-        for (int i = 0; i < 70; i++) {
-            Assert.assertTrue(String.valueOf(i), thisYear >= Integer.parseInt(YearUtil.toFourDigitYear(f.format((d2 - i + 100) % 100))));
-        }
-    }
-
-}
\\ No newline at end of file