-1

This is the original method, my intent was to abstract away many of the specifics of the code, to improve readability, by placing the specifics in functions which performed the same action, but with a more readable name.

With this first method I achieve the desired behavior and rows[x] is properly added to lineRows.

def getAllRowsForLine( rows, index ) {
    def lineRows = [rows[index]]
    def newOperatorNotFound
    def x = index + 1
    if ( x <= rows.size() - 1 ) {
        newOperatorNotFound = true
        while ( x <= ( rows.size() - 1 ) && newOperatorNotFound ) {
            if ( rows[x].PGM_PROC_OPE.trim() == "" ) {
                lineRows << rows[x]
            } else if ( rows[x].PGM_PROC_TY == "AN" || rows[x].PGM_PROC_TY == "OR" ) {
                lineRows << rows[x]
            }
            else {
                newOperatorNotFound = false
            }
        x++
        }

    }
    return lineRows
}    

Here is the refactored code along with the relative methods for context.

This method is not resulting in the desired behavior, breaking the loop after the first rows[x] is added to lineRows.

def getAllRowsForLine2( rows, index ) {
    def lineRows = [rows[index]]
    def newOperatorNotFound
    def i = index + 1
    if ( moreRows( rows, i ) ) {
        newOperatorNotFound = true
        while ( moreRows( rows, i ) && newOperatorNotFound ) {
            if ( operatorEmpty( rows, index ) ) {
                lineRows << rows[i]
            } 
            else if ( procTypeAnd( rows, i ) || procTypeOr( rows, i ) ) {
                lineRows << rows[i]
            } else {
                newOperatorNotFound = false
            }
        i++
        }
    }
    return lineRows
}

def operatorEmpty( rows, index ) {
    return rows[index].PGM_PROC_OPE.trim() == ""
}

def procTypeAnd( rows, index ) {
    return rows[index].PGM_PROC_TY == "AN"
}

def procTypeOr( rows, index ) {
    return rows[index].PGM_PROC_TY == "OR"
}

def moreRows( rows, index ) {
    return index <= ( rows.size() - 1 )
}

As far as I can tell these things are equivalent. I ran the below code to attempt testing the equivalency of the functions and it returns true.

println lineProcessor.getAllRowsForLine( rows, 0 ) == lineProcessor.getAllRowsForLine2( rows, 0 )
=> true
kschmit90
  • 378
  • 2
  • 3
  • 13

1 Answers1

0

Oops, I realized that I had used index in the operatorEmpty function instead of i. If I change index to i the function performs as expected.

kschmit90
  • 378
  • 2
  • 3
  • 13