Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

method: optional operation

 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

Some methods has in its documentation: (optional operation)
For example you can take a look in http://java.sun.com/javase/6/docs/api/java/util/List.html#add(E)

What does it mean?

Thanks!
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64847
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means that an implementor can provide the operation if appropriate, but can also not provide the operation and still be compliant with the intention of the interface.

Of course, the optional method still has to be implemented for the implementing class to compile, but such methods generally throw an OperationNotSupportedException.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:It means that an implementor can provide the operation if appropriate, but can also not provide the operation and still be compliant with the intention of the interface.

Of course, the optional method still has to be implemented for the implementing class to compile, but such methods generally throw an OperationNotSupportedException.

Could you provide an example?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An example of what?

Implement the interface, but throw an UOE for the optional operations--not difficult.
 
Campbell Ritchie
Sheriff
Pie
Posts: 48971
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear means this Exception.
 
Rob Spoor
Sheriff
Pie
Posts: 20532
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Leandro Coutinho wrote:
Bear Bibeault wrote:It means that an implementor can provide the operation if appropriate, but can also not provide the operation and still be compliant with the intention of the interface.

Of course, the optional method still has to be implemented for the implementing class to compile, but such methods generally throw an OperationNotSupportedException.

Could you provide an example?

The List returned by Collections.unmodifiableList will throw an exception when you try to add or remove an element from it. The add and remove methods are there, they just don't work.
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're right David, it's not difficult. It's because Bear's first statement confused me a little. :]
Thanks for the link Campbell.

Rob Prime wrote:
Leandro Coutinho wrote:
Bear Bibeault wrote:It means that an implementor can provide the operation if appropriate, but can also not provide the operation and still be compliant with the intention of the interface.

Of course, the optional method still has to be implemented for the implementing class to compile, but such methods generally throw an OperationNotSupportedException.

Could you provide an example?

The List returned by Collections.unmodifiableList will throw an exception when you try to add or remove an element from it. The add and remove methods are there, they just don't work.

Thanks a lot Rob!!!
 
Leandro Coutinho
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Andrea Chiavazza
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I came across this issue when implementing the interface Iterator.
The operation remove() is "optional", and shall you not want to support it, the exception that should be thrown is UnsupportedOperationException, which is a RuntimeException.
You will not be able to throw OperationNotSupportedException because it is a checked exception, unless the interface methods declares that you can do so.
Whenever you come across an "optional" operation just read the API documentation that should specify which exception should be thrown when the operation is not supported.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic