001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2024 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) 2009-2024 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.io.Closeable;
041
042import com.unboundid.util.NotExtensible;
043import com.unboundid.util.Nullable;
044import com.unboundid.util.ThreadSafety;
045import com.unboundid.util.ThreadSafetyLevel;
046
047
048
049/**
050 * This class defines an API that may be implemented by a class that provides
051 * access to a sequence of entries, one entry at a time (e.g., entries read from
052 * an LDIF file, or returned as part of an LDAP search).  It provides a
053 * convenient way to operate on a set of entries without regard for the source
054 * of those entries.  Implementations currently available include the
055 * {@link LDAPEntrySource} class, which can be used to iterate across entries
056 * returned from a directory server in response to a search request, and the
057 * {@link com.unboundid.ldif.LDIFEntrySource} class, which can be used to
058 * iterate across entries in an LDIF file.
059 * <BR><BR>
060 * Note that the {@link #close} method MUST be called if the entry source is to
061 * be discarded before guaranteeing that all entries have been read.  The
062 * {@code close} method may be called after all entries have been read, but it
063 * is not required.  All entry source implementations MUST ensure that all
064 * resources are properly released if the caller has read through all entries,
065 * or if an error occurs that prevents the caller from continuing to read
066 * through the entries (i.e., if {@link #nextEntry} throws an
067 * {@link EntrySourceException} and the
068 * {@link EntrySourceException#mayContinueReading()} method returns
069 * {@code false}).
070 * <BR><BR>
071 * <H2>Example</H2>
072 * The following example demonstrates the process that may be used for iterating
073 * across the entries provided by an entry source:
074 * <PRE>
075 * LDIFReader ldifReader = new LDIFReader(ldifFilePath);
076 * EntrySource entrySource = new LDIFEntrySource(ldifReader);
077 *
078 * int entriesRead = 0;
079 * int exceptionsCaught = 0;
080 * try
081 * {
082 *   while (true)
083 *   {
084 *     try
085 *     {
086 *       Entry entry = entrySource.nextEntry();
087 *       if (entry == null)
088 *       {
089 *         // There are no more entries to be read.
090 *         break;
091 *       }
092 *       else
093 *       {
094 *         // Do something with the entry here.
095 *         entriesRead++;
096 *       }
097 *     }
098 *     catch (EntrySourceException e)
099 *     {
100 *       // Some kind of problem was encountered (e.g., a malformed entry
101 *       // found in an LDIF file, or a referral returned from a directory).
102 *       // See if we can continue reading entries.
103 *       exceptionsCaught++;
104 *       if (! e.mayContinueReading())
105 *       {
106 *         break;
107 *       }
108 *     }
109 *   }
110 * }
111 * finally
112 * {
113 *   entrySource.close();
114 * }
115 * </PRE>
116 */
117@NotExtensible()
118@ThreadSafety(level=ThreadSafetyLevel.INTERFACE_NOT_THREADSAFE)
119public abstract class EntrySource
120       implements Closeable
121{
122  /**
123   * Retrieves the next entry from the entry source, if there is at least one
124   * remaining entry.  This method may block if no entries are immediately
125   * available.
126   *
127   * @return  The next entry from the entry source, or {@code null} if there are
128   *          no more entries to retrieve.
129   *
130   * @throws  EntrySourceException  If a problem occurs while attempting to read
131   *                                the next entry from the entry source.
132   */
133  @Nullable()
134  public abstract Entry nextEntry()
135         throws EntrySourceException;
136
137
138
139  /**
140   * Indicates that this entry source will no longer be needed and any resources
141   * associated with it may be closed.  This method MUST be called if the entry
142   * source is no longer needed before all entries have been read.  It MAY be
143   * called after all entries have been read with no ill effects, but this is
144   * not necessary as the entry source will have already been closed after all
145   * entries have been read.
146   */
147  @Override()
148  public abstract void close();
149}