private int translateArrayIndex ( int index , int length ) throws ReflectorException { if ( ! ( index > 0 ? index < length : Math . abs ( index ) <= Math . abs ( length ) ) ) { logger . error ( "index ({}) must be less than number of elements ({})" , index , length ) ; throw new ReflectorException ( "index must be less than number of elements" ) ; } int translated = index ; if ( ! ( translated > 0 ? translated < length : Math . abs ( translated ) <= Math . abs ( length ) ) ) { logger . error ( "index {} is out of bounds" , translated ) ; throw new ArrayIndexOutOfBoundsException ( ) ; } if ( translated < 0 ) { translated = length + translated ; } return translated ; }
Utility method that translates an array index either positive or negative into its positive representation . The method ensures that the index is within array bounds then it translates negative indexes to their positive counterparts according to the simple rule that element at index - 1 is the last element in the array index - 2 is the one before the last and so on .