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.io.IOException;
24  
25  import org.apache.directory.api.ldap.model.constants.Loggers;
26  import org.apache.directory.api.ldap.model.cursor.AbstractCursor;
27  import org.apache.directory.api.ldap.model.cursor.CursorException;
28  import org.apache.directory.api.ldap.model.cursor.InvalidCursorPositionException;
29  import org.apache.directory.api.ldap.model.cursor.Tuple;
30  import org.apache.directory.api.ldap.model.exception.LdapException;
31  import org.slf4j.Logger;
32  import org.slf4j.LoggerFactory;
33  
34  
35  /**
36   * A cursor that converts SingletonOrOrderedSet objects in the value from a
37   * AvlTreeMap into Tuples with just K and V presuming that all the keys have
38   * no duplicates.
39   *
40   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
41   */
42  public class AvlTreeMapNoDupsWrapperCursor<K, V> extends AbstractCursor<Tuple<K, V>>
43  {
44      /** A dedicated log for cursors */
45      private static final Logger LOG_CURSOR = LoggerFactory.getLogger( Loggers.CURSOR_LOG.getName() );
46  
47      /** Speedup for logs */
48      private static final boolean IS_DEBUG = LOG_CURSOR.isDebugEnabled();
49  
50      private final AvlSingletonOrOrderedSetCursor<K, V> wrapped;
51      private final Tuple<K, V> returnedTuple = new Tuple<>();
52  
53  
54      public AvlTreeMapNoDupsWrapperCursor( AvlSingletonOrOrderedSetCursor<K, V> wrapped )
55      {
56          if ( IS_DEBUG )
57          {
58              LOG_CURSOR.debug( "Creating AvlTreeMapNoDupsWrapperCursor {}", this );
59          }
60  
61          this.wrapped = wrapped;
62      }
63  
64  
65      public void afterKey( K key ) throws Exception
66      {
67          wrapped.afterKey( key );
68      }
69  
70  
71      public void afterValue( K key, V value ) throws Exception
72      {
73          throw new UnsupportedOperationException( "This Cursor does not support duplicate keys." );
74      }
75  
76  
77      public void beforeKey( K key ) throws Exception
78      {
79          wrapped.beforeKey( key );
80      }
81  
82  
83      public void beforeValue( K key, V value ) throws Exception
84      {
85          throw new UnsupportedOperationException( "This Cursor does not support duplicate keys." );
86      }
87  
88  
89      /**
90       * {@inheritDoc}
91       */
92      public void after( Tuple<K, V> element ) throws LdapException, CursorException
93      {
94          wrapped.afterKey( element.getKey() );
95      }
96  
97  
98      /**
99       * {@inheritDoc}
100      */
101     public void afterLast() throws LdapException, CursorException
102     {
103         wrapped.afterLast();
104     }
105 
106 
107     /**
108      * {@inheritDoc}
109      */
110     public boolean available()
111     {
112         return wrapped.available();
113     }
114 
115 
116     /**
117      * {@inheritDoc}
118      */
119     public void before( Tuple<K, V> element ) throws LdapException, CursorException
120     {
121         wrapped.beforeKey( element.getKey() );
122     }
123 
124 
125     /**
126      * {@inheritDoc}
127      */
128     public void beforeFirst() throws LdapException, CursorException
129     {
130         wrapped.beforeFirst();
131     }
132 
133 
134     /**
135      * {@inheritDoc}
136      */
137     public boolean first() throws LdapException, CursorException
138     {
139         return wrapped.first();
140     }
141 
142 
143     /**
144      * {@inheritDoc}
145      */
146     public Tuple<K, V> get() throws CursorException
147     {
148         if ( wrapped.available() )
149         {
150             Tuple<K, SingletonOrOrderedSet<V>> tuple = wrapped.get();
151 
152             if ( tuple.getValue().isOrderedSet() )
153             {
154                 tuple.getValue().getOrderedSet().printTree();
155             }
156 
157             returnedTuple.setBoth( tuple.getKey(), tuple.getValue().getSingleton() );
158             return returnedTuple;
159         }
160 
161         throw new InvalidCursorPositionException();
162     }
163 
164 
165     /**
166      * {@inheritDoc}
167      */
168     public boolean last() throws LdapException, CursorException
169     {
170         return wrapped.last();
171     }
172 
173 
174     /**
175      * {@inheritDoc}
176      */
177     public boolean next() throws LdapException, CursorException
178     {
179         return wrapped.next();
180     }
181 
182 
183     /**
184      * {@inheritDoc}
185      */
186     public boolean previous() throws LdapException, CursorException
187     {
188         return wrapped.previous();
189     }
190 
191 
192     /**
193      * {@inheritDoc}
194      */
195     @Override
196     public void close() throws IOException
197     {
198         if ( IS_DEBUG )
199         {
200             LOG_CURSOR.debug( "Closing AvlTreeMapNoDupsWrapperCursor {}", this );
201         }
202 
203         wrapped.close();
204     }
205 
206 
207     /**
208      * {@inheritDoc}
209      */
210     @Override
211     public void close( Exception reason ) throws IOException
212     {
213         if ( IS_DEBUG )
214         {
215             LOG_CURSOR.debug( "Closing AvlTreeMapNoDupsWrapperCursor {}", this );
216         }
217 
218         wrapped.close( reason );
219     }
220 }