Simplify hierarchy units
[lgpl/argeo-commons.git] / org.argeo.util / src / org / argeo / osgi / useradmin / LdapNameUtils.java
index b98c8bff7225035485392bbc9088c90b25a0b752..f76d9441b9a5613799f4bd659faa87cf2553593b 100644 (file)
@@ -1,27 +1,27 @@
 package org.argeo.osgi.useradmin;
 
-import java.util.StringJoiner;
-
 import javax.naming.InvalidNameException;
 import javax.naming.ldap.LdapName;
 import javax.naming.ldap.Rdn;
 
 /** Utilities to simplify using {@link LdapName}. */
-public class LdapNameUtils {
-
-       public static String toRevertPath(String dn, String prefix) {
-               if (!dn.endsWith(prefix))
-                       throw new IllegalArgumentException("Prefix " + prefix + " not consistent with " + dn);
-               String relativeName = dn.substring(0, dn.length() - prefix.length() - 1);
-               LdapName name = toLdapName(relativeName);
-               StringJoiner path = new StringJoiner("/");
-               for (int i = 0; i < name.size(); i++) {
-                       path.add(name.get(i));
+class LdapNameUtils {
+
+       static LdapName relativeName(LdapName prefix, LdapName dn) {
+               try {
+                       if (!dn.startsWith(prefix))
+                               throw new IllegalArgumentException("Prefix " + prefix + " not consistent with " + dn);
+                       LdapName res = (LdapName) dn.clone();
+                       for (int i = 0; i < prefix.size(); i++) {
+                               res.remove(0);
+                       }
+                       return res;
+               } catch (InvalidNameException e) {
+                       throw new IllegalStateException("Cannot find realtive name", e);
                }
-               return path.toString();
        }
 
-       public static LdapName getParent(LdapName dn) {
+       static LdapName getParent(LdapName dn) {
                try {
                        LdapName parent = (LdapName) dn.clone();
                        parent.remove(parent.size() - 1);
@@ -31,7 +31,7 @@ public class LdapNameUtils {
                }
        }
 
-       public static LdapName toLdapName(String distinguishedName) {
+       static LdapName toLdapName(String distinguishedName) {
                try {
                        return new LdapName(distinguishedName);
                } catch (InvalidNameException e) {
@@ -39,15 +39,15 @@ public class LdapNameUtils {
                }
        }
 
-       public static Rdn getLastRdn(LdapName dn) {
+       static Rdn getLastRdn(LdapName dn) {
                return dn.getRdn(dn.size() - 1);
        }
 
-       public static String getLastRdnAsString(LdapName dn) {
+       static String getLastRdnAsString(LdapName dn) {
                return getLastRdn(dn).toString();
        }
 
-       public static String getLastRdnValue(LdapName dn) {
+       static String getLastRdnValue(LdapName dn) {
                return getLastRdn(dn).getValue().toString();
        }