0

I was looking the javadoc of List to find a method that allow replacement of a element ( set(int, E) ) and I saw that the method is "optional". I searched for a while and found this other question with a great answer from Laurence Gonsalves.

But I still have a question, until now everytime I used any interface, I never expected that a implementation could throw an runtime exception (unless I used it wrong or if it was a bug in the implementation code), even though I had exception when accidently tried to add elements to an immutable List, I didn't realized what was going on.

My question is: if I have an API method that has a List (or some other interface) parameter that has to invoke "optional" operations on that object how should I proceed?

  • Should I avoid using List, and use some implementation as parameter?
  • Should I make a defensive copy of the list, modify it and return that list? (this could change the implementation used by the api client)

I know that the need to change a list received as a parameter in a method is questionable, but it may be necessary

Community
  • 1
  • 1
Rafael Teles
  • 2,708
  • 2
  • 16
  • 32
  • Hi Jarrod. I believe this is not a duplicate question, I'm not asking why the operation is optional, I'm asking how to deal with it. I had linked another question in my own question that explain why it is optional, and as I said in the question I read it... – Rafael Teles Sep 21 '15 at 19:01

1 Answers1

2

The Java libraries themselves consistently answer both of your questions "no." For example, the utility method Collections.shuffle accepts any List, and throws an exception if the List passed to it does not support the necessary operations. In general, sticking with that behavior is consistent with the Java libraries and with predominant development practices: go ahead and use the List as if it were mutable, and if someone passes in a List not supporting the necessary operations, they'll get the exceptions they deserve.

Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Well, that makes sense, so I basically have to make clear in the method documentation that the list must be mutable?! – Rafael Teles Sep 21 '15 at 20:13
  • it's normal that for 'optional' operations the operation can be done another way - such as deleting and inserting instead of replacing an element. Possibly with significant performance penalties and possibly with unwanted side effects. You could consider catching the exception and doing the operation the 'backup' way. As long as you document what your method does in those cases, shouldn't be a problem. – DJClayworth Sep 21 '15 at 20:16
  • @DJClayworth I wouldn't expect that to be the common case -- usually I would expect that if there was another way to support the operation, even an inefficient one, that the implementation would go ahead and use that approach in the method. – Louis Wasserman Sep 21 '15 at 20:29
  • @RafaelTeles: Yup, that's the way to do it. – Louis Wasserman Sep 21 '15 at 20:29