Getting rid of java.util.Iterator -
my problem related java. don't java.util
package because of optional methods. instance, don't that, when return list
, have prevent user modify list; return list implements, say, interface unmodifiablelist
(yes, know collections.unmodifiablelist()
, many reasons, don't encapsulate list). on other hand, when given list external piece of code, list a1) unmodifiable, a2) modifiable side-effects on object returned me list, or a3) modifiable without side-effects, , b1) immutable (assuming elements in list immutable well) or b2) mutable , unsafe use key.
i can rewrite existing classes (just copy/paste java.util
package) , introduce interfaces unmodifiablelist
, modifiablelist
, etc. without effort. however, have following problem: forall mechanism
for (final object o: listofobjects) { // }
which uses class java.lang.iterable
, uses java.util.iterator
.
so problem is, how should cope iterator
class?
i have several possible solutions looking elegant.
- is possible make java use different class
iterable
forall loops? - if not, possible mark
java.util.iterator.remove()
deprecated? - also, possible customise ides (eclipse , netbeans) warn me every time use
iterator
?
my current plan define own iterator
, extension of java.util.iterator
, , mark remove()
method deprecated. next, have script looking @ code , making sure java.util
used (except in definition of own iterator
). way, should safe.
edit: advises , links, guys. cheers.
i not go wisdom (or lack thereof) of replacing java.util.*
, far iterable
, iterator
go:
you cannot replace
iterable
in foreach loops - it's practically hardcoded in java compiler.the java specification allows throwing fine
unsupportedoperationexception
when optional operation is, well, unsupported. wrong that? if testsuite has proper coverage, know immediately, anyway. abstract implementations of several java interfaces (abstract*
) throw exception unsupported operations default.if code trying remove objects collection not support it, have greater problems deciding
remove()
. , if passing collections third-party code, will break if violate java interfaces.you override
remove()
method nothing silence errors... if can take unexpected breakage in code expects removed items be removed.
Comments
Post a Comment