1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.directory.server.core.partition.tree;
21
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import javax.naming.NamingException;
27
28 import org.apache.directory.server.core.partition.Partition;
29 import org.apache.directory.shared.ldap.name.LdapDN;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32
33
34
35
36
37
38
39
40
41
42
43 public class BranchNode implements Node
44 {
45 private static final Logger LOG = LoggerFactory.getLogger( BranchNode.class );
46
47
48 private Map<String, Node> children;
49
50
51
52
53
54 public BranchNode()
55 {
56 children = new HashMap<String, Node>(3);
57 }
58
59
60
61
62
63 public boolean isLeaf()
64 {
65 return false;
66 }
67
68
69
70
71
72
73
74
75
76
77
78
79
80 public BranchNode recursivelyAddPartition( BranchNode current, LdapDN dn, int index, Partition partition ) throws NamingException
81 {
82 String rdnAtIndex = dn.getRdn( index ).toString();
83
84 if ( index == dn.size() - 1 )
85 {
86 return current.addNode( rdnAtIndex, new LeafNode( partition ) );
87 }
88 else
89 {
90 Node newNode = current.getChild( rdnAtIndex );
91
92 if ( newNode instanceof LeafNode )
93 {
94 String message = "Overlapping partitions are not allowed";
95 LOG.error( message );
96 throw new NamingException( message );
97 }
98
99 if ( newNode == null )
100 {
101 newNode = new BranchNode();
102 }
103
104 Node child = recursivelyAddPartition( (BranchNode)newNode, dn, index + 1, partition );
105 return current.addNode( rdnAtIndex, child );
106 }
107 }
108
109
110
111
112
113
114
115
116
117 public BranchNode addNode( String rdn, Node child )
118 {
119 children.put( rdn, child );
120 return this;
121 }
122
123
124
125
126
127
128
129
130
131 public boolean contains( String rdn )
132 {
133 return children.containsKey( rdn );
134 }
135
136
137
138
139
140
141
142
143 public Node getChild( String rdn )
144 {
145 if ( children.containsKey( rdn ) )
146 {
147 return children.get( rdn );
148 }
149
150 return null;
151 }
152
153
154
155
156
157 public String toString()
158 {
159 StringBuilder sb = new StringBuilder();
160 sb.append( "{" );
161 boolean isFirst = true;
162
163 for ( Node child:children.values() )
164 {
165 if ( isFirst )
166 {
167 isFirst = false;
168 }
169 else
170 {
171 sb.append( ", " );
172 }
173
174 if ( child instanceof BranchNode )
175 {
176 sb.append( "Branch: ").append( child.toString() );
177 }
178 else
179 {
180 sb.append( "Leaf: " ).append( "'" ).append( child.toString() ).append( "'" );
181 }
182 }
183
184 sb.append( "}" );
185 return sb.toString();
186 }
187 }