Hi, I have some ideas (using functors), but I'm not sure if there's a better way. I have this huge nested loop in Java, and each loop is virtually identical except the inner-most loop. Is there a way to factor each loop out into a separate function (preferable generic function) to reduce code duplication and brain overload when trying to look at 5 nested loops?
Code:
// Validate the format of all other sub-sections of the XML file.
for ( int authenticationIndex = 0; authenticationIndex < authenticationNodeList.getLength(); ++authenticationIndex )
{
Node authenticationNode = authenticationNodeList.item( authenticationIndex );
if ( authenticationNode.hasChildNodes() == false )
{
throw new XMLParseException( "XML format is invalid! No child nodes under " + authenticationNode.getNodeName() );
}
NodeList nbuNodeList = authenticationNode.getChildNodes();
Set<Set<Set<Set<UserStateFunctor>>>> authenticationSet = null;
for ( int nbuIndex = 0; nbuIndex < nbuNodeList.getLength(); ++nbuIndex )
{
Node nbuNode = nbuNodeList.item( nbuIndex );
if ( nbuNode.hasChildNodes() == false )
{
throw new XMLParseException( "XML format is invalid! No child nodes under " + nbuNode.getNodeName() );
}
NodeList securityProviderNodeList = nbuNode.getChildNodes();
Set<Set<Set<Set<UserStateFunctor>>>> securityProviderSet = null;
for ( int spIndex = 0; spIndex < securityProviderNodeList.getLength(); ++spIndex )
{
Node securityProviderNode = securityProviderNodeList.item( spIndex );
if ( securityProviderNode.hasChildNodes() == false )
{
throw new XMLParseException( "XML format is invalid! No child nodes under " + securityProviderNode.getNodeName() );
}
NodeList bUserTypeNodeList = securityProviderNode.getChildNodes();
Set<Set<Set<UserStateFunctor>>> bUserTypeSet = null;
for ( int bUserTypeIndex = 0; bUserTypeIndex < bUserTypeNodeList.getLength(); ++bUserTypeIndex )
{
Node bUserTypeNode = bUserTypeNodeList.item( bUserTypeIndex );
if ( bUserTypeNode.hasChildNodes() == false )
{
throw new XMLParseException( "XML format is invalid! No child nodes under " + bUserTypeNode.getNodeName() );
}
NodeList logonTypeNodeList = bUserTypeNode.getChildNodes();
Set<Set<UserStateFunctor>> logonTypeSet = null;
for ( int logonTypeIndex = 0; logonTypeIndex < logonTypeNodeList.getLength(); ++logonTypeIndex )
{
Node logonTypeNode = logonTypeNodeList.item( logonTypeIndex );
if ( logonTypeNode.hasChildNodes() == false )
{
throw new XMLParseException( "XML format is invalid! No child nodes under " + logonTypeNode.getNodeName() );
}
NodeList checkNodeList = logonTypeNode.getChildNodes();
Set<UserStateFunctor> userStateSet = getCheckStatesFromXML( checkNodeList );
if ( (logonTypeSet.add( userStateSet ) == false) ||
(bUserTypeSet.add( logonTypeSet ) == false) ||
(securityProviderSet.add( bUserTypeSet ) == false) ||
(authenticationSet.add( bUserTypeSet ) == false) )
{
throw new XMLParseException( "XML format is invalid!" );
}
} // End of logonTypeIndex loop.
} // End of bUserTypeIndex loop.
} // End of spIndex loop.
} // End of nbuIndex loop.
} // authenticationIndex loop.