/** * Retrieves the {@link javax.naming.directory.Attributes Attributes} view of the type/value * mappings contained in this Rdn. * * @return The non-null attributes containing the type/value mappings of this Rdn. */ public Attributes toAttributes() { Attributes attrs = new BasicAttributes(true); for (int i = 0; i < entries.size(); i++) { RdnEntry entry = entries.get(i); Attribute attr = attrs.put(entry.getType(), entry.getValue()); if (attr != null) { attr.add(entry.getValue()); attrs.put(attr); } } return attrs; }
public boolean equals(Object obj) { if (obj == this) { return true; } if (!(obj instanceof RdnEntry)) { return false; } // Any change here must be reflected in hashCode() RdnEntry that = (RdnEntry) obj; return (type.equalsIgnoreCase(that.type)) && (getValueComparable().equals(that.getValueComparable())); }
/* * Adds the given attribute type and value to this Rdn. * The string attribute values are not interpreted as * <a href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a> * formatted RDN strings. That is the values are used * literally (not parsed) and assumed to be unescaped. * * @param type The non-null and non-empty string attribute type. * @param value The non-null and non-empty attribute value. * @return The updated Rdn, not a new one. Cannot be null. * @see #toString() */ Rdn put(String type, Object value) { // create new Entry RdnEntry newEntry = new RdnEntry(); newEntry.type = type; if (value instanceof byte[]) { // clone the byte array newEntry.value = ((byte[]) value).clone(); } else { newEntry.value = value; } entries.add(newEntry); return this; }
public int compareTo(RdnEntry that) { int diff = type.compareToIgnoreCase(that.type); if (diff != 0) { return diff; } if (value.equals(that.value)) { // try shortcut return 0; } return getValueComparable().compareTo(that.getValueComparable()); }
/** * Constructs an Rdn from the given attribute set. See {@link javax.naming.directory.Attributes * Attributes}. * * <p>The string attribute values are not interpreted as <a * href="http://www.ietf.org/rfc/rfc2253.txt">RFC 2253</a> formatted RDN strings. That is, the * values are used literally (not parsed) and assumed to be unescaped. * * @param attrSet The non-null and non-empty attributes containing type/value mappings. * @throws InvalidNameException If contents of <tt>attrSet</tt> cannot be used to construct a * valid RDN. */ public Rdn(Attributes attrSet) throws InvalidNameException { if (attrSet.size() == 0) { throw new InvalidNameException("Attributes cannot be empty"); } entries = new ArrayList<>(attrSet.size()); NamingEnumeration<? extends Attribute> attrs = attrSet.getAll(); try { for (int nEntries = 0; attrs.hasMore(); nEntries++) { RdnEntry entry = new RdnEntry(); Attribute attr = attrs.next(); entry.type = attr.getID(); entry.value = attr.get(); entries.add(nEntries, entry); } } catch (NamingException e) { InvalidNameException e2 = new InvalidNameException(e.getMessage()); e2.initCause(e); throw e2; } sort(); // arrange entries for comparison }