directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r349900 - in /directory/network/branches/chain_refactor/src/java/org/apache/mina/common: DefaultIoFilterChainBuilder.java IoFilterChain.java support/AbstractIoFilterChain.java
Date Wed, 30 Nov 2005 06:59:08 GMT
Author: trustin
Date: Tue Nov 29 22:58:56 2005
New Revision: 349900

URL: http://svn.apache.org/viewcvs?rev=349900&view=rev
Log:
* Added DefaultIoFilterChainBuilder
* Removed IoFilterChain manipulation methods based on IoFilter reference

Added:
    directory/network/branches/chain_refactor/src/java/org/apache/mina/common/DefaultIoFilterChainBuilder.java
  (with props)
Modified:
    directory/network/branches/chain_refactor/src/java/org/apache/mina/common/IoFilterChain.java
    directory/network/branches/chain_refactor/src/java/org/apache/mina/common/support/AbstractIoFilterChain.java

Added: directory/network/branches/chain_refactor/src/java/org/apache/mina/common/DefaultIoFilterChainBuilder.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/chain_refactor/src/java/org/apache/mina/common/DefaultIoFilterChainBuilder.java?rev=349900&view=auto
==============================================================================
--- directory/network/branches/chain_refactor/src/java/org/apache/mina/common/DefaultIoFilterChainBuilder.java
(added)
+++ directory/network/branches/chain_refactor/src/java/org/apache/mina/common/DefaultIoFilterChainBuilder.java
Tue Nov 29 22:58:56 2005
@@ -0,0 +1,260 @@
+package org.apache.mina.common;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.IdentityHashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ListIterator;
+import java.util.Map;
+
+import org.apache.mina.common.IoFilter.NextFilter;
+import org.apache.mina.common.IoFilterChain.Entry;
+
+public class DefaultIoFilterChainBuilder implements IoFilterChainBuilder
+{
+    private List entries = new ArrayList();
+    private final Map entriesByName = new HashMap();
+    private final Map entriesByFilter = new IdentityHashMap();
+    
+    /**
+     * Creates a new instance with an empty filter list.
+     */
+    public DefaultIoFilterChainBuilder()
+    {
+    }
+
+    /**
+     * Returns the {@link IoFilter} with the specified <tt>name</tt> in the filter
+     * list.
+     * 
+     * @return <tt>null</tt> if there's no such filter in the filter list
+     */
+    public synchronized Entry get( String name )
+    {
+        return ( Entry ) entriesByName.get( name );
+    }
+    
+    /**
+     * Returns the name of the specified {@link IoFilter} in the filter list.
+     * @return <tt>null</tt> if there's no such filter in the filter list.
+     */
+    public synchronized Entry get( IoFilter filter )
+    {
+        return ( Entry ) entriesByFilter.get( filter );
+    }
+    
+    /**
+     * Returns the list of all {@link Entry}s the filter list contains.
+     */
+    public List getAll()
+    {
+        return new ArrayList( entries );
+    }
+    
+    /**
+     * Returns the reversed list of all {@link Entry}s the filter list contains.
+     */
+    public List getAllReversed()
+    {
+        List result = getAll();
+        Collections.reverse( result );
+        return result;
+    }
+
+    /**
+     * Adds the specified filter with the specified name at the beginning
+     * of the filter list.
+     */
+    public synchronized void addFirst( String name, IoFilter filter )
+    {
+        register( 0, new EntryImpl( name, filter ) );
+    }
+    
+    /**
+     * Adds the specified filter with the specified name at the end of
+     * the filter list.
+     */
+    public synchronized void addLast( String name, IoFilter filter )
+    {
+        register( entries.size(), new EntryImpl( name, filter ) );
+    }
+
+    /**
+     * Adds the specified filter with the specified name just before the filter whose name
is
+     * <code>baseName</code> in this chain.
+     */
+    public synchronized void addBefore( String baseName, String name, IoFilter filter )
+    {
+        checkBaseName( baseName );
+        
+        for( ListIterator i = entries.listIterator(); i.hasNext(); )
+        {
+            Entry base = ( Entry ) i.next();
+            if( base.getName().equals( baseName ) )
+            {
+                register( i.previousIndex(), new EntryImpl( name, filter ) );
+                break;
+            }
+        }
+    }
+
+    /**
+     * Adds the specified filter with the specified name just after the filter whose name
is
+     * <code>baseName</code> in this chain.
+     */
+    public synchronized void addAfter( String baseName, String name, IoFilter filter )
+    {
+        checkBaseName( baseName );
+        
+        List entries = new ArrayList( this.entries );
+        
+        for( ListIterator i = entries.listIterator(); i.hasNext(); )
+        {
+            Entry base = ( Entry ) i.next();
+            if( base.getName().equals( baseName ) )
+            {
+                register( i.nextIndex(), new EntryImpl( name, filter ) );
+                break;
+            }
+        }
+    }
+
+    /**
+     * Removes the filter with the specified name from this chain.
+     */
+    public synchronized IoFilter remove( String name )
+    {
+        if( name == null )
+        {
+            throw new NullPointerException( "name" );
+        }
+
+        for( ListIterator i = entries.listIterator(); i.hasNext(); )
+        {
+            Entry e = ( Entry ) i.next();
+            if( e.getName().equals( name ) )
+            {
+                deregister( i.previousIndex(), e );
+                return e.getFilter();
+            }
+        }
+        
+        throw new IllegalArgumentException( "Unknown filter name: " + name );
+    }
+
+    /**
+     * Removes the specifiec <tt>filter</tt> from this chain.
+     * 
+     * @return the name of the removed {@link IoFilter}
+     */
+    public synchronized String remove( IoFilter filter )
+    {
+        if( filter == null )
+        {
+            throw new NullPointerException( "filter" );
+        }
+        
+        Entry e = get( filter );
+        if( e == null )
+        {
+            throw new IllegalArgumentException( "The specified filter doesn't belong to this
builder." );
+        }
+        
+        String name = e.getName();
+        remove( name );
+        return name;
+    }
+
+    /**
+     * Removes all filters added to this chain.
+     * @throws Exception if {@link IoFilter#destroy(IoFilterChain, NextFilter)} thrown an
exception.
+     */
+    public synchronized void clear() throws Exception
+    {
+        entries = new ArrayList();
+        entriesByName.clear();
+        entriesByFilter.clear();
+    }
+    
+    public void buildFilterChain( IoFilterChain chain ) throws Exception
+    {
+        for( Iterator i = entries.iterator(); i.hasNext(); )
+        {
+            Entry e = ( Entry ) i.next();
+            chain.addLast( e.getName(), e.getFilter() );
+        }
+    }
+
+    private void checkBaseName( String baseName )
+    {
+        if( baseName == null )
+        {
+            throw new NullPointerException( "baseName" );
+        }
+        if( !entriesByName.containsKey( baseName ) )
+        {
+            throw new IllegalArgumentException( "Unknown filter name: " + baseName );
+        }
+    }
+
+    private void register( int index, Entry e )
+    {
+        if( entriesByName.containsKey( e.getName() ) )
+        {
+            throw new IllegalArgumentException( "Other filter is using the same name: " +
e.getName() );
+        }
+
+        List newEntries = new ArrayList( entries );
+        newEntries.add( index, e );
+        this.entries = newEntries;
+        entriesByName.put( e.getName(), e );
+        entriesByFilter.put( e.getFilter(), e );
+    }
+    
+    private void deregister( int index, Entry e )
+    {
+        List newEntries = new ArrayList( entries );
+        newEntries.remove( index );
+        this.entries = newEntries;
+        entriesByName.remove( e.getName() );
+        entriesByFilter.remove( e.getFilter() );
+    }
+
+    public static class EntryImpl implements Entry
+    {
+        private final String name;
+        private final IoFilter filter;
+        
+        private EntryImpl( String name, IoFilter filter )
+        {
+            if( name == null )
+            {
+                throw new NullPointerException( "name" );
+            }
+            if( filter == null )
+            {
+                throw new NullPointerException( "filter" );
+            }
+            
+            this.name = name;
+            this.filter = filter;
+        }
+
+        public String getName()
+        {
+            return name;
+        }
+        
+        public IoFilter getFilter()
+        {
+            return filter;
+        }
+
+        public NextFilter getNextFilter()
+        {
+            throw new IllegalStateException();
+        }
+    }
+}

Propchange: directory/network/branches/chain_refactor/src/java/org/apache/mina/common/DefaultIoFilterChainBuilder.java
------------------------------------------------------------------------------
    svn:keywords = HeadURL Id LastChangedBy LastChangedDate LastChangedRevision

Modified: directory/network/branches/chain_refactor/src/java/org/apache/mina/common/IoFilterChain.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/chain_refactor/src/java/org/apache/mina/common/IoFilterChain.java?rev=349900&r1=349899&r2=349900&view=diff
==============================================================================
--- directory/network/branches/chain_refactor/src/java/org/apache/mina/common/IoFilterChain.java
(original)
+++ directory/network/branches/chain_refactor/src/java/org/apache/mina/common/IoFilterChain.java
Tue Nov 29 22:58:56 2005
@@ -43,12 +43,6 @@
     Entry get( String name );
     
     /**
-     * Returns the {@link Entry} with the specified {@link IoFilter} in this chain.
-     * @return <tt>null</tt> if there's no such filter in this chain.
-     */
-    Entry get( IoFilter filter );
-    
-    /**
      * Returns the list of all {@link Entry}s this chain contains.
      */
     List getAll();
@@ -78,25 +72,11 @@
     void addBefore( String baseName, String name, IoFilter filter ) throws Exception;
 
     /**
-     * Adds the specified filter with the specified name just before the specified
-     * <code>baseFilter</code> in this chain.
-     * @throws Exception if {@link IoFilter#init(IoFilterChain, NextFilter)} thrown an exception.
-     */
-    void addBefore( IoFilter baseFilter, String name, IoFilter filter ) throws Exception;
-
-    /**
      * Adds the specified filter with the specified name just after the filter whose name
is
      * <code>baseName</code> in this chain.
      * @throws Exception if {@link IoFilter#init(IoFilterChain, NextFilter)} thrown an exception.
      */
     void addAfter( String baseName, String name, IoFilter filter ) throws Exception;
-
-    /**
-     * Adds the specified filter with the specified name just after the
-     * specified <tt>baseFilter</tt> in this chain.
-     * @throws Exception if {@link IoFilter#init(IoFilterChain, NextFilter)} thrown an exception.
-     */
-    void addAfter( IoFilter baseFilter, String name, IoFilter filter ) throws Exception;
 
     /**
      * Removes the filter with the specified name from this chain.

Modified: directory/network/branches/chain_refactor/src/java/org/apache/mina/common/support/AbstractIoFilterChain.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/chain_refactor/src/java/org/apache/mina/common/support/AbstractIoFilterChain.java?rev=349900&r1=349899&r2=349900&view=diff
==============================================================================
--- directory/network/branches/chain_refactor/src/java/org/apache/mina/common/support/AbstractIoFilterChain.java
(original)
+++ directory/network/branches/chain_refactor/src/java/org/apache/mina/common/support/AbstractIoFilterChain.java
Tue Nov 29 22:58:56 2005
@@ -251,17 +251,6 @@
         return e;
     }
     
-    public Entry get( IoFilter filter )
-    {
-        Entry e = ( Entry ) filter2entry.get( filter );
-        if( e == null )
-        {
-            return null;
-        }
-        
-        return e;
-    }
-    
     public synchronized void addFirst( String name,
                                        IoFilter filter ) throws Exception
     {
@@ -285,29 +274,11 @@
         register( baseEntry.prevEntry, name, filter );
     }
 
-    public synchronized void addBefore( IoFilter baseFilter,
-                                        String name,
-                                        IoFilter filter ) throws Exception
-    {
-        EntryImpl baseEntry = getEntry( baseFilter );
-        checkAddable( name );
-        register( baseEntry.prevEntry, name, filter );
-    }
-
     public synchronized void addAfter( String baseName,
                                        String name,
                                        IoFilter filter ) throws Exception
     {
         EntryImpl baseEntry = checkOldName( baseName );
-        checkAddable( name );
-        register( baseEntry, name, filter );
-    }
-
-    public synchronized void addAfter( IoFilter baseFilter,
-                                       String name,
-                                       IoFilter filter ) throws Exception
-    {
-        EntryImpl baseEntry = getEntry( baseFilter );
         checkAddable( name );
         register( baseEntry, name, filter );
     }



Mime
View raw message