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.migrate.ldapjdk;
037
038
039
040import java.io.Serializable;
041import java.util.ArrayList;
042import java.util.Enumeration;
043
044import com.unboundid.ldap.sdk.Attribute;
045import com.unboundid.ldap.sdk.Entry;
046import com.unboundid.util.NotExtensible;
047import com.unboundid.util.NotNull;
048import com.unboundid.util.Nullable;
049import com.unboundid.util.NotMutable;
050import com.unboundid.util.ThreadSafety;
051import com.unboundid.util.ThreadSafetyLevel;
052
053
054
055/**
056 * This class provides a data structure that represents an LDAP entry.
057 * <BR><BR>
058 * This class is primarily intended to be used in the process of updating
059 * applications which use the Netscape Directory SDK for Java to switch to or
060 * coexist with the UnboundID LDAP SDK for Java.  For applications not written
061 * using the Netscape Directory SDK for Java, the {@link Entry} class should be
062 * used instead.
063 */
064@NotExtensible()
065@NotMutable()
066@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
067public class LDAPEntry
068       implements Serializable
069{
070  /**
071   * The serial version UID for this serializable class.
072   */
073  private static final long serialVersionUID = -6285850560316222689L;
074
075
076
077  // The DN for this entry.
078  @NotNull private final String dn;
079
080  // The attribute set for this entry.
081  @NotNull private final LDAPAttributeSet attributeSet;
082
083
084
085  /**
086   * Creates a new LDAP entry with a zero-length DN and no attributes.
087   */
088  public LDAPEntry()
089  {
090    this("", new LDAPAttributeSet());
091  }
092
093
094
095  /**
096   * Creates a new LDAP entry with the provided DN and no attributes.
097   *
098   * @param  distinguishedName  The DN to use for the entry.
099   */
100  public LDAPEntry(@NotNull final String distinguishedName)
101  {
102    this(distinguishedName, new LDAPAttributeSet());
103  }
104
105
106
107  /**
108   * Creates a new LDAP entry with the provided DN and attributes.
109   *
110   * @param  distinguishedName  The DN to use for the entry.
111   * @param  attrs              The attributes to use for the entry.
112   */
113  public LDAPEntry(@NotNull final String distinguishedName,
114                   @Nullable final LDAPAttributeSet attrs)
115  {
116    dn = distinguishedName;
117
118    if (attrs == null)
119    {
120      attributeSet = new LDAPAttributeSet();
121    }
122    else
123    {
124      attributeSet = attrs;
125    }
126  }
127
128
129
130  /**
131   * Creates a new LDAP entry from the provided {@link Entry} object.
132   *
133   * @param  entry  The entry to use to create this LDAP entry.
134   */
135  public LDAPEntry(@NotNull final Entry entry)
136  {
137    dn = entry.getDN();
138
139    attributeSet = new LDAPAttributeSet();
140    for (final Attribute a : entry.getAttributes())
141    {
142      attributeSet.add(new LDAPAttribute(a));
143    }
144  }
145
146
147
148  /**
149   * Retrieves the distinguished name for this entry.
150   *
151   * @return  The distinguished name for this entry.
152   */
153  @NotNull()
154  public String getDN()
155  {
156    return dn;
157  }
158
159
160
161  /**
162   * Retrieves the attributes for this entry.
163   *
164   * @return  The attributes for this entry.
165   */
166  @NotNull()
167  public LDAPAttributeSet getAttributeSet()
168  {
169    return attributeSet;
170  }
171
172
173
174  /**
175   * Retrieves the set of attributes containing the specified subtype for this
176   * entry.
177   *
178   * @param  subtype  The subtype for the attributes to retrieve.
179   *
180   * @return  The set of attributes containing the specified subtype.
181   */
182  @NotNull()
183  public LDAPAttributeSet getAttributeSet(@NotNull final String subtype)
184  {
185    return attributeSet.getSubset(subtype);
186  }
187
188
189
190  /**
191   * Retrieves the attribute with the specified name.
192   *
193   * @param  attrName  The name of the attribute to retrieve.
194   *
195   * @return  The requested attribute, or {@code null} if there is none.
196   */
197  @Nullable()
198  public LDAPAttribute getAttribute(@NotNull final String attrName)
199  {
200    return attributeSet.getAttribute(attrName);
201  }
202
203
204
205  /**
206   * Retrieves the attribute with the specified base name and language subtype.
207   *
208   * @param  attrName  The base name of the attribute to retrieve.
209   * @param  lang      The language subtype for the attribute to retrieve.
210   *
211   * @return  The requested attribute, or {@code null} if there is none.
212   */
213  @Nullable()
214  public LDAPAttribute getAttribute(@NotNull final String attrName,
215                                    @Nullable final String lang)
216  {
217    return attributeSet.getAttribute(attrName, lang);
218  }
219
220
221
222  /**
223   * Retrieves an {@link Entry} object that is the equivalent of this LDAP
224   * entry.
225   *
226   * @return  The {@code Entry} object that is the equivalent of this LDAP
227   *          entry.
228   */
229  @NotNull()
230  public final Entry toEntry()
231  {
232    final ArrayList<Attribute> attrs = new ArrayList<>(attributeSet.size());
233    final Enumeration<LDAPAttribute> attrEnum = attributeSet.getAttributes();
234    while (attrEnum.hasMoreElements())
235    {
236      attrs.add(attrEnum.nextElement().toAttribute());
237    }
238
239    return new Entry(dn, attrs);
240  }
241
242
243
244  /**
245   * Retrieves a string representation of this LDAP entry.
246   *
247   * @return  A string representation of this LDAP entry.
248   */
249  @Override()
250  @NotNull()
251  public String toString()
252  {
253    return toEntry().toString();
254  }
255}