You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by el...@apache.org on 2010/12/22 13:19:12 UTC
svn commit: r1051869 - in /directory/shared/branches/shared-AP/ldap/src:
main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java
test/java/org/apache/directory/shared/ldap/util/tree/TestDnNode.java
Author: elecharny
Date: Wed Dec 22 12:19:11 2010
New Revision: 1051869
URL: http://svn.apache.org/viewvc?rev=1051869&view=rev
Log:
Added a method and a test to get the closest parent in a DnNode which has an element from a starting point.
Modified:
directory/shared/branches/shared-AP/ldap/src/main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java
directory/shared/branches/shared-AP/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/TestDnNode.java
Modified: directory/shared/branches/shared-AP/ldap/src/main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-AP/ldap/src/main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java?rev=1051869&r1=1051868&r2=1051869&view=diff
==============================================================================
--- directory/shared/branches/shared-AP/ldap/src/main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java (original)
+++ directory/shared/branches/shared-AP/ldap/src/main/java/org/apache/directory/shared/ldap/util/tree/DnNode.java Wed Dec 22 12:19:11 2010
@@ -780,6 +780,59 @@ public class DnNode<N> implements Clonea
return hasElement;
}
+
+ /**
+ * Get the closest Node for a given DN which has an element, if present in the tree.<br>
+ * For instance, if we have stored dc=acme, dc=org into the tree,
+ * the DN: ou=example, dc=acme, dc=org will have a parent, and
+ * dc=acme, dc=org will be returned if it has an associated element.
+ * <br>For the DN ou=apache, dc=org, there is no parent, so null will be returned.
+ *
+ * @param dn the normalized distinguished name to resolve to a parent
+ * @return the Node associated with the normalized dn
+ */
+ public DN getParentWithElement( DN dn )
+ {
+ List<RDN> rdns = dn.getRdns();
+
+ DnNode<N> currentNode = this;
+ int pos = 0;
+
+ // Iterate through all the RDN until we find the associated partition
+ for ( int i = rdns.size() - 1; i >= 1; i-- )
+ {
+ RDN rdn = rdns.get( i );
+
+ if ( currentNode.hasChildren() )
+ {
+ currentNode = currentNode.children.get( rdn );
+
+ if ( currentNode == null )
+ {
+ break;
+ }
+
+ if ( currentNode.hasElement() )
+ {
+ pos = i;
+ }
+
+ parent = currentNode;
+ }
+ else
+ {
+ break;
+ }
+ }
+
+ if ( pos == 0 )
+ {
+ return DN.EMPTY_DN;
+ }
+
+ return dn.getPrefix( dn.size() - pos );
+ }
+
/**
* {@inheritDoc}
Modified: directory/shared/branches/shared-AP/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/TestDnNode.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-AP/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/TestDnNode.java?rev=1051869&r1=1051868&r2=1051869&view=diff
==============================================================================
--- directory/shared/branches/shared-AP/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/TestDnNode.java (original)
+++ directory/shared/branches/shared-AP/ldap/src/test/java/org/apache/directory/shared/ldap/util/tree/TestDnNode.java Wed Dec 22 12:19:11 2010
@@ -802,4 +802,40 @@ public class TestDnNode
dns = dnLookupTree.getDescendantElements( dn6 );
assertEquals( 0, dns.size() );
}
+
+
+ //---------------------------------------------------------------------------
+ // Test the getParentElement(DN) method
+ //---------------------------------------------------------------------------
+ @Test
+ public void testGetParentElement() throws Exception
+ {
+ DnNode<DN> dnLookupTree = new DnNode<DN>();
+ DN dn1 = new DN( "dc=directory,dc=apache,dc=org" );
+ DN dn2 = new DN( "dc=mina,dc=apache,dc=org" );
+ DN dn3 = new DN( "dc=test,dc=com" );
+ DN dn4 = new DN( "dc=acme,dc=com" );
+ DN dn5 = new DN( "dc=acme,c=us,dc=com" );
+ DN dn6 = new DN( "dc=empty" );
+
+ DN org = new DN( "dc=org" );
+ DN apache = new DN( "dc=apache,dc=org" );
+ DN test = new DN( "dc=test,dc=directory,dc=apache,dc=org" );
+
+ dnLookupTree.add( dn1, dn1 );
+ dnLookupTree.add( dn2, dn2 );
+ dnLookupTree.add( dn3, dn3 );
+ dnLookupTree.add( dn4, dn4 );
+ dnLookupTree.add( dn5 );
+ dnLookupTree.add( dn6, dn6 );
+
+ // Inject some intermediary nodes
+ dnLookupTree.add( org, org );
+
+ assertTrue( dnLookupTree.hasParentElement( apache ) );
+ assertEquals( org, dnLookupTree.getParentWithElement( dn1 ) );
+ assertEquals( org, dnLookupTree.getParentWithElement( apache ) );
+ assertEquals( dn1, dnLookupTree.getParentWithElement( test ) );
+ assertEquals( DN.EMPTY_DN, dnLookupTree.getParentWithElement( org ) );
+ }
}