001    /*
002     *   Licensed to the Apache Software Foundation (ASF) under one
003     *   or more contributor license agreements.  See the NOTICE file
004     *   distributed with this work for additional information
005     *   regarding copyright ownership.  The ASF licenses this file
006     *   to you under the Apache License, Version 2.0 (the
007     *   "License"); you may not use this file except in compliance
008     *   with the License.  You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     *   Unless required by applicable law or agreed to in writing,
013     *   software distributed under the License is distributed on an
014     *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     *   KIND, either express or implied.  See the License for the
016     *   specific language governing permissions and limitations
017     *   under the License.
018     *
019     */
020    package org.apache.directory.server.core.avltree;
021    
022    import java.util.Collections;
023    import java.util.Comparator;
024    import java.util.List;
025    
026    import org.apache.directory.server.i18n.I18n;
027    
028    
029    /**
030     * An immutable AvlTree wrapping a singleton.
031     *
032     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033     * @version $Rev$, $Date$
034     */
035    public class AvlTreeSingleton<K> implements AvlTree<K>
036    {
037        private final LinkedAvlNode<K> singleton;
038        private final Comparator<K> comparator;
039        
040        
041        public AvlTreeSingleton( K key, Comparator<K> comparator )
042        {
043            this.singleton = new LinkedAvlNode<K>( key );
044            this.comparator = comparator;
045        }
046        
047    
048        /**
049         * {@inheritDoc}
050         */
051        public LinkedAvlNode<K> find( K key )
052        {
053            if ( key != null && comparator.compare( key, singleton.key ) == 0 )
054            {
055                return singleton;
056            }
057    
058            return null;
059        }
060    
061        
062        /**
063         * {@inheritDoc}
064         */
065        public LinkedAvlNode<K> findGreater( K key )
066        {
067            if ( key != null && comparator.compare( key, singleton.key ) < 0 )
068            {
069                return singleton;
070            }
071    
072            return null;
073        }
074    
075        
076        /**
077         * {@inheritDoc}
078         */
079        public LinkedAvlNode<K> findGreaterOrEqual( K key )
080        {
081            if ( key != null && comparator.compare( key, singleton.key ) <= 0 )
082            {
083                return singleton;
084            }
085    
086            return null;
087        }
088        
089    
090        /**
091         * {@inheritDoc}
092         */
093        public LinkedAvlNode<K> findLess( K key )
094        {
095            if ( key != null && comparator.compare( key, singleton.key ) > 0 )
096            {
097                return singleton;
098            }
099    
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    }