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.avltree;
21
22
23 import java.util.Collections;
24 import java.util.Comparator;
25 import java.util.List;
26
27 import org.apache.directory.server.i18n.I18n;
28
29
30
31
32
33
34
35 public class AvlTreeSingleton<K> implements AvlTree<K>
36 {
37 private final LinkedAvlNode<K> singleton;
38 private final Comparator<K> comparator;
39
40
41 public AvlTreeSingleton( K key, Comparator<K> comparator )
42 {
43 this.singleton = new LinkedAvlNode<>( key );
44 this.comparator = comparator;
45 }
46
47
48
49
50
51 public LinkedAvlNode<K> find( K key )
52 {
53 if ( key != null && comparator.compare( key, singleton.key ) == 0 )
54 {
55 return singleton;
56 }
57
58 return null;
59 }
60
61
62
63
64
65 public LinkedAvlNode<K> findGreater( K key )
66 {
67 if ( key != null && comparator.compare( key, singleton.key ) < 0 )
68 {
69 return singleton;
70 }
71
72 return null;
73 }
74
75
76
77
78
79 public LinkedAvlNode<K> findGreaterOrEqual( K key )
80 {
81 if ( key != null && comparator.compare( key, singleton.key ) <= 0 )
82 {
83 return singleton;
84 }
85
86 return null;
87 }
88
89
90
91
92
93 public LinkedAvlNode<K> findLess( K key )
94 {
95 if ( key != null && comparator.compare( key, singleton.key ) > 0 )
96 {
97 return singleton;
98 }
99
100 return null;
101 }
102
103
104
105
106
107 public LinkedAvlNode<K> findLessOrEqual( K key )
108 {
109 if ( key != null && comparator.compare( key, singleton.key ) >= 0 )
110 {
111 return singleton;
112 }
113
114 return null;
115 }
116
117
118
119
120
121 public Comparator<K> getComparator()
122 {
123 return comparator;
124 }
125
126
127
128
129
130 public LinkedAvlNode<K> getFirst()
131 {
132 return singleton;
133 }
134
135
136
137
138
139 public List<K> getKeys()
140 {
141 return Collections.singletonList( singleton.getKey() );
142 }
143
144
145
146
147
148 public LinkedAvlNode<K> getLast()
149 {
150 return singleton;
151 }
152
153
154
155
156
157 public LinkedAvlNode<K> getRoot()
158 {
159 return singleton;
160 }
161
162
163
164
165
166 public int getSize()
167 {
168 return 1;
169 }
170
171
172 public K insert( K key )
173 {
174 throw new UnsupportedOperationException( I18n.err( I18n.ERR_444 ) );
175 }
176
177
178 public boolean isEmpty()
179 {
180 return false;
181 }
182
183
184 public void printTree()
185 {
186 System.out.println( "[ " + singleton + " ]" );
187 }
188
189
190 public K remove( K key )
191 {
192 throw new UnsupportedOperationException( I18n.err( I18n.ERR_444 ) );
193 }
194 }