View Javadoc
1   package org.argeo.naming;
2   
3   import java.util.EnumSet;
4   import java.util.Set;
5   import java.util.TreeSet;
6   
7   import javax.naming.InvalidNameException;
8   import javax.naming.ldap.LdapName;
9   
10  /**
11   * An object that can be identified with an X.500 distinguished name.
12   * 
13   * @see https://tools.ietf.org/html/rfc1779
14   */
15  public interface Distinguished {
16  	/** The related distinguished name. */
17  	String dn();
18  
19  	/** The related distinguished name as an {@link LdapName}. */
20  	default LdapName distinguishedName() {
21  		try {
22  			return new LdapName(dn());
23  		} catch (InvalidNameException e) {
24  			throw new IllegalArgumentException("Distinguished name " + dn() + " is not properly formatted.", e);
25  		}
26  	}
27  
28  	/** List all DNs of an enumeration as strings. */
29  	static Set<String> enumToDns(EnumSet<? extends Distinguished> enumSet) {
30  		Set<String> res = new TreeSet<>();
31  		for (Enum<? extends Distinguished> enm : enumSet) {
32  			res.add(((Distinguished) enm).dn());
33  		}
34  		return res;
35  	}
36  }