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  
21  package org.apache.directory.server.core.partition.impl.btree.mavibot;
22  
23  
24  import java.io.IOException;
25  
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.exception.LdapException;
29  import org.apache.directory.mavibot.btree.ValueCursor;
30  import org.apache.directory.server.i18n.I18n;
31  
32  
33  /**
34   * TODO ValueTreeCursor.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   */
38  public class ValueTreeCursor<V> extends AbstractCursor<V>
39  {
40  
41      private ValueCursor<V> wrapped;
42  
43      private V available;
44  
45      // marker to detect the availability (cause Mavibot supports null values also)
46      private V notAvailable = ( V ) new Object();
47  
48  
49      public ValueTreeCursor( ValueCursor<V> cursor )
50      {
51          this.wrapped = cursor;
52      }
53  
54  
55      @Override
56      public boolean available()
57      {
58          return ( available != notAvailable );
59      }
60  
61  
62      @Override
63      public void before( V element ) throws LdapException, CursorException
64      {
65          throw new UnsupportedOperationException( I18n.err( I18n.ERR_446 ) );
66      }
67  
68  
69      @Override
70      public void after( V element ) throws LdapException, CursorException
71      {
72          throw new UnsupportedOperationException( I18n.err( I18n.ERR_446 ) );
73      }
74  
75  
76      @Override
77      public void beforeFirst() throws LdapException, CursorException
78      {
79      }
80  
81  
82      @Override
83      public void afterLast() throws LdapException, CursorException
84      {
85          throw new UnsupportedOperationException( I18n.err( I18n.ERR_446 ) );
86      }
87  
88  
89      @Override
90      public boolean first() throws LdapException, CursorException
91      {
92          throw new UnsupportedOperationException( I18n.err( I18n.ERR_446 ) );
93      }
94  
95  
96      @Override
97      public boolean last() throws LdapException, CursorException
98      {
99          throw new UnsupportedOperationException( I18n.err( I18n.ERR_446 ) );
100     }
101 
102 
103     @Override
104     public boolean previous() throws LdapException, CursorException
105     {
106         try
107         {
108             if ( wrapped.hasPrev() )
109             {
110                 available = wrapped.prev();
111                 return true;
112             }
113             else
114             {
115                 available = notAvailable;
116                 return false;
117             }
118         }
119         catch ( IOException e )
120         {
121             throw new CursorException( e );
122         }
123     }
124 
125 
126     @Override
127     public boolean next() throws LdapException, CursorException
128     {
129         try
130         {
131             if ( wrapped.hasNext() )
132             {
133                 available = wrapped.next();
134                 return true;
135             }
136             else
137             {
138                 available = notAvailable;
139                 return false;
140             }
141         }
142         catch ( IOException e )
143         {
144             throw new CursorException( e );
145         }
146     }
147 
148 
149     @Override
150     public V get() throws CursorException
151     {
152         return available;
153     }
154 
155 }