directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r759977 - in /directory/shared/branches/shared-replication/cursor: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/directory/ src/main/java/org/apache/directory/shared/ src/main/java/or...
Date Mon, 30 Mar 2009 14:10:19 GMT
Author: elecharny
Date: Mon Mar 30 14:10:18 2009
New Revision: 759977

URL: http://svn.apache.org/viewvc?rev=759977&view=rev
Log:
Moved core-cursor to a dedicated shared cursor sub project

Added:
    directory/shared/branches/shared-replication/cursor/   (with props)
    directory/shared/branches/shared-replication/cursor/pom.xml
    directory/shared/branches/shared-replication/cursor/src/
    directory/shared/branches/shared-replication/cursor/src/main/
    directory/shared/branches/shared-replication/cursor/src/main/java/
    directory/shared/branches/shared-replication/cursor/src/main/java/org/
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/AbstractCursor.java
  (with props)
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/ClosureMonitor.java
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/Cursor.java
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/CursorClosedException.java
  (with props)
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/CursorIterator.java
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/DefaultClosureMonitor.java
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/EmptyCursor.java
  (with props)
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InconsistentCursorStateException.java
  (with props)
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InvalidCursorPositionException.java
  (with props)
    directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/SingletonCursor.java
  (with props)
    directory/shared/branches/shared-replication/cursor/src/test/
    directory/shared/branches/shared-replication/cursor/src/test/java/

Propchange: directory/shared/branches/shared-replication/cursor/
------------------------------------------------------------------------------
--- svn:ignore (added)
+++ svn:ignore Mon Mar 30 14:10:18 2009
@@ -0,0 +1,4 @@
+target
+.classpath
+.project
+.settings

Added: directory/shared/branches/shared-replication/cursor/pom.xml
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-replication/cursor/pom.xml?rev=759977&view=auto
==============================================================================
--- directory/shared/branches/shared-replication/cursor/pom.xml (added)
+++ directory/shared/branches/shared-replication/cursor/pom.xml Mon Mar 30 14:10:18 2009
@@ -0,0 +1,35 @@
+<?xml version="1.0" encoding="ISO-8859-1"?>
+<!--
+    Licensed to the Apache Software Foundation (ASF) under one or more
+    contributor license agreements.  See the NOTICE file distributed with
+    this work for additional information regarding copyright ownership.
+    The ASF licenses this file to You under the Apache License, Version 2.0
+    (the "License"); you may not use this file except in compliance with
+    the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+    Unless required by applicable law or agreed to in writing, software
+    distributed under the License is distributed on an "AS IS" BASIS,
+    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+    See the License for the specific language governing permissions and
+    limitations under the License.
+-->
+<!-- $Rev:  $ $Date:  $ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
+  <modelVersion>4.0.0</modelVersion>
+  <parent>
+    <groupId>org.apache.directory.shared</groupId>
+    <artifactId>shared-parent</artifactId>
+    <version>0.9.14-SNAPSHOT</version>
+  </parent>
+  <artifactId>shared-cursor</artifactId>
+  <name>Cursor implementation</name>
+
+  <description>
+    Cursor interfaces
+  </description>
+  
+  <packaging>jar</packaging>
+</project>
+

Added: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/AbstractCursor.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/AbstractCursor.java?rev=759977&view=auto
==============================================================================
--- directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/AbstractCursor.java
(added)
+++ directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/AbstractCursor.java
Mon Mar 30 14:10:18 2009
@@ -0,0 +1,96 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.directory.shared.ldap.cursor;
+
+
+import java.util.Iterator;
+
+
+/**
+ * Simple class that contains often used Cursor code.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public abstract class AbstractCursor<E> implements Cursor<E>
+{
+    /** The default associated monitor */
+    private ClosureMonitor monitor = new DefaultClosureMonitor();
+
+    /**
+     * {@inheritDoc}
+     */
+    public final void setClosureMonitor( ClosureMonitor monitor )
+    {
+        if ( monitor == null )
+        {
+            throw new NullPointerException( "monitor" );
+        }
+        
+        this.monitor = monitor;
+    }
+    
+
+    /**
+     * Check that the cursor is not closed.
+     * 
+     * @param operation
+     * @throws Exception
+     */
+    protected final void checkNotClosed( String operation ) throws Exception
+    {
+        monitor.checkNotClosed();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public final boolean isClosed()
+    {
+        return monitor.isClosed();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void close( Exception cause ) throws Exception
+    {
+        monitor.close( cause );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void close() throws Exception
+    {
+        monitor.close();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public Iterator<E> iterator()
+    {
+        return new CursorIterator<E>( this );
+    }
+}

Propchange: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/AbstractCursor.java
------------------------------------------------------------------------------
    svn:executable = *

Added: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/ClosureMonitor.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/ClosureMonitor.java?rev=759977&view=auto
==============================================================================
--- directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/ClosureMonitor.java
(added)
+++ directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/ClosureMonitor.java
Mon Mar 30 14:10:18 2009
@@ -0,0 +1,81 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+package org.apache.directory.shared.ldap.cursor;
+
+
+/**
+ * A monitor used by Cursors to detect conditions when they should stop 
+ * performing some work during advance operations such as next(), previous(),
+ * first() etc, and release resources.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public interface ClosureMonitor
+{
+    /**
+     * Sets monitor state to closed, and sets the cause to a 
+     * CursorClosedException without an error message string.
+     */
+    void close();
+
+    
+    /**
+     * Sets monitor state to closed, and sets the cause to a 
+     * CursorClosedException with a specific error message string.
+     * 
+     * @param cause error message string
+     */
+    void close( String cause );
+    
+    
+    /**
+     * Sets monitor state to closed, and sets the cause to a specific 
+     * Exception.
+     * 
+     * @param cause the exception to associate with the closure
+     */
+    void close( Exception cause );
+    
+    
+    /**
+     * Gets whether the state of this ClosureMonitor is set to closed.
+     *
+     * @return true if state is closed, false if open
+     */
+    boolean isClosed();
+    
+    
+    /**
+     * Checks if state of this ClosureMonitor is set to closed and if so, 
+     * throws the causing Exception.
+     *
+     * @throws Exception the cause of the closure
+     */
+    void checkNotClosed() throws Exception;
+    
+    
+    /**
+     * Gets the cause of the closure.
+     *
+     * @return the causing Exception
+     */
+    Exception getCause();
+}

Added: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/Cursor.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/Cursor.java?rev=759977&view=auto
==============================================================================
--- directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/Cursor.java
(added)
+++ directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/Cursor.java
Mon Mar 30 14:10:18 2009
@@ -0,0 +1,226 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.directory.shared.ldap.cursor;
+
+
+
+/**
+ * A Cursor for bidirectional traversal over elements in a dataSet. Cursors
+ * unlike Iterators or Enumerations may advance to an element by calling
+ * next() or previous() which returns true or false if the request succeeds
+ * with a viable element at the new position.  Operations for relative
+ * positioning in larger increments are provided.  If the cursor can not
+ * advance, then the Cursor is either positioned before the first element or
+ * after the last element in which case the user of the Cursor must stop
+ * advancing in the respective direction.  If an advance succeeds a get()
+ * operation retrieves the current object at the Cursors position.
+ *
+ * Although this interface presumes Cursors can advance bidirectionally,
+ * implementations may restrict this by throwing
+ * UnsupportedOperationExceptions.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public interface Cursor<E> extends Iterable<E>
+{
+    /**
+     * Determines whether or not a call to get() will succeed.
+     *
+     * @return true if the cursor is valid get() will succeed, false otherwise
+     */
+    boolean available();
+
+    /**
+     * Prepares this Cursor, so a subsequent call to Cursor#next() with a
+     * true return value, will have positioned the Cursor on a dataSet 
+     * element equal to or less than the element argument but not greater.  
+     * A call to Cursor#previous() with a true return value will position 
+     * the Cursor on a dataSet element less than the argument.  If 
+     * Cursor#next() returns false then the Cursor is past the last element 
+     * and so all values in the dataSet are less than the argument.  If 
+     * Cursor#previous() returns false then the Cursor is positioned before 
+     * the first element and all elements in the dataSet are greater than 
+     * the argument.
+     *
+     * @param element the element to be positioned before
+     * @throws Exception with problems accessing the underlying btree
+     * @throws UnsupportedOperationException if this method is not supported
+     */
+    void before( E element ) throws Exception;
+
+
+    /**
+     * Prepares this Cursor, so a subsequent call to Cursor#previous() with a
+     * true return value, will have positioned the Cursor on a dataSet element
+     * equal to or less than the element argument but not greater. A call to
+     * Cursor#next() with a true return value will position the Cursor on a
+     * dataSet element greater than the argument.  If Cursor#next() returns
+     * false then the Cursor is past the last element and so all values in the
+     * dataSet are less than or equal to the argument.  If Cursor#previous()
+     * returns false then the Cursor is positioned before the first element
+     * and all elements in the dataSet are greater than the argument.
+     *
+     * @param element the element to be positioned after
+     * @throws Exception if there are problems positioning this cursor or if
+     * this Cursor is closed
+     * @throws UnsupportedOperationException if this method is not supported
+     */
+    void after( E element ) throws Exception;
+
+
+    /**
+     * Positions this Cursor before the first element.
+     *
+     * @throws Exception if there are problems positioning this cursor or if
+     * this Cursor is closed
+     * @throws UnsupportedOperationException if this method is not supported
+     */
+    void beforeFirst() throws Exception;
+
+
+    /**
+     * Positions this Cursor after the last element.
+     *
+     * @throws Exception if there are problems positioning this Cursor or if
+     * this Cursor is closed
+     * @throws UnsupportedOperationException if this method is not supported
+     */
+    void afterLast() throws Exception;
+
+
+    /**
+     * Positions this Cursor at the first element.
+     *
+     * @return true if the position has been successfully changed to the first
+     * element, false otherwise
+     * @throws Exception if there are problems positioning this Cursor or if
+     * this Cursor is closed
+     * @throws UnsupportedOperationException if this method is not supported
+     */
+    boolean first() throws Exception;
+
+
+    /**
+     * Positions this Cursor at the last element.
+     *
+     * @return true if the position has been successfully changed to the last
+     * element, false otherwise
+     * @throws Exception if there are problems positioning this Cursor or if
+     * this Cursor is closed
+     * @throws UnsupportedOperationException if this method is not supported
+     */
+    boolean last() throws Exception;
+
+
+    /**
+     * Checks if this Cursor is closed.  Calls to this operation should not
+     * fail with exceptions if and only if the cursor is in the closed state.
+     *
+     * @return true if this Cursor is closed, false otherwise
+     * @throws Exception if there are problems determining the cursor's closed state
+     * @throws UnsupportedOperationException if this method is not supported
+     */
+    boolean isClosed() throws Exception;
+
+
+    /**
+     * Advances this Cursor to the previous position.  If called before
+     * explicitly positioning this Cursor, the position is presumed to be
+     * after the last element and this method moves the cursor back to the
+     * last element.
+     *
+     * @return true if the advance succeeded, false otherwise
+     * @throws Exception if there are problems advancing to the next position
+     * @throws UnsupportedOperationException if this method is not supported
+     */
+    boolean previous() throws Exception;
+
+
+    /**
+     * Advances this Cursor to the next position.  If called before
+     * explicitly positioning this Cursor, the position is presumed to be
+     * before the first element and this method moves the cursor forward to
+     * the first element.
+     *
+     * @return true if the advance succeeded, false otherwise
+     * @throws Exception if there are problems advancing to this Cursor to
+     * the next position, or if this Cursor is closed
+     * @throws UnsupportedOperationException if this method is not supported
+     */
+    boolean next() throws Exception;
+
+
+    /**
+     * Gets the object at the current position.  Cursor implementations may
+     * choose to reuse element objects by re-populating them on advances
+     * instead of creating new objects on each advance.
+     *
+     * @return the object at the current position
+     * @throws Exception if the object at this Cursor's current position
+     * cannot be retrieved, or if this Cursor is closed
+     */
+    E get() throws Exception;
+
+
+    /**
+     * Gets whether or not this Cursor will return the same element object
+     * instance on get() operations for any position of this Cursor.  Some
+     * Cursor implementations may reuse the same element copying values into
+     * it for every position rather than creating and emit new element
+     * objects on each advance.  Some Cursor implementations may return
+     * different elements for each position yet the same element instance
+     * is returned for the same position. In these cases this method should
+     * return true.
+     *
+     * @return true if elements are reused by this Cursor
+     */
+    boolean isElementReused();
+
+
+    /**
+     * Closes this Cursor and frees any resources it my have allocated.
+     * Repeated calls to this method after this Cursor has already been
+     * called should not fail with exceptions.
+     *
+     * @throws Exception if for some reason this Cursor could not be closed
+     */
+    void close() throws Exception;
+
+
+    /**
+     * Closes this Cursor and frees any resources it my have allocated.
+     * Repeated calls to this method after this Cursor has already been
+     * called should not fail with exceptions.  The reason argument is 
+     * the Exception instance thrown instead of the standard 
+     * CursorClosedException.
+     *
+     * @param reason exception thrown when this Cursor is accessed after close
+     * @throws Exception if for some reason this Cursor could not be closed
+     */
+    void close( Exception reason ) throws Exception;
+    
+    
+    /**
+     * Sets a non-null closure monitor to associate with this Cursor.
+     *
+     * @param monitor the monitor to use for detecting Cursor close events
+     */
+    void setClosureMonitor( ClosureMonitor monitor );
+}

Added: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/CursorClosedException.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/CursorClosedException.java?rev=759977&view=auto
==============================================================================
--- directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/CursorClosedException.java
(added)
+++ directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/CursorClosedException.java
Mon Mar 30 14:10:18 2009
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.directory.shared.ldap.cursor;
+
+
+/**
+ * A specific form of IOException to note that an operation is being
+ * attempted on a closed Cursor.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class CursorClosedException extends Exception
+{
+    private static final long serialVersionUID = -5723233489761854394L;
+
+
+    public CursorClosedException()
+    {
+    }
+
+
+    public CursorClosedException( String s )
+    {
+        super( s );
+    }
+}

Propchange: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/CursorClosedException.java
------------------------------------------------------------------------------
    svn:executable = *

Added: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/CursorIterator.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/CursorIterator.java?rev=759977&view=auto
==============================================================================
--- directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/CursorIterator.java
(added)
+++ directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/CursorIterator.java
Mon Mar 30 14:10:18 2009
@@ -0,0 +1,89 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *  
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *  
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License. 
+ *  
+ */
+package org.apache.directory.shared.ldap.cursor;
+
+
+import java.util.Iterator;
+
+
+/**
+ * An Iterator over a Cursor so Cursors can be Iterable for using in foreach
+ * constructs.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $$Rev$$
+ */
+public class CursorIterator<E> implements Iterator<E>
+{
+    /** The inner cursor we will iterate */
+    private final Cursor<E> cursor;
+    
+    /** A flag used to store the cursor state */
+    private boolean available;
+
+
+    /**
+     * 
+     * Creates a new instance of CursorIterator.
+     *
+     * @param cursor The inner cursor
+     */
+    public CursorIterator( Cursor<E> cursor )
+    {
+        this.cursor = cursor;
+        this.available = cursor.available();
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean hasNext()
+    {
+        return available;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public E next()
+    {
+        try
+        {
+            E element = cursor.get();
+            available = cursor.next();
+            return element;
+        }
+        catch ( Exception e )
+        {
+            throw new RuntimeException( "Failure on underlying Cursor.", e );
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void remove()
+    {
+        throw new UnsupportedOperationException( "Underlying Cursor does not support removal."
);
+    }
+}

Added: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/DefaultClosureMonitor.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/DefaultClosureMonitor.java?rev=759977&view=auto
==============================================================================
--- directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/DefaultClosureMonitor.java
(added)
+++ directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/DefaultClosureMonitor.java
Mon Mar 30 14:10:18 2009
@@ -0,0 +1,127 @@
+/*
+ *   Licensed to the Apache Software Foundation (ASF) under one
+ *   or more contributor license agreements.  See the NOTICE file
+ *   distributed with this work for additional information
+ *   regarding copyright ownership.  The ASF licenses this file
+ *   to you under the Apache License, Version 2.0 (the
+ *   "License"); you may not use this file except in compliance
+ *   with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing,
+ *   software distributed under the License is distributed on an
+ *   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *   KIND, either express or implied.  See the License for the
+ *   specific language governing permissions and limitations
+ *   under the License.
+ *
+ */
+package org.apache.directory.shared.ldap.cursor;
+
+
+/**
+ * A basic ClosureMonitor that simply uses a boolean for state and a cause 
+ * exception.
+ * 
+ * Note that we consciously chose not to synchronize close() operations with
+ * checks to see if the monitor state is closed because it costs to 
+ * synchronize and it's OK for the Cursor not to stop immediately when close()
+ * is called.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class DefaultClosureMonitor implements ClosureMonitor
+{
+    /** Tells if the monitor is closed or not */
+    private boolean closed;
+    
+    /** If we get an exception, the cause is stored in this variable */
+    private Exception cause;
+
+    
+    /**
+     * {@inheritDoc}
+     */
+    public final void close()
+    {
+        // state check needed to "try" not to overwrite exception (lack of 
+        // synchronization may still allow overwriting but who cares that much
+        if ( ! closed )
+        {
+            // not going to sync because who cares if it takes a little longer 
+            // to stop but we need to set cause before toggling closed state 
+            // or else check for closure can throw null cause 
+            cause = new CursorClosedException();
+            closed = true;
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public final void close( final String cause )
+    {
+        // state check needed to "try" not to overwrite exception (lack of 
+        // synchronization may still allow overwriting but who cares that much
+        if ( ! closed )
+        {
+            // not going to sync because who cares if it takes a little longer 
+            // to stop but we need to set cause before toggling closed state 
+            // or else check for closure can throw null cause 
+            this.cause = new CursorClosedException( cause );
+            closed = true;
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public final void close( final Exception cause )
+    {
+        // state check needed to "try" not to overwrite exception (lack of 
+        // synchronization may still allow overwriting but who cares that much
+        if ( ! closed )
+        {
+            // not going to sync because who cares if it takes a little longer 
+            // to stop but we need to set cause before toggling closed state 
+            // or else check for closure can throw null cause 
+            this.cause = cause;
+            closed = true;
+        }
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public final Exception getCause()
+    {
+        return cause;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public final boolean isClosed()
+    {
+        return closed;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void checkNotClosed() throws Exception
+    {
+        // lack of synchronization may cause pass but eventually it will work
+        if ( closed )
+        {
+            throw cause;
+        }
+    }
+}

Added: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/EmptyCursor.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/EmptyCursor.java?rev=759977&view=auto
==============================================================================
--- directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/EmptyCursor.java
(added)
+++ directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/EmptyCursor.java
Mon Mar 30 14:10:18 2009
@@ -0,0 +1,131 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.directory.shared.ldap.cursor;
+
+
+/**
+ * An empty Cursor implementation.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class EmptyCursor<E> extends AbstractCursor<E>
+{
+    /**
+     * {@inheritDoc}
+     */
+    public boolean available()
+    {
+        return false;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    public void before( E element ) throws Exception
+    {
+        checkNotClosed( "before()" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void after( E element ) throws Exception
+    {
+        checkNotClosed( "after()" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void beforeFirst() throws Exception
+    {
+        checkNotClosed( "beforeFirst()" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public void afterLast() throws Exception
+    {
+        checkNotClosed( "afterLast()" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean first() throws Exception
+    {
+        checkNotClosed( "first()" );
+        return false;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean last() throws Exception
+    {
+        checkNotClosed( "last()" );
+        return false;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean previous() throws Exception
+    {
+        checkNotClosed( "previous()" );
+        return false;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean next() throws Exception
+    {
+        checkNotClosed( "next()" );
+        return false;
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public E get() throws Exception
+    {
+        checkNotClosed( "get()" );
+        throw new InvalidCursorPositionException( "This cursor is empty and cannot return
elements!" );
+    }
+
+
+    /**
+     * {@inheritDoc}
+     */
+    public boolean isElementReused()
+    {
+        return false;
+    }
+}

Propchange: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/EmptyCursor.java
------------------------------------------------------------------------------
    svn:executable = *

Added: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InconsistentCursorStateException.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InconsistentCursorStateException.java?rev=759977&view=auto
==============================================================================
--- directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InconsistentCursorStateException.java
(added)
+++ directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InconsistentCursorStateException.java
Mon Mar 30 14:10:18 2009
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.directory.shared.ldap.cursor;
+
+
+/**
+ * Thrown to indicate a condition in the Cursor where the state seems
+ * inconsistent based on internal accounting.  This may indicate the
+ * underlying structure has changed after the Cursor has been created.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class InconsistentCursorStateException extends Exception
+{
+    private static final long serialVersionUID = 6222645005251534704L;
+
+
+    public InconsistentCursorStateException()
+    {
+    }
+
+
+    public InconsistentCursorStateException( String s )
+    {
+        super( s );
+    }
+}
\ No newline at end of file

Propchange: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InconsistentCursorStateException.java
------------------------------------------------------------------------------
    svn:executable = *

Added: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InvalidCursorPositionException.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InvalidCursorPositionException.java?rev=759977&view=auto
==============================================================================
--- directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InvalidCursorPositionException.java
(added)
+++ directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InvalidCursorPositionException.java
Mon Mar 30 14:10:18 2009
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.directory.shared.ldap.cursor;
+
+
+/**
+ * Thrown to indicate an illegal position state for a Cursor when a call to
+ * get is made.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class InvalidCursorPositionException extends Exception
+{
+    private static final long serialVersionUID = 5730037129071653272L;
+
+
+    public InvalidCursorPositionException ()
+    {
+    }
+
+
+    public InvalidCursorPositionException ( String s )
+    {
+        super( s );
+    }
+}

Propchange: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/InvalidCursorPositionException.java
------------------------------------------------------------------------------
    svn:executable = *

Added: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/SingletonCursor.java
URL: http://svn.apache.org/viewvc/directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/SingletonCursor.java?rev=759977&view=auto
==============================================================================
--- directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/SingletonCursor.java
(added)
+++ directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/SingletonCursor.java
Mon Mar 30 14:10:18 2009
@@ -0,0 +1,242 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.directory.shared.ldap.cursor;
+
+
+import java.util.Comparator;
+
+
+/**
+ * A Cursor over a single element.
+ *
+ * @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
+ * @version $Rev$, $Date$
+ */
+public class SingletonCursor<E> extends AbstractCursor<E>
+{
+    private boolean beforeFirst = true;
+    private boolean afterLast;
+    private boolean onSingleton;
+    private final Comparator<E> comparator;
+    private final E singleton;
+
+
+    public SingletonCursor( E singleton )
+    {
+        this( singleton, null );
+    }
+
+
+    public SingletonCursor( E singleton, Comparator<E> comparator )
+    {
+        this.singleton = singleton;
+        this.comparator = comparator;
+    }
+
+
+    public boolean available()
+    {
+        return onSingleton;
+    }
+    
+
+    public void before( E element ) throws Exception
+    {
+        checkNotClosed( "before()" );
+
+        if ( comparator == null )
+        {
+            throw new UnsupportedOperationException(
+                    "Without a comparator I cannot advance to just before the specified element."
);
+        }
+
+        int comparison = comparator.compare( singleton, element );
+
+        if ( comparison < 0 )
+        {
+            first();
+        }
+        else
+        {
+            beforeFirst();
+        }
+    }
+
+
+    public void after( E element ) throws Exception
+    {
+        checkNotClosed( "after()" );
+
+        if ( comparator == null )
+        {
+            throw new UnsupportedOperationException(
+                    "Without a comparator I cannot advance to just after the specified element."
);
+        }
+
+        int comparison = comparator.compare( singleton, element );
+
+        if ( comparison > 0 )
+        {
+            first();
+        }
+        else
+        {
+            afterLast();
+        }
+    }
+
+
+    public void beforeFirst() throws Exception
+    {
+        checkNotClosed( "()" );
+        beforeFirst = true;
+        afterLast = false;
+        onSingleton = false;
+    }
+
+
+    public void afterLast() throws Exception
+    {
+        checkNotClosed( "()" );
+        beforeFirst = false;
+        afterLast = true;
+        onSingleton = false;
+    }
+
+
+    public boolean first() throws Exception
+    {
+        checkNotClosed( "()" );
+        beforeFirst = false;
+        onSingleton = true;
+        afterLast = false;
+        return true;
+    }
+
+
+    public boolean last() throws Exception
+    {
+        checkNotClosed( "()" );
+        beforeFirst = false;
+        onSingleton = true;
+        afterLast = false;
+        return true;
+    }
+
+
+    public boolean isFirst() throws Exception
+    {
+        checkNotClosed( "()" );
+        return onSingleton;
+    }
+
+
+    public boolean isLast() throws Exception
+    {
+        checkNotClosed( "()" );
+        return onSingleton;
+    }
+
+
+    public boolean isAfterLast() throws Exception
+    {
+        checkNotClosed( "()" );
+        return afterLast;
+    }
+
+
+    public boolean isBeforeFirst() throws Exception
+    {
+        checkNotClosed( "()" );
+        return beforeFirst;
+    }
+
+
+    public boolean previous() throws Exception
+    {
+        checkNotClosed( "()" );
+        if ( beforeFirst )
+        {
+            return false;
+        }
+
+        if ( afterLast )
+        {
+            beforeFirst = false;
+            onSingleton = true;
+            afterLast = false;
+            return true;
+        }
+
+        // must be on the singleton
+        beforeFirst = true;
+        onSingleton = false;
+        afterLast = false;
+        return false;
+    }
+
+
+    public boolean next() throws Exception
+    {
+        checkNotClosed( "()" );
+        if ( beforeFirst )
+        {
+            beforeFirst = false;
+            onSingleton = true;
+            afterLast = false;
+            return true;
+        }
+
+        if ( afterLast )
+        {
+            return false;
+        }
+
+        // must be on the singleton
+        beforeFirst = false;
+        onSingleton = false;
+        afterLast = true;
+        return false;
+    }
+
+
+    public E get() throws Exception
+    {
+        checkNotClosed( "()" );
+        if ( onSingleton )
+        {
+            return singleton;
+        }
+
+        if ( beforeFirst )
+        {
+            throw new InvalidCursorPositionException( "Cannot access element if positioned
before first." );
+        }
+        else
+        {
+            throw new InvalidCursorPositionException( "Cannot access element if positioned
after last." );
+        }
+    }
+
+
+    public boolean isElementReused()
+    {
+        return true;
+    }
+}

Propchange: directory/shared/branches/shared-replication/cursor/src/main/java/org/apache/directory/shared/ldap/cursor/SingletonCursor.java
------------------------------------------------------------------------------
    svn:executable = *



Mime
View raw message