// toInstant() int N = 1000; for (TimeUnit unit : EnumSet.allOf(TimeUnit.class)) { for (int i = 0; i < N; i++) { long value = rand.nextLong();
FileTime ft = FileTime.from(value, unit);
Instant instant = ft.toInstant(); if (instant != Instant.MIN && instant != Instant.MAX) {
eqTime(value, unit, instant);
}
}
} for (TimeUnit unit : EnumSet.allOf(TimeUnit.class)) { long value = Long.MIN_VALUE;
FileTime ft = FileTime.from(value, unit);
Instant instant = ft.toInstant(); if (unit.compareTo(TimeUnit.SECONDS) < 0) {
eqTime(value, unit, instant);
} elseif (!instant.equals(Instant.MIN)) { thrownew RuntimeException("should overflow to MIN");
}
value = Long.MAX_VALUE;
ft = FileTime.from(value, unit);
instant = ft.toInstant(); if (unit.compareTo(TimeUnit.SECONDS) < 0) {
eqTime(value, unit, instant);
} elseif (!instant.equals(Instant.MAX)) { thrownew RuntimeException("should overflow to MAX");
}
}
// from(Instant) finallong MAX_SECOND = 31556889864403199L; for (int i = 0; i < N; i++) { long v = rand.nextLong(); long secs = v % MAX_SECOND;
Instant instant = Instant.ofEpochSecond(secs, rand.nextInt(1000_000_000));
FileTime ft = FileTime.from(instant); if (!ft.toInstant().equals(instant) || ft.to(SECONDS) != secs) { thrownew RuntimeException("from(Instant) failed");
} long millis = v;
instant = Instant.ofEpochMilli(millis);
ft = FileTime.from(instant); if (!ft.toInstant().equals(instant) ||
ft.toMillis() != instant.toEpochMilli()) { thrownew RuntimeException("from(Instant) failed");
} long nanos = v;
ft = FileTime.from(nanos, NANOSECONDS);
secs = nanos / 1000_000_000;
nanos = nanos % 1000_000_000;
instant = Instant.ofEpochSecond(secs, nanos); if (!ft.equals(FileTime.from(instant))) { thrownew RuntimeException("from(Instant) failed");
}
}
FileTime time = FileTime.fromMillis(now); if (time.equals(null)) thrownew RuntimeException("should not be equal to null"); try {
time.compareTo(null); thrownew RuntimeException("NullPointerException expected");
} catch (NullPointerException npe) { }
staticvoid overflow(long minmax, long v) { if (v != minmax) thrownew RuntimeException("saturates to Long.MIN/MAX_VALUE expected");
}
staticvoid cmp(long v1, TimeUnit u1, long v2, TimeUnit u2, int expected) { int result = FileTime.from(v1, u1).compareTo(FileTime.from(v2, u2)); if (result != expected) thrownew RuntimeException("unexpected order");
}
staticvoid cmp(Instant ins, long v2, TimeUnit u2, int expected) { int result = FileTime.from(ins).compareTo(FileTime.from(v2, u2)); if (result != expected) thrownew RuntimeException("unexpected order");
}
staticvoid eq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
FileTime t1 = FileTime.from(v1, u1);
FileTime t2 = FileTime.from(v2, u2); if (!t1.equals(t2)) thrownew RuntimeException("not equal"); if (t1.hashCode() != t2.hashCode()) thrownew RuntimeException("hashCodes should be equal");
}
staticvoid eq(Instant ins, long v2, TimeUnit u2) {
FileTime t1 = FileTime.from(ins);
FileTime t2 = FileTime.from(v2, u2); if (!t1.equals(t2)) thrownew RuntimeException("not equal"); if (t1.hashCode() != t2.hashCode()) thrownew RuntimeException("hashCodes should be equal");
}
staticvoid eqTime(long value, TimeUnit unit, Instant instant) { long secs = SECONDS.convert(value, unit); long nanos = NANOSECONDS.convert(value - unit.convert(secs, SECONDS), unit); if (nanos < 0) { // normalize nanoOfSecond to positive
secs -= 1;
nanos += 1000_000_000;
} if (secs != instant.getEpochSecond() || (int)nanos != instant.getNano()) {
System.err.println(" ins=" + instant); thrownew RuntimeException("ft and instant are not the same time point");
}
}
staticvoid neq(long v1, TimeUnit u1, long v2, TimeUnit u2) {
FileTime t1 = FileTime.from(v1, u1);
FileTime t2 = FileTime.from(v2, u2); if (t1.equals(t2)) thrownew RuntimeException("should not be equal");
}
staticvoid neq(Instant ins, long v2, TimeUnit u2) {
FileTime t1 = FileTime.from(ins);
FileTime t2 = FileTime.from(v2, u2); if (t1.equals(t2)) thrownew RuntimeException("should not be equal");
}
staticvoid to(long v, TimeUnit unit) {
FileTime t = FileTime.from(v, unit); for (TimeUnit u: TimeUnit.values()) { long result = t.to(u); long expected = u.convert(v, unit); if (result != expected) { thrownew RuntimeException("unexpected result");
}
}
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.