private static int _indexOfStartingFrom ( List < ? > list , int startIndex , Object searchValue ) { int itemCount = list . size ( ) ; boolean found = false ; // start searching from location remembered from last time for ( int currIndex = startIndex ; currIndex < itemCount ; currIndex ++ ) { Object currId = list . get ( currIndex ) ; if ( ( searchValue == currId ) || ( ( searchValue != null ) && searchValue . equals ( currId ) ) ) { return currIndex ; } } // handle case where we started past the first item and didn't find the // searchValue. Now search from the beginning to where we started if ( startIndex > 0 ) { for ( int currIndex = 0 ; currIndex < startIndex ; currIndex ++ ) { Object currId = list . get ( currIndex ) ; if ( ( searchValue == currId ) || ( ( searchValue != null ) && searchValue . equals ( currId ) ) ) { return currIndex ; } } } // didn't find it return - 1 ; }
Similar to List . indexOf except that we start searching from a specific index and then wrap aroud . For this to be performant the List should implement RandomAccess .