private Matches lookupDo(Template tmpl,int maxMatches){
  if (maxMatches < 0)   throw new IllegalArgumentException("negative maxMatches");
  int totalMatches=0;
  ArrayList<Item> matches=null;
  ItemIter iter=matchingItems(tmpl);
  if (maxMatches > 0 || iter.dupsPossible) {
    int suggestedSize=iter.suggestedSize();
    suggestedSize=maxMatches < suggestedSize ? maxMatches : suggestedSize;
    matches=new ArrayList<Item>(suggestedSize);
  }
  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(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);
}

private Matches lookupDo ( Template tmpl , int maxMatches ) { if ( maxMatches < 0 ) throw new IllegalArgumentException ( "negative maxMatches" ) ; int totalMatches = 0 ; ArrayList < Item > matches = null ; ItemIter iter = matchingItems ( tmpl ) ; if ( maxMatches > 0 || iter . dupsPossible ) { int suggestedSize = iter . suggestedSize ( ) ; suggestedSize = maxMatches < suggestedSize ? maxMatches : suggestedSize ; matches = new ArrayList < Item > ( suggestedSize ) ; } 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 ( 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 ) ; }
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 .
