Author: navssurtani
Date: 2008-06-26 13:01:49 -0400 (Thu, 26 Jun 2008)
New Revision: 6061
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java
searchable/trunk/src/test/java/org/jboss/cache/search/QueryResultIteratorImplTest.java
Log:
Implemented QueryResultIteratorImpl.
Modified:
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java
===================================================================
---
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java 2008-06-26
16:40:06 UTC (rev 6060)
+++
searchable/trunk/src/main/java/org/jboss/cache/search/QueryResultIteratorImpl.java 2008-06-26
17:01:49 UTC (rev 6061)
@@ -29,79 +29,104 @@
public void jumpToResult(int index) throws IndexOutOfBoundsException
{
- //TODO: Implement
+ if (index > idList.size() || index < 0)
+ {
+ throw new IndexOutOfBoundsException("The index you entered is either
greater than the size of the list or negative");
+ }
+ this.index = index;
}
public void first()
{
- //TODO: Implement
+ index = 0;
}
public void last()
{
- //TODO: Implement
+ index = idList.size() - 1;
}
public void afterFirst()
{
- //TODO: Implement
+ index = 1;
}
public void beforeLast()
{
- //TODO: Implement
+ index = idList.size() - 2;
}
public boolean isFirst()
{
- return false; //TODO: Implement
+ return idList.get(index) == idList.get(0);
}
public boolean isLast()
{
- return false; //TODO: Implement
+ return idList.get(index) == idList.get(idList.size() - 1);
}
public boolean isAfterFirst()
{
- return false; //TODO: Implement
+ return idList.get(index) == idList.get(1);
}
public boolean isBeforeLast()
{
- return false; //TODO: Implement
+ return idList.get(index) == idList.get(idList.size() - 2);
}
public boolean hasNext()
{
- return false; //TODO: Implement
+ if (idList.size() != 1)
+ {
+ return !isLast();
+ }
+ else
+ {
+ return false;
+ }
}
public Object next()
{
if (!hasNext()) throw new NoSuchElementException("Out of boundaries");
+ Object toReturn = entityLoader.load(idList.get(index));
index++;
- return next;
+ return toReturn;
}
public boolean hasPrevious()
{
- return false; //TODO: Implement
+ if (idList.size() != 1)
+ {
+ return !isFirst();
+ }
+ else
+ {
+ return false;
+ }
}
public Object previous()
{
- return null; //TODO: Implement
+ if (!hasPrevious()) throw new NoSuchElementException("Out of
boundaries");
+ Object toReturn = entityLoader.load(idList.get(index));
+ index--;
+ return toReturn;
}
public int nextIndex()
{
- return 0; //TODO: Implement
+ if (!hasNext()) throw new NoSuchElementException("Out of boundaries");
+ return index + 1;
+
}
public int previousIndex()
{
- return 0; //TODO: Implement
+ if (!hasPrevious()) throw new NoSuchElementException("Out of
boundaries");
+ return index - 1;
}
public void remove()
Modified:
searchable/trunk/src/test/java/org/jboss/cache/search/QueryResultIteratorImplTest.java
===================================================================
---
searchable/trunk/src/test/java/org/jboss/cache/search/QueryResultIteratorImplTest.java 2008-06-26
16:40:06 UTC (rev 6060)
+++
searchable/trunk/src/test/java/org/jboss/cache/search/QueryResultIteratorImplTest.java 2008-06-26
17:01:49 UTC (rev 6061)
@@ -102,14 +102,13 @@
//Makes sure that the iterator is pointing at the last element.
assert iterator.isLast();
- //Iterator points at the previous element.
- Object previous = iterator.previous();
+ Object next = iterator.next();
//Returns the size of the list of ids.
int size = ids.size();
//Makes sure that previous is the last element.
- assert previous == dummyResults.get(ids.get(size - 1));
+ assert next == dummyResults.get(ids.get(size - 1));
//Check that the iterator is NOT pointing at the last element.
assert !iterator.isLast();
@@ -126,7 +125,7 @@
//Previous element in the list
Object previous = iterator.previous();
- //Check that previous is the second element.
+ //Check that previous is the first element.
assert previous == dummyResults.get(ids.get(0));
//Make sure that the iterator isn't pointing at the second element.
@@ -147,7 +146,7 @@
//Check that next is the penultimate element.
int size = ids.size();
- assert next == dummyResults.get(ids.get(size - 1));
+ assert next == dummyResults.get(ids.get(size - 2));
//Make sure that the iterator is not pointing at the penultimate element.
assert !iterator.isAfterFirst();
@@ -186,78 +185,28 @@
assert iterator.isBeforeLast();
}
- public void testHasNext()
+ public void testNextAndHasNext()
{
- int size = ids.size();
-
- if (size > 1)
- {
- iterator.first();
- assert iterator.hasNext();
-
- iterator.last();
- assert !iterator.hasNext();
-
- }
-
- else
- {
- assert false;
- }
-
- }
-
- public void testNext()
- {
iterator.first();
- if (iterator.hasNext())
+ for (int i = 0; i < ids.size(); i++)
{
- iterator.next();
-
- assert iterator.isAfterFirst();
- assert !iterator.isFirst();
+ Object expectedValue = dummyResults.get(ids.get(i));
+ assert iterator.hasNext(); // should have next as long as we are less than the
number of elements.
+ assert expectedValue == iterator.next(); // tests next()
}
-
- else
- {
- assert false;
- }
+ assert !iterator.hasNext(); // this should now NOT be true.
}
- public void testHasPrevious()
+ public void testPreviousAndHasPrevious()
{
- int size = ids.size();
-
- if (size > 1)
- {
- iterator.first();
- assert !iterator.hasPrevious();
-
- iterator.last();
- assert iterator.hasPrevious();
-
- }
-
- else
- {
- assert false;
- }
- }
-
- public void testPrevious()
- {
iterator.last();
- if(iterator.hasPrevious())
+ for (int i = ids.size(); i > 0; i--)
{
- iterator.previous();
- assert iterator.isBeforeLast();
- assert !iterator.isLast();
-
+ Object expectedValue = dummyResults.get(ids.get(i));
+ assert iterator.hasPrevious(); // should have next as long as we are less than
the number of elements.
+ assert expectedValue == iterator.previous(); // tests next()
}
- else
- {
- assert false;
- }
+ assert !iterator.hasPrevious(); // this should now NOT be true.
}
@@ -271,7 +220,7 @@
}
- public void previousIndexTest()
+ public void testPreviousIndex()
{
iterator.first();
assert iterator.previousIndex() == -1;
@@ -280,6 +229,16 @@
assert iterator.previousIndex() == (ids.size() - 1);
}
+ public void testPreviousAndNext()
+ {
+ iterator.jumpToResult(5);
+ for (int i=0; i<10; i++) // repeat 10 times
+ {
+ assert iterator.next() == iterator.previous();
+ }
+
+ }
+
public static class DummyEntityLoader extends CacheEntityLoader
{
private List<CacheEntityId> allKnownIds;
Show replies by date