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 org.apache.directory.server.xdbm.AbstractTupleCursor;
023    import org.apache.directory.server.xdbm.Tuple;
024    import org.apache.directory.shared.ldap.cursor.InvalidCursorPositionException;
025    
026    
027    /**
028     * A cursor that converts SingletonOrOrderedSet objects in the value from a
029     * AvlTreeMap into Tuples with just K and V presuming that all the keys have
030     * no duplicates. 
031     *
032     * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
033     * @version $Rev$, $Date$
034     */
035    public class AvlTreeMapNoDupsWrapperCursor<K,V> extends AbstractTupleCursor<K, V>
036    {
037        private final AvlSingletonOrOrderedSetCursor<K, V> wrapped;
038        private final Tuple<K,V> returnedTuple = new Tuple<K, V>();
039        
040        
041        public AvlTreeMapNoDupsWrapperCursor( AvlSingletonOrOrderedSetCursor<K, V> wrapped )
042        {
043            this.wrapped = wrapped;
044        }
045        
046    
047        public void afterKey( K key ) throws Exception
048        {
049            wrapped.afterKey( key );
050        }
051    
052        
053        public void afterValue( K key, V value ) throws Exception
054        {
055            throw new UnsupportedOperationException( "This Cursor does not support duplicate keys." );
056        }
057    
058        
059        public void beforeKey( K key ) throws Exception
060        {
061            wrapped.beforeKey( key );
062        }
063    
064        
065        public void beforeValue( K key, V value ) throws Exception
066        {
067            throw new UnsupportedOperationException( "This Cursor does not support duplicate keys." );
068        }
069    
070        
071        public void after( Tuple<K, V> element ) throws Exception
072        {
073            wrapped.afterKey( element.getKey() );
074        }
075    
076        
077        public void afterLast() throws Exception
078        {
079            wrapped.afterLast();
080        }
081    
082        
083        public boolean available()
084        {
085            return wrapped.available();
086        }
087    
088        
089        public void before( Tuple<K, V> element ) throws Exception
090        {
091            wrapped.beforeKey( element.getKey() );
092        }
093    
094        
095        public void beforeFirst() throws Exception
096        {
097            wrapped.beforeFirst();
098        }
099    
100        
101        public boolean first() throws Exception
102        {
103            return wrapped.first();
104        }
105        
106    
107        public Tuple<K, V> get() throws Exception
108        {
109            if ( wrapped.available() )
110            {
111                Tuple<K, SingletonOrOrderedSet<V>> tuple = wrapped.get();
112                
113                if ( tuple.getValue().isOrderedSet() )
114                {
115                    System.out.println( "tuple key = " + tuple.getKey() );
116                    tuple.getValue().getOrderedSet().printTree();
117                }
118                
119                returnedTuple.setBoth( tuple.getKey(), tuple.getValue().getSingleton() );
120                return returnedTuple;
121            }
122            
123            throw new InvalidCursorPositionException();
124        }
125    
126        
127        public boolean isElementReused()
128        {
129            return true;
130        }
131    
132        
133        public boolean last() throws Exception
134        {
135            return wrapped.last();
136        }
137    
138        
139        public boolean next() throws Exception
140        {
141            return wrapped.next();
142        }
143    
144        
145        public boolean previous() throws Exception
146        {
147            return wrapped.previous();
148        }
149    }