View Javadoc

1   /*
2    * Copyright 2002-2005 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package net.sf.ldaptemplate;
17  
18  import java.util.LinkedList;
19  import java.util.List;
20  
21  import javax.naming.directory.SearchResult;
22  
23  /***
24   * A SearchResultCallbackHandler to collect all results in an internal List.
25   * Useful in combination with e.g. an
26   * {@link net.sf.ldaptemplate.AttributesMapper} or
27   * {@link net.sf.ldaptemplate.ContextMapper}.
28   * 
29   * @see net.sf.ldaptemplate.LdapTemplate
30   * @see net.sf.ldaptemplate.LdapTemplate.AttributesMapperCallbackHandler
31   * @see net.sf.ldaptemplate.LdapTemplate.ContextMapperCallbackHandler
32   * @author Mattias Arthursson
33   */
34  public abstract class CollectingSearchResultCallbackHandler implements
35          SearchResultCallbackHandler {
36  
37      private List list = new LinkedList();
38  
39      /*
40       * (non-Javadoc)
41       * 
42       * @see net.sf.ldaptemplate.SearchResultCallbackHandler#handleSearchResult(javax.naming.directory.SearchResult)
43       */
44      public void handleSearchResult(SearchResult searchResult) {
45          list.add(getObjectFromResult(searchResult));
46      }
47  
48      /***
49       * Handle a single search result and transform it to an Object.
50       * 
51       * @param searchResult
52       *            a SearchResult from a search operation.
53       * @return an object constructed from the data in the SearchResult.
54       */
55      protected abstract Object getObjectFromResult(SearchResult searchResult);
56  
57      /***
58       * Get the assembled list.
59       * 
60       * @return the list of all assembled objects.
61       */
62      public List getList() {
63          return list;
64      }
65  }