Etam
Etam

Reputation: 4673

Strange ClassCastException

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);

Upvotes: 1

Views: 345

Answers (2)

Tom Hawtin - tackline
Tom Hawtin - tackline

Reputation: 147164

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).

Upvotes: 0

MarkusQ
MarkusQ

Reputation: 21950

What happens if you try:

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

e.g., cast it?

Upvotes: 1

Related Questions