Hi there, this is my first post here, still quite new to Java. I'm having trouble with a custom iterator when trying to test it with JUnit. The reason to write a custom iterator is this: I want to get the object in the array, then for each item I want to get it's child items with a new iterator call.
It's quite complicated because naturally I'm trying to hide the implementation from the "user". The iterator holds a list of classes called StandardSortElement. Each of these classes holds a class called AvailableResouce
which in turn holds an AbstractResource, and it's AbstractResource that I want the iterator to return. My JUnit testing shows that this part works correctly.
For the child items, they are also held inside the StandardSortElement as an array list. Here is the error I get
"The method createChildIterator() is undefined for the type Iterator<AbstractResource>"
First here is an interface for the custom iterator:
public interface SortIteratorInterface extends Iterator<AbstractResource>{
public abstract Iterator<BookingInterface> createChildIterator();
}
Here is the iterator class:
public class StandardSortIterator implements SortIteratorInterface {
ArrayList<StandardSortElement> items;
Integer position = 0;
/************************************************************************************/
/**
* a constructor that passes in a sorted list of StandardSortElement
*
*/
public StandardSortIterator(ArrayList<StandardSortElement> items) {
this.items = items;
}
/************************************************************************************/
/**
* Return bookings associated with the currently iterated StandardSortElement
*
*/
public Iterator<BookingInterface> createChildIterator() {
return items.get(position).createBookingIterator();
}
/************************************************************************************/
/**
* Determine if there is another item in the list
*
*/
public boolean hasNext() {
if (position >= items.size()) {
return false;
} else {
return true;
}
}
/************************************************************************************/
/**
* Return the next resource in the list. We have a list of StandardSortElement but
* we want to return the resource within.
*
*/
public AbstractResource next() {
StandardSortElement ssa = items.get(position); // Get a handle on the StandardSortElement
AbstractResource ar = ssa.getResource(); // Now get the resource that's packaged up inside
position++; // Increment the position
return ar; // return the resource
}
/************************************************************************************/
/**
* Do not implement - mandatory interface implementation but not required here.
*
*/
public void remove() {
// TODO Auto-generated method stub
}
/************************************************************************************/
}
This is the code taken from the JUnit test class
/************************************************************************************/
/**
* This iterator also has an interal child iterator. Check this works as expected
*/
@Test
public void testChildIterator() {
Iterator<AbstractResource> iterator = new StandardSortIterator(items);
Integer i = 0;
while (iterator.hasNext()) {
AbstractResource abstractResource = (AbstractResource)iterator.next();
if (i.equals(0)) {
// The first element should contain children - check it works
Iterator<BookingInterface> childIterator = iterator.createChildIterator(); // ERROR OCCURS HERE
}
i++;
}
assertTrue(i.equals(items.size()));
}
/************************************************************************************/
Thank you in advance for any help you can provide!
Paul