aspose file tools*
The moose likes Java in General and the fly likes method: optional operation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "method: optional operation" Watch "method: optional operation" New topic
Author

method: optional operation

Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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

Joined: Jan 10, 2002
Posts: 61766
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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

Joined: Sep 29, 2008
Posts: 12617

An example of what?

Implement the interface, but throw an UOE for the optional operations--not difficult.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40052
    
  28
Bear means this Exception.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19784
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Leandro Coutinho
Ranch Hand

Joined: Mar 04, 2009
Posts: 417
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

Joined: Mar 04, 2009
Posts: 417
Andrea Chiavazza
Greenhorn

Joined: Jul 02, 2010
Posts: 14
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method: optional operation