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.xdbm.search.impl;
21  
22  
23  import org.apache.directory.server.xdbm.IndexEntry;
24  import org.apache.directory.server.xdbm.Store;
25  import org.apache.directory.server.xdbm.AbstractIndexCursor;
26  import org.apache.directory.server.xdbm.IndexCursor;
27  import org.apache.directory.server.xdbm.search.Evaluator;
28  import org.apache.directory.server.core.cursor.InvalidCursorPositionException;
29  import org.apache.directory.server.core.entry.ServerEntry;
30  import org.apache.directory.shared.ldap.filter.ExprNode;
31  
32  
33  /**
34   * A Cursor returning candidates satisfying a logical negation expression.
35   *
36   * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
37   * @version $$Rev$$
38   */
39  public class NotCursor<V> extends AbstractIndexCursor<V, ServerEntry>
40  {
41      private static final String UNSUPPORTED_MSG =
42          "NotCursors are not ordered and do not support positioning by element.";
43      private final IndexCursor<V,ServerEntry> ndnCursor;
44      private final Evaluator<? extends ExprNode, ServerEntry> childEvaluator;
45      private boolean available = false;
46  
47  
48      @SuppressWarnings("unchecked")
49      public NotCursor( Store<ServerEntry> db,
50                        Evaluator<? extends ExprNode, ServerEntry> childEvaluator ) throws Exception
51      {
52          this.childEvaluator = childEvaluator;
53          this.ndnCursor = ( IndexCursor<V,ServerEntry> ) db.getNdnIndex().forwardCursor();
54      }
55  
56  
57      public boolean available()
58      {
59          return available;
60      }
61  
62  
63      public void beforeValue( Long id, V value ) throws Exception
64      {
65          throw new UnsupportedOperationException( UNSUPPORTED_MSG );
66      }
67  
68  
69      public void before( IndexEntry<V, ServerEntry> element ) throws Exception
70      {
71          throw new UnsupportedOperationException( UNSUPPORTED_MSG );
72      }
73  
74  
75      public void after( IndexEntry<V, ServerEntry> element ) throws Exception
76      {
77          throw new UnsupportedOperationException( UNSUPPORTED_MSG );
78      }
79  
80  
81      public void afterValue( Long id, V value ) throws Exception
82      {
83          throw new UnsupportedOperationException( UNSUPPORTED_MSG );
84      }
85  
86  
87      public void beforeFirst() throws Exception
88      {
89          checkNotClosed( "beforeFirst()" );
90          ndnCursor.beforeFirst();
91          available = false;
92      }
93  
94  
95      public void afterLast() throws Exception
96      {
97          checkNotClosed( "afterLast()" );
98          ndnCursor.afterLast();
99          available = false;
100     }
101 
102 
103     public boolean first() throws Exception
104     {
105         beforeFirst();
106         return next();
107     }
108 
109 
110     public boolean last() throws Exception
111     {
112         afterLast();
113         return previous();
114     }
115 
116 
117     public boolean previous() throws Exception
118     {
119         while ( ndnCursor.previous() )
120         {
121             checkNotClosed( "previous()" );
122             IndexEntry<?,ServerEntry> candidate = ndnCursor.get();
123             if ( ! childEvaluator.evaluate( candidate ) )
124             {
125                 return available = true;
126             }
127         }
128 
129         return available = false;
130     }
131 
132 
133     public boolean next() throws Exception
134     {
135         while ( ndnCursor.next() )
136         {
137             checkNotClosed( "next()" );
138             IndexEntry<?,ServerEntry> candidate = ndnCursor.get();
139             if ( ! childEvaluator.evaluate( candidate ) )
140             {
141                 return available = true;
142             }
143         }
144 
145         return available = false;
146     }
147 
148 
149     public IndexEntry<V, ServerEntry> get() throws Exception
150     {
151         checkNotClosed( "get()" );
152         if ( available )
153         {
154             return ndnCursor.get();
155         }
156 
157         throw new InvalidCursorPositionException( "Cursor has not been positioned yet." );
158     }
159 
160 
161     public boolean isElementReused()
162     {
163         return ndnCursor.isElementReused();
164     }
165 
166 
167     public void close() throws Exception
168     {
169         super.close();
170         ndnCursor.close();
171     }
172 }