001/*
002 * Copyright 2011-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2011-2020 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2011-2020 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.ldap.sdk;
037
038
039
040import java.util.ArrayList;
041import java.util.Collections;
042import java.util.List;
043
044import com.unboundid.util.InternalUseOnly;
045import com.unboundid.util.Mutable;
046import com.unboundid.util.ThreadSafety;
047import com.unboundid.util.ThreadSafetyLevel;
048
049
050
051/**
052 * This class provides a basic implementation of the
053 * {@link AsyncSearchResultListener} interface that will merely set the
054 * result object to a local variable that can be accessed through a getter
055 * method.  It provides a listener that may be easily used when processing
056 * an asynchronous search operation using the {@link AsyncRequestID} as a
057 * {@code java.util.concurrent.Future} object.
058 * <BR><BR>
059 * Note that this class stores all entries and references returned by the
060 * associated search in memory so that they can be retrieved in lists.  This may
061 * not be suitable for searches that have the potential return a large number
062 * of entries.  For such searches, an alternate
063 * {@link AsyncSearchResultListener} implementation may be needed, or it may be
064 * more appropriate to use an {@link LDAPEntrySource} object for the search.
065 */
066@Mutable()
067@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
068public final class BasicAsyncSearchResultListener
069       implements AsyncSearchResultListener
070{
071  /**
072   * The serial version UID for this serializable class.
073   */
074  private static final long serialVersionUID = 2289128360755244209L;
075
076
077
078  // The list of search result entries that have been returned.
079  private final List<SearchResultEntry> entryList;
080
081  // The list of search result references that have been returned.
082  private final List<SearchResultReference> referenceList;
083
084  // The search result that has been received for the associated search
085  // operation.
086  private volatile SearchResult searchResult;
087
088
089
090  /**
091   * Creates a new instance of this class for use in processing a single search
092   * operation.  A single basic async search result listener object may not be
093   * used for multiple operations.
094   */
095  public BasicAsyncSearchResultListener()
096  {
097    searchResult  = null;
098    entryList     = new ArrayList<>(5);
099    referenceList = new ArrayList<>(5);
100  }
101
102
103
104  /**
105   * {@inheritDoc}
106   */
107  @InternalUseOnly()
108  @Override()
109  public void searchEntryReturned(final SearchResultEntry searchEntry)
110  {
111    entryList.add(searchEntry);
112  }
113
114
115
116  /**
117   * {@inheritDoc}
118   */
119  @InternalUseOnly()
120  @Override()
121  public void searchReferenceReturned(
122                   final SearchResultReference searchReference)
123  {
124    referenceList.add(searchReference);
125  }
126
127
128
129  /**
130   * {@inheritDoc}
131   */
132  @InternalUseOnly()
133  @Override()
134  public void searchResultReceived(final AsyncRequestID requestID,
135                                    final SearchResult searchResult)
136  {
137    this.searchResult = searchResult;
138  }
139
140
141
142  /**
143   * Retrieves the result that has been received for the associated asynchronous
144   * search operation, if it has been received.
145   *
146   * @return  The result that has been received for the associated asynchronous
147   *          search operation, or {@code null} if no response has been received
148   *          yet.
149   */
150  public SearchResult getSearchResult()
151  {
152    return searchResult;
153  }
154
155
156
157  /**
158   * Retrieves a list of the entries returned for the search operation.  This
159   * should only be called after the operation has completed and a
160   * non-{@code null} search result object is available, because it may not be
161   * safe to access the contents of the list if it may be altered while the
162   * search is still in progress.
163   *
164   * @return  A list of the entries returned for the search operation.
165   */
166  public List<SearchResultEntry> getSearchEntries()
167  {
168    return Collections.unmodifiableList(entryList);
169  }
170
171
172
173  /**
174   * Retrieves a list of the references returned for the search operation.  This
175   * should only be called after the operation has completed and a
176   * non-{@code null} search result object is available, because it may not be
177   * safe to access the contents of the list if it may be altered while the
178   * search is still in progress.
179   *
180   * @return  A list of the references returned for the search operation.
181   */
182  public List<SearchResultReference> getSearchReferences()
183  {
184    return Collections.unmodifiableList(referenceList);
185  }
186}