question: private Matches lookupDo ( Template tmpl , int maxMatches ) { if ( maxMatches < 0 ) throw new IllegalArgumentException ( "negative maxMatches" ) ; int totalMatches = 0 ; ArrayList matches = null ; ItemIter iter = matchingItems ( tmpl ) ; if ( maxMatches > 0 || iter . dupsPossible ) matches = new ArrayList ( ) ; if ( iter . dupsPossible ) { while ( iter . hasNext ( ) ) { Item item = iter . next ( ) ; if ( ! matches . contains ( item ) ) matches . add ( item ) ; } totalMatches = matches . size ( ) ; if ( maxMatches > 0 ) { for ( int i = matches . size ( ) ; -- i >= maxMatches ; ) matches . remove ( i ) ; for ( int i = matches . size ( ) ; -- i >= 0 ; ) { matches . set ( i , copyItem ( ( Item ) matches . get ( i ) ) ) ; } } else { matches = null ; } } else { while ( iter . hasNext ( ) ) { Item item = iter . next ( ) ; totalMatches ++ ; if ( -- maxMatches >= 0 ) matches . add ( copyItem ( item ) ) ; } } return new Matches ( matches , totalMatches ) ; }
target:  The code that does the real work of lookup.  We do a deep copy of the items being returned, both to avoid having them modified while being marshalled (by a concurrent update method), and to substitute replacements for embedded EntryClass and ServiceType instances, to minimize data sent back to the client.  If duplicates are possible from the iterator, we save all matches, weeding out duplicates as we go, then trim to maxMatches and deep copy.