View Javadoc
1   /*
2    *   Licensed to the Apache Software Foundation (ASF) under one
3    *   or more contributor license agreements.  See the NOTICE file
4    *   distributed with this work for additional information
5    *   regarding copyright ownership.  The ASF licenses this file
6    *   to you under the Apache License, Version 2.0 (the
7    *   "License"); you may not use this file except in compliance
8    *   with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   *   Unless required by applicable law or agreed to in writing,
13   *   software distributed under the License is distributed on an
14   *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   *   KIND, either express or implied.  See the License for the
16   *   specific language governing permissions and limitations
17   *   under the License.
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   * An immutable AvlTree wrapping a singleton.
32   *
33   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
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       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
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       * {@inheritDoc}
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      * {@inheritDoc}
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      * {@inheritDoc}
120      */
121     public Comparator<K> getComparator()
122     {
123         return comparator;
124     }
125 
126 
127     /**
128      * {@inheritDoc}
129      */
130     public LinkedAvlNode<K> getFirst()
131     {
132         return singleton;
133     }
134 
135 
136     /**
137      * {@inheritDoc}
138      */
139     public List<K> getKeys()
140     {
141         return Collections.singletonList( singleton.getKey() );
142     }
143 
144 
145     /**
146      * {@inheritDoc}
147      */
148     public LinkedAvlNode<K> getLast()
149     {
150         return singleton;
151     }
152 
153 
154     /**
155      * {@inheritDoc}
156      */
157     public LinkedAvlNode<K> getRoot()
158     {
159         return singleton;
160     }
161 
162 
163     /**
164      * {@inheritDoc}
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 }