1

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); 
4
  • How about showing a complete example which exhibits the problem? In particular, the declaration of breaks is missing. Commented Mar 15, 2009 at 8:06
  • List<Duty> breaks = week.substract(weekDuties); Commented Mar 15, 2009 at 10:38
  • Does Duty extend Timerange? Commented Jul 25, 2010 at 21:21
  • Does Duty.durationInHours differ from Timerange.durationInHours? Commented Jul 27, 2010 at 19:03

2 Answers 2

1

What happens if you try:

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

e.g., cast it?

Sign up to request clarification or add additional context in comments.

2 Comments

It is OK, but i need to know WHY? :).
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?
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.