views:

159

answers:

2

This:

Timerange longest = Timerange.longest(breaks);
if (longest.durationInHours() >= MIN_FREE_HOURS)
    return true;

is OK.

But this:

if (Timerange.longest(breaks).durationInHours() >= MIN_FREE_HOURS)
    return true;

gives:

java.lang.ClassCastException

Do you know why?!

For simplicity:

public static final <T extends Timerange> T longest(List<T> timeranges) {
    return timeranges.get(0);
}

Breaks:

List<Duty> breaks = week.substract(weekDuties);
+1  A: 

What happens if you try:

if (((Timerange) Timerange.longest(breaks)).durationInHours() >= MIN_FREE_HOURS)
    return true;

e.g., cast it?

MarkusQ
It is OK, but i need to know WHY? :).
etam
So from that we can conclude that longest isn't returning a Timerange, but something that's compatible with Timerange (otherwise the cast and the assignment would have failed). Why might that be?
MarkusQ
A: 

Presumably somewhere in your code you are getting a warning. Listen to your compiler.

To get details add -Xlint (in particular -Xlint:unchecked) to your javac commandline (or do your development environment's equivalent).

Tom Hawtin - tackline