001/* 002 * Copyright 2009-2014 UnboundID Corp. 003 * All Rights Reserved. 004 */ 005/* 006 * Copyright (C) 2009-2014 UnboundID Corp. 007 * 008 * This program is free software; you can redistribute it and/or modify 009 * it under the terms of the GNU General Public License (GPLv2 only) 010 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only) 011 * as published by the Free Software Foundation. 012 * 013 * This program is distributed in the hope that it will be useful, 014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 016 * GNU General Public License for more details. 017 * 018 * You should have received a copy of the GNU General Public License 019 * along with this program; if not, see <http://www.gnu.org/licenses>. 020 */ 021package com.unboundid.ldap.sdk.migrate.ldapjdk; 022 023 024 025import java.io.Serializable; 026import java.util.ArrayList; 027import java.util.Enumeration; 028 029import com.unboundid.ldap.sdk.Attribute; 030import com.unboundid.ldap.sdk.Entry; 031import com.unboundid.util.NotExtensible; 032import com.unboundid.util.NotMutable; 033import com.unboundid.util.ThreadSafety; 034import com.unboundid.util.ThreadSafetyLevel; 035 036 037 038/** 039 * This class provides a data structure that represents an LDAP entry. 040 * <BR><BR> 041 * This class is primarily intended to be used in the process of updating 042 * applications which use the Netscape Directory SDK for Java to switch to or 043 * coexist with the UnboundID LDAP SDK for Java. For applications not written 044 * using the Netscape Directory SDK for Java, the {@link Entry} class should be 045 * used instead. 046 */ 047@NotExtensible() 048@NotMutable() 049@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE) 050public class LDAPEntry 051 implements Serializable 052{ 053 /** 054 * The serial version UID for this serializable class. 055 */ 056 private static final long serialVersionUID = -6285850560316222689L; 057 058 059 060 // The DN for this entry. 061 private final String dn; 062 063 // The attribute set for this entry. 064 private final LDAPAttributeSet attributeSet; 065 066 067 068 /** 069 * Creates a new LDAP entry with a zero-length DN and no attributes. 070 */ 071 public LDAPEntry() 072 { 073 this("", new LDAPAttributeSet()); 074 } 075 076 077 078 /** 079 * Creates a new LDAP entry with the provided DN and no attributes. 080 * 081 * @param distinguishedName The DN to use for the entry. 082 */ 083 public LDAPEntry(final String distinguishedName) 084 { 085 this(distinguishedName, new LDAPAttributeSet()); 086 } 087 088 089 090 /** 091 * Creates a new LDAP entry with the provided DN and attributes. 092 * 093 * @param distinguishedName The DN to use for the entry. 094 * @param attrs The attributes to use for the entry. 095 */ 096 public LDAPEntry(final String distinguishedName, final LDAPAttributeSet attrs) 097 { 098 dn = distinguishedName; 099 100 if (attrs == null) 101 { 102 attributeSet = new LDAPAttributeSet(); 103 } 104 else 105 { 106 attributeSet = attrs; 107 } 108 } 109 110 111 112 /** 113 * Creates a new LDAP entry from the provided {@link Entry} object. 114 * 115 * @param entry The entry to use to create this LDAP entry. 116 */ 117 public LDAPEntry(final Entry entry) 118 { 119 dn = entry.getDN(); 120 121 attributeSet = new LDAPAttributeSet(); 122 for (final Attribute a : entry.getAttributes()) 123 { 124 attributeSet.add(new LDAPAttribute(a)); 125 } 126 } 127 128 129 130 /** 131 * Retrieves the distinguished name for this entry. 132 * 133 * @return The distinguished name for this entry. 134 */ 135 public String getDN() 136 { 137 return dn; 138 } 139 140 141 142 /** 143 * Retrieves the attributes for this entry. 144 * 145 * @return The attributes for this entry. 146 */ 147 public LDAPAttributeSet getAttributeSet() 148 { 149 return attributeSet; 150 } 151 152 153 154 /** 155 * Retrieves the set of attributes containing the specified subtype for this 156 * entry. 157 * 158 * @param subtype The subtype for the attributes to retrieve. 159 * 160 * @return The set of attributes containing the specified subtype. 161 */ 162 public LDAPAttributeSet getAttributeSet(final String subtype) 163 { 164 return attributeSet.getSubset(subtype); 165 } 166 167 168 169 /** 170 * Retrieves the attribute with the specified name. 171 * 172 * @param attrName The name of the attribute to retrieve. 173 * 174 * @return The requested attribute, or {@code null} if there is none. 175 */ 176 public LDAPAttribute getAttribute(final String attrName) 177 { 178 return attributeSet.getAttribute(attrName); 179 } 180 181 182 183 /** 184 * Retrieves the attribute with the specified base name and language subtype. 185 * 186 * @param attrName The base name of the attribute to retrieve. 187 * @param lang The language subtype for the attribute to retrieve. 188 * 189 * @return The requested attribute, or {@code null} if there is none. 190 */ 191 public LDAPAttribute getAttribute(final String attrName, final String lang) 192 { 193 return attributeSet.getAttribute(attrName, lang); 194 } 195 196 197 198 /** 199 * Retrieves an {@link Entry} object that is the equivalent of this LDAP 200 * entry. 201 * 202 * @return The {@code Entry} object that is the equivalent of this LDAP 203 * entry. 204 */ 205 public final Entry toEntry() 206 { 207 final ArrayList<Attribute> attrs = 208 new ArrayList<Attribute>(attributeSet.size()); 209 final Enumeration<LDAPAttribute> attrEnum = attributeSet.getAttributes(); 210 while (attrEnum.hasMoreElements()) 211 { 212 attrs.add(attrEnum.nextElement().toAttribute()); 213 } 214 215 return new Entry(dn, attrs); 216 } 217 218 219 220 221 /** 222 * Retrieves a string representation of this LDAP entry. 223 * 224 * @return A string representation of this LDAP entry. 225 */ 226 @Override() 227 public String toString() 228 { 229 return toEntry().toString(); 230 } 231}