Java日期属性比较器合同违规

时间:2018-10-31 05:20:23

标签: java apache-spark mapreduce comparator

我有一个对象列表,要按日期降序排序(最新的优先)。 Foo类具有String参数“ createdAt”,该参数用于定义与方法覆盖的比较。

“ createdAt”可以为null,空白或无效日期。

@Override
public int compareTo(Foo foo) {
    Date created = new Date(0);
    Date otherCreated = new Date(0);

    try {
        created = Utils.getFormattedDate(createdAt);

    } catch (Exception e) {
        e.printStackTrace();
    }
    try {
        otherCreated  = Utils.getFormattedDate(foo.createdAt);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Long.compare(otherCreated.getTime(),created.getTime());

}

对于null检查,我已经定义了一个比较器

public static final Comparator fooComparator = new Comparator<Foo>() {
    @Override
    public int compare(Foo o1, Foo o2) {
        if (o1 ==null || o1.getCreatedAt() == null) {
            return (o2 ==null || o2.getCreatedAt() == null) ? 0 : 1;
        }
        if (o2==null || o2.getCreatedAt() == null) {
            return -1;
        }
        return o1.compareTo(o2);
    }
};

我尝试使用

ArrayList <Foo> fooList = ....
Collections.sort(fooList, fooComparator);

但是在火花上在MapReduce作业中运行此代码时,我得到“比较方法违反了它的一般约定”

我试图编写许多可传递的测试用例,但它们都通过了。我想念什么?

这是我的考试

@org.junit.Test
public void testCompare_Contract() {

    Foo t2 = new Foo();
    t2.setId(2L);
    t2.setCreatedAt(formatter.format(new Date(2018, 10, 28, 0, 0, 0).getTime()));
    t2.setText("Foo 2");

    Foo t3 = new Foo();
    t3.setId(3L);
    t3.setCreatedAt(null);
    t3.setText("Foo 3");

    Foo t4 = new Foo();
    t4.setId(4L);
    t4.setCreatedAt(formatter.format(new Date(2018, 10, 30, 0, 0, 0).getTime()));
    t4.setText("Foo 4");


    assertEquals(1, t3.compareTo(t2));
    assertEquals(-1, t2.compareTo(t3));

    assertEquals(1, t3.compareTo(t4));
    assertEquals(-1, t4.compareTo(t3));

    assertEquals(1, t2.compareTo(t4));
    assertEquals(-1, t4.compareTo(t2));

    assertEquals(0, t2.compareTo(t2));

}

0 个答案:

没有答案