java.util.Calendar#hashCode ( )源码实例Demo

下面列出了java.util.Calendar#hashCode ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: openjdk-jdk9   文件: CalendarRegression.java

/**
 * 4080631: Calendar.hashCode is amazingly bad
 */
public void Test4080631() {
    Calendar cal = Calendar.getInstance();
    int h1 = cal.hashCode();
    cal.add(SECOND, +1);
    int h2 = cal.hashCode();
    Calendar cal2 = (Calendar) cal.clone();
    cal.add(MILLISECOND, +1);
    int h3 = cal.hashCode();
    logln("hash code: h1=" + h1 + ", h2=" + h2 + ", h3=" + h3);
    if (h1 == h2 || h1 == h3 || h2 == h3) {
        errln("hash code is poor: hashCode=" + h1);
    }
    h2 = cal2.hashCode();
    cal.add(MILLISECOND, -1);
    int h4 = cal.hashCode();
    logln("hash code: h2=" + h2 + ", h4=" + h4);
    if (cal.equals(cal2) && h2 != h4) {
        errln("broken hash code: h2=" + h2 + ", h4=" + h4);
    }
    int x = cal.getFirstDayOfWeek() + 3;
    if (x > SATURDAY) {
        x -= 7;
    }
    cal.setFirstDayOfWeek(x);
    int h5 = cal.hashCode();
    logln("hash code: h4=" + h4 + ", h5=" + h5);
    if (h4 == h5) {
        errln("has code is poor with first day of week param: hashCode=" + h4);
    }
}
 
源代码2 项目: openjdk-jdk9   文件: CalendarRegression.java

/**
 * Calendar and GregorianCalendar hashCode() methods need improvement.
 * Calendar needs a good implementation that subclasses can override,
 * and GregorianCalendar should use that implementation.
 */
public void Test4136399() {
    /* Note: This test is actually more strict than it has to be.
    * Technically, there is no requirement that unequal objects have
    * unequal hashes.  We only require equal objects to have equal hashes.
    * It is desirable for unequal objects to have distributed hashes, but
    * there is no hard requirement here.
    *
    * In this test we make assumptions about certain attributes of calendar
    * objects getting represented in the hash, which need not always be the
    * case (although it does work currently with the given test). */
    Calendar a = Calendar.getInstance();
    Calendar b = (Calendar) a.clone();
    if (a.hashCode() != b.hashCode()) {
        errln("Calendar hash code unequal for cloned objects");
    }

    b.setMinimalDaysInFirstWeek(7 - a.getMinimalDaysInFirstWeek());
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores minimal days in first week");
    }
    b.setMinimalDaysInFirstWeek(a.getMinimalDaysInFirstWeek());

    b.setFirstDayOfWeek((a.getFirstDayOfWeek() % 7) + 1); // Next day
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores first day of week");
    }
    b.setFirstDayOfWeek(a.getFirstDayOfWeek());

    b.setLenient(!a.isLenient());
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores lenient setting");
    }
    b.setLenient(a.isLenient());

    // Assume getTimeZone() returns a reference, not a clone
    // of a reference -- this is true as of this writing
    b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset() + 60 * 60 * 1000);
    if (a.hashCode() == b.hashCode()) {
        errln("Calendar hash code ignores zone");
    }
    b.getTimeZone().setRawOffset(a.getTimeZone().getRawOffset());

    GregorianCalendar c = new GregorianCalendar();
    GregorianCalendar d = (GregorianCalendar) c.clone();
    if (c.hashCode() != d.hashCode()) {
        errln("GregorianCalendar hash code unequal for clones objects");
    }
    Date cutover = c.getGregorianChange();
    d.setGregorianChange(new Date(cutover.getTime() + 24 * 60 * 60 * 1000));
    if (c.hashCode() == d.hashCode()) {
        errln("GregorianCalendar hash code ignores cutover");
    }
}