• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Paweł Baczyński
  • Piet Souris
  • Vijitha Kumara

FilteredIterator

 
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to implement a FilteredIterator, let me know if this implementation is okay:


[ August 18, 2006: Message edited by: Raghav Sam ]
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks ok so far, from skimming over it. The save path would be to write some JUnit tests for it, though.

Instead of the setters, I'd probably set those things in the constructor - they definitely need to be set before the iterator can be used, and you probably shouldn't change them later on. Setting the fields only in the contstructor (and perhaps even making them final) would guarantee both.

The class would also be more flexible in use by passing it an Iterator to decorate instead of a Collection - that way, you can also use it with Iterators that are not coming from Collections.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here are some specific tests which can be passed by standard iterators:

To see how your code handles them, uncomment the FilteredIterator stuff in setUp(). Note that this is not intended to be a complete set of tests - just several specific ones that came to mind as I read the code.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good work, Jim! I'm abashed to see what I've missed...
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If this is 1.5+ you'd be well served to remember generics.
 
Raghav Sam
Ranch Hand
Posts: 412
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Ilja Preuss:
Looks ok so far, from skimming over it. The save path would be to write some JUnit tests for it, though.



JUnit test (Filter out strings starting with upper case letters):

assuming the filtered iterator is instantiated and the filter set.


Instead of the setters, I'd probably set those things in the constructor - they definitely need to be set before the iterator can be used, and you probably shouldn't change them later on. Setting the fields only in the contstructor (and perhaps even making them final) would guarantee both.

The class would also be more flexible in use by passing it an Iterator to decorate instead of a Collection - that way, you can also use it with Iterators that are not coming from Collections.


Thanks for the input, I will try to use that info.

BTW, is there an English version of your website?
[ August 21, 2006: Message edited by: Raghav Sam ]
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Raghav Sam:

BTW, is there an English version of your website?



If you are talking about the book, unfortunately there is no translation available, and likely never will be.

The first edition is translated, though, and is quite good, too: https://coderanch.com/t/93538/Book-Reviews/Unit-Testing-Java-Johannes-Link
[ August 21, 2006: Message edited by: Ilja Preuss ]
 
To avoid criticism do nothing, say nothing, be nothing. -Elbert Hubbard. Please critique this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!