Skip to content

Commit

Permalink
Merge pull request #7722 from LeeVi3w/iterators-rework
Browse files Browse the repository at this point in the history
Updated PagingIterator mechanism and tests
  • Loading branch information
mandy-chessell authored Jun 12, 2023
2 parents ea4e348 + 23226e8 commit 58b9476
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,41 @@ public PagingIterator(PropertyIteratorBase iterator,
@Override
public boolean hasNext()
{
return (cachedElementStart < totalElementCount);
if (cachedElementList == null)
{
return false;
}

if (cachedElementPointer == cachedElementList.size())
{
try
{
cachedElementList = iterator.getCachedList(cachedElementStart, maxCacheSize);
if (cachedElementList == null || cachedElementList.isEmpty())
{
/*
* Prevents the pointer from being equal to the size of the list, signifying that the list is empty.
*/
cachedElementPointer = -1;
return false;
}

cachedElementPointer = 0;
}
catch (PropertyServerException error)
{
/*
* Problem retrieving next cache. The exception includes a detailed error message,
*/
throw new OCFRuntimeException(OCFErrorCode.PROPERTIES_NOT_AVAILABLE.getMessageDefinition(error.getReportedErrorMessage(),
this.toString()),
this.getClass().getName(),
"next",
error);
}
}

return true;
}


Expand All @@ -184,38 +218,9 @@ public boolean hasNext()
@Override
public ElementBase next()
{
/*
* Check more elements available
*/
if (this.hasNext())
{
ElementBase retrievedElement;

/*
* If the pointer is at the end of the cache then retrieve more content from the property (metadata)
* server.
*/
if (cachedElementPointer == cachedElementList.size())
{
try
{
cachedElementList = iterator.getCachedList(cachedElementStart, maxCacheSize);
cachedElementPointer = 0;
}
catch (PropertyServerException error)
{
/*
* Problem retrieving next cache. The exception includes a detailed error message,
*/
throw new OCFRuntimeException(OCFErrorCode.PROPERTIES_NOT_AVAILABLE.getMessageDefinition(error.getReportedErrorMessage(),
this.toString()),
this.getClass().getName(),
"next",
error);
}
}

retrievedElement = iterator.cloneElement(cachedElementList.get(cachedElementPointer));
ElementBase retrievedElement = iterator.cloneElement(cachedElementList.get(cachedElementPointer));
cachedElementPointer++;
cachedElementStart++;

Expand All @@ -232,9 +237,9 @@ public ElementBase next()
/*
* Throw runtime exception to show the caller they are not using the list correctly.
*/
throw new OCFRuntimeException(OCFErrorCode.NO_MORE_ELEMENTS.getMessageDefinition(this.getClass().getSimpleName()),
this.getClass().getName(),
"next");
throw new OCFRuntimeException(OCFErrorCode.NO_MORE_ELEMENTS.getMessageDefinition(this.getClass().getName()),
this.getClass().getName(),
"next");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,74 +31,6 @@ public SchemaAttributes(int totalElementCount,
int maxCacheSize)
{
super(totalElementCount, maxCacheSize);
// Anonymous class for temporary changing the behaviour of the iterator in the case of SchemaAttributes
// as to not use the "totalElementCount" parameter because its value is always 0
super.pagingIterator = new PagingIterator(this, totalElementCount, maxCacheSize)
{
@Override
public ElementBase next()
{
if (this.hasNext())
{
ElementBase retrievedElement = iterator.cloneElement(cachedElementList.get(cachedElementPointer));
cachedElementPointer++;
cachedElementStart++;

log.debug("Returning next element:");
log.debug("==> totalElementCount: " + totalElementCount);
log.debug("==> cachedElementPointer: " + cachedElementPointer);
log.debug("==> cachedElementStart:" + cachedElementStart);
log.debug("==> maxCacheSize:" + maxCacheSize);

return retrievedElement;
}
else
{
/*
* Throw runtime exception to show the caller they are not using the list correctly.
*/
throw new OCFRuntimeException(OCFErrorCode.NO_MORE_ELEMENTS.getMessageDefinition(this.getClass().getName()),
this.getClass().getName(),
"next");
}
}

@Override
public boolean hasNext()
{
if (cachedElementList == null)
{
return false;
}

if (cachedElementPointer == cachedElementList.size())
{
try
{
cachedElementList = iterator.getCachedList(cachedElementStart, maxCacheSize);
if (cachedElementList == null)
{
return false;
}

cachedElementPointer = 0;
}
catch (PropertyServerException error)
{
/*
* Problem retrieving next cache. The exception includes a detailed error message,
*/
throw new OCFRuntimeException(OCFErrorCode.PROPERTIES_NOT_AVAILABLE.getMessageDefinition(error.getReportedErrorMessage(),
this.toString()),
this.getClass().getName(),
"next",
error);
}
}

return true;
}
};
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,23 +118,23 @@ private void validatePropertyIterator(int totalElementCount, int maxCache
}


/**
* Validate that element count is set.
/*
* This test is no longer relevant for SchemaAttributes as in real scenarios the count is no longer calculated.
*/
@Test public void testElementCount()
{
SchemaAttributes propertyIterator = getPropertyIterator(30, 10);

assertTrue(propertyIterator.getElementCount() == 30);

SchemaAttributes clonedPropertyIterator = new MockSchemaAttributes( propertyIterator);

assertTrue(clonedPropertyIterator.getElementCount() == 30);

clonedPropertyIterator = new MockSchemaAttributes( null);

assertTrue(clonedPropertyIterator.getElementCount() == 0);
}
// @Test public void testElementCount()
// {
// SchemaAttributes propertyIterator = getPropertyIterator(30, 10);
//
// assertTrue(propertyIterator.getElementCount() == 30);
//
// SchemaAttributes clonedPropertyIterator = new MockSchemaAttributes( propertyIterator);
//
// assertTrue(clonedPropertyIterator.getElementCount() == 30);
//
// clonedPropertyIterator = new MockSchemaAttributes( null);
//
// assertTrue(clonedPropertyIterator.getElementCount() == 0);
// }


/**
Expand Down

0 comments on commit 58b9476

Please sign in to comment.