[seam-commits] Seam SVN: r7722 - in trunk/src/main/org/jboss/seam: servlet and 1 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Thu Mar 27 11:55:22 EDT 2008


Author: pete.muir at jboss.org
Date: 2008-03-27 11:55:22 -0400 (Thu, 27 Mar 2008)
New Revision: 7722

Added:
   trunk/src/main/org/jboss/seam/util/SortItem.java
Removed:
   trunk/src/main/org/jboss/seam/util/SortItem.java
Modified:
   trunk/src/main/org/jboss/seam/Component.java
   trunk/src/main/org/jboss/seam/servlet/SeamFilter.java
   trunk/src/main/org/jboss/seam/util/Sorter.java
Log:
Better sorter (risky)

Modified: trunk/src/main/org/jboss/seam/Component.java
===================================================================
--- trunk/src/main/org/jboss/seam/Component.java	2008-03-27 14:50:42 UTC (rev 7721)
+++ trunk/src/main/org/jboss/seam/Component.java	2008-03-27 15:55:22 UTC (rev 7722)
@@ -957,44 +957,49 @@
 
    private List<Interceptor> newSort(List<Interceptor> list)
    {
-      List<SortItem<Interceptor>> siList = new ArrayList<SortItem<Interceptor>>();
-      Map<Class<?>,SortItem<Interceptor>> ht = new HashMap<Class<?>,SortItem<Interceptor>>();
-
-      for (Interceptor i : list)
+      
+      List<SortItem<Interceptor>> sortable = new ArrayList<SortItem<Interceptor>>();
+      
+      for (final Interceptor i : list)
       {
-         SortItem<Interceptor> si = new SortItem<Interceptor>(i);
-         siList.add(si);
-         ht.put( i.getUserInterceptorClass(), si );
-      }
+         
+         
+         sortable.add(new SortItem<Interceptor>()
+         {
+            
+            Class<?> clazz = i.getUserInterceptorClass();
+            org.jboss.seam.annotations.intercept.Interceptor interceptor = clazz.getAnnotation(org.jboss.seam.annotations.intercept.Interceptor.class);
 
-      for (SortItem<Interceptor> si : siList)
-      {
-         Class<?> clazz = si.getObj().getUserInterceptorClass();
-         if ( clazz.isAnnotationPresent(org.jboss.seam.annotations.intercept.Interceptor.class) )
-         {
-            org.jboss.seam.annotations.intercept.Interceptor interceptorAnn = clazz.getAnnotation(org.jboss.seam.annotations.intercept.Interceptor.class);
-            for (Class<?> cl : Arrays.asList( interceptorAnn.around() ) )
+            @Override
+            public List<Class> getAround()
             {
-               SortItem<Interceptor> sortItem = ht.get(cl);
-               if (sortItem!=null) si.getAround().add( sortItem );
+               return Arrays.asList(interceptor.around());
             }
-            for (Class<?> cl : Arrays.asList( interceptorAnn.within() ) )
+
+            @Override
+            protected Interceptor getObject()
             {
-               SortItem<Interceptor> sortItem = ht.get(cl);
-               if (sortItem!=null) si.getWithin().add( sortItem );
+               return i;
             }
-         }
-      }
 
-      Sorter<Interceptor> sList = new Sorter<Interceptor>();
-      siList = sList.sort(siList);
-
-      list.clear();
-      for (SortItem<Interceptor> si : siList)
-      {
-         list.add( si.getObj() );
+            @Override
+            public List<Class> getWithin()
+            {
+               return Arrays.asList(interceptor.within());
+            }
+            
+            @Override
+            public Class getKey()
+            {
+               return clazz;
+            }
+            
+         });
       }
-      return list ;
+      log.info(getName() + " unsorted interceptors: " + sortable);
+      List<Interceptor> temp = new Sorter<Interceptor>().sort(sortable);
+      log.info(getName() + " sorted interceptors: " + temp);
+      return temp;
    }
 
    private void initDefaultInterceptors()

Modified: trunk/src/main/org/jboss/seam/servlet/SeamFilter.java
===================================================================
--- trunk/src/main/org/jboss/seam/servlet/SeamFilter.java	2008-03-27 14:50:42 UTC (rev 7721)
+++ trunk/src/main/org/jboss/seam/servlet/SeamFilter.java	2008-03-27 15:55:22 UTC (rev 7722)
@@ -3,9 +3,7 @@
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
 
 import javax.servlet.Filter;
 import javax.servlet.FilterChain;
@@ -105,51 +103,60 @@
 
    private List<Filter> getSortedFilters()
    {
+      List<SortItem<Filter>> sortable = new ArrayList<SortItem<Filter>>(); 
       //retrieve the Filter instances from the application context
-      Map<String, SortItem<Filter>> sortItemsMap = new HashMap<String, SortItem<Filter>>();
-      List<SortItem<Filter>> sortItems = new ArrayList<SortItem<Filter>>();
       
-      for (String filterName : Init.instance().getInstalledFilters())
+      
+      for (final String filterName : Init.instance().getInstalledFilters())
       {
-         Filter filter = (Filter) Component.getInstance(filterName, ScopeType.APPLICATION);
-         boolean disabled = false;
-         if (filter instanceof AbstractFilter)
+         
+         sortable.add(new SortItem<Filter>()
          {
-             disabled = ((AbstractFilter) filter).isDisabled();
-         }
-         if (!disabled)
-         {
-             SortItem<Filter> si = new SortItem<Filter>(filter);         
-             sortItemsMap.put(filterName, si);
-             sortItems.add(si);
-         }
-      }
 
-      //create sort items
-      for (SortItem<Filter> sortItem : sortItems)
-      {
-         org.jboss.seam.annotations.web.Filter filterAnn = getFilterAnnotation(sortItem.getObj().getClass());
-         if ( filterAnn != null )
-         {
-            for (String s : Arrays.asList( filterAnn.around() ) )
+            private Filter filter = (Filter) Component.getInstance(filterName, ScopeType.APPLICATION);
+            
+            @Override
+            public List<String> getAround()
             {
-               SortItem<Filter> aroundSortItem = sortItemsMap.get(s);
-               if (sortItem!=null && aroundSortItem != null) sortItem.getAround().add( aroundSortItem );
+               return Arrays.asList( getFilterAnnotation( filter.getClass() ).around() );
             }
-            for (String s : Arrays.asList( filterAnn.within() ) )
+
+            @Override
+            protected Filter getObject()
             {
-               SortItem<Filter> withinSortItem = sortItemsMap.get(s);
-               if (sortItem!=null && withinSortItem != null) sortItem.getWithin().add( withinSortItem );
+               return filter;
             }
-         }
+
+            @Override
+            public List<String> getWithin()
+            {
+               return Arrays.asList( getFilterAnnotation( filter.getClass() ).within() );
+            }
+            
+            @Override
+            public boolean isAddable()
+            {
+               if (filter instanceof AbstractFilter)
+               {
+                  return !((AbstractFilter) filter).isDisabled();
+               }
+               else
+               {
+                  return true;
+               }
+            }
+            
+            @Override
+            public String getKey()
+            {
+               return filter.getClass().getName();
+            }
+ 
+            
+         });
       }
-
-      // Do the sort
-      Sorter<Filter> sList = new Sorter<Filter>();
-      sortItems = sList.sort(sortItems);
-      List<Filter> sorted = new ArrayList<Filter>();
-      for (SortItem<Filter> si: sortItems) sorted.add( si.getObj() );
-      return sorted;
+      return new Sorter<Filter>().sort(sortable);
+      
    }
    
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

Deleted: trunk/src/main/org/jboss/seam/util/SortItem.java
===================================================================
--- trunk/src/main/org/jboss/seam/util/SortItem.java	2008-03-27 14:50:42 UTC (rev 7721)
+++ trunk/src/main/org/jboss/seam/util/SortItem.java	2008-03-27 15:55:22 UTC (rev 7722)
@@ -1,33 +0,0 @@
-package org.jboss.seam.util;
-
-import java.util.ArrayList;
-import java.util.List;
-
-public class SortItem<T> 
-{
-   
-	private T obj;
-	private List<SortItem> around = new ArrayList<SortItem>();
-	private List<SortItem> within = new ArrayList<SortItem>();
-	
-	public SortItem(T obj)
-   {
-		this.obj = obj;
-	}
-   
-	public T getObj()
-   {
-		return obj;
-	}
-
-	public List<SortItem> getAround()
-   {
-		return around;
-	}
-   
-	public List<SortItem> getWithin()
-   {
-		return within;
-	}
-   
-}

Added: trunk/src/main/org/jboss/seam/util/SortItem.java
===================================================================
--- trunk/src/main/org/jboss/seam/util/SortItem.java	                        (rev 0)
+++ trunk/src/main/org/jboss/seam/util/SortItem.java	2008-03-27 15:55:22 UTC (rev 7722)
@@ -0,0 +1,44 @@
+package org.jboss.seam.util;
+
+import java.util.ArrayList;
+import java.util.List;
+
+
+public abstract class SortItem<T>
+{
+   
+   private List<SortItem> around = new ArrayList<SortItem>();
+   private List<SortItem> within = new ArrayList<SortItem>();
+
+   final List<SortItem> getAroundSortItems()
+   {
+      return around;
+   }
+   
+   final List<SortItem> getWithinSortItems()
+   {
+      return within;
+   }
+
+   public Object getKey()
+   {
+      return getObject().hashCode();
+   }
+   
+   protected abstract T getObject();
+   
+   public boolean isAddable()
+   {
+      return true;
+   }
+   
+   public abstract List<? extends Object> getAround();
+   public abstract List<? extends Object> getWithin();
+   
+   @Override
+   public String toString()
+   {
+      return getObject().toString() + " within [" + getWithin() + "] around [" + getAround() + "]";
+   }
+   
+}


Property changes on: trunk/src/main/org/jboss/seam/util/SortItem.java
___________________________________________________________________
Name: svn:mime-type
   + text/plain
Name: svn:keywords
   + Author Date Id Revision
Name: svn:eol-style
   + native

Modified: trunk/src/main/org/jboss/seam/util/Sorter.java
===================================================================
--- trunk/src/main/org/jboss/seam/util/Sorter.java	2008-03-27 14:50:42 UTC (rev 7721)
+++ trunk/src/main/org/jboss/seam/util/Sorter.java	2008-03-27 15:55:22 UTC (rev 7722)
@@ -1,54 +1,28 @@
 package org.jboss.seam.util;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 public class Sorter<T> 
 {
    
-	private List<SortItem<T>> list = null; //new ArrayList();
-
-	public List<SortItem<T>> sort(List<SortItem<T>> lst)
+	private void remove(SortItem<T> item, List<SortItem<T>> list)
    {
-		
-      this.list = lst;
-		List<SortItem<T>> res = new ArrayList<SortItem<T>>();
-		SortItem<T> inmost = null;
-		
-      do 
-      {	
-			inmost = getInmost();
-			if (inmost!=null)
-         {
-				res.add(inmost);
-				remove(inmost);
-			}
-		}
-      while ( !list.isEmpty() && inmost!=null );
-		
-      if ( !list.isEmpty() )
-      {
-			throw new IllegalArgumentException("Can not sort list:"+list);
-		}
-		
-      return res;		
-	}
-   
-	private void remove(SortItem<T> item)
-   {
 		list.remove(item);
 		for (SortItem<T> o: list)
       {
-			o.getWithin().remove(item);
+			o.getWithinSortItems().remove(item);
 		}
 	}
    
-	private SortItem<T> getInmost() 
+	private SortItem<T> getInmost(List<SortItem<T>> list) 
    {
 		SortItem<T> res=null;
 		for (SortItem<T> o: list)
       {
-			if ( o.getWithin().isEmpty() && nobodyWantsAround(o) )
+			if ( o.getWithinSortItems().isEmpty() && nobodyWantsAround(o, list) )
          {
 				res = o;
 				break;
@@ -57,12 +31,12 @@
 		return res;
 	}
    
-	private boolean nobodyWantsAround(SortItem<T> item)
+	private boolean nobodyWantsAround(SortItem<T> item, List<SortItem<T>> list)
    {
 		boolean res = true;
 		for (SortItem<T> o: list)
       {
-			if ( o.getAround().contains(item) )
+			if ( o.getAroundSortItems().contains(item) )
          {
 				res = false;
 				break;
@@ -71,4 +45,69 @@
 		return res;
 	}
    
+	public List<T> sort(List<SortItem<T>> sortable)
+	{
+      
+	   // Build a map of items
+      Map<Object, SortItem<T>> sortItemsMap = new HashMap<Object, SortItem<T>>();
+      for (SortItem<T> item : sortable)
+      {
+         if (item.isAddable())
+         {         
+             sortItemsMap.put(item.getKey(), item);
+         }
+      }
+      
+      List<SortItem<T>> sortItemsList = new ArrayList<SortItem<T>>();
+      
+      for ( Object key : sortItemsMap.keySet() )
+      {
+         SortItem<T> sortItem = sortItemsMap.get(key);
+         for ( Object aroundKey : sortItem.getAround() )
+         {
+            if ( sortItemsMap.get( aroundKey ) != null )
+            {
+               sortItem.getAroundSortItems().add( sortItemsMap.get(aroundKey) );
+            }
+         }
+         for ( Object withinKey : sortItem.getWithin() )
+         {
+            if ( sortItemsMap.get( withinKey ) != null )
+            {
+               sortItem.getWithinSortItems().add( sortItemsMap.get( withinKey ) );
+            }
+         }
+         sortItemsList.add(sortItem);
+      }
+
+      List<SortItem<T>> result = new ArrayList<SortItem<T>>();
+      SortItem<T> inmost = null;
+      
+      do 
+      {  
+         inmost = getInmost(sortItemsList);
+         if (inmost!=null)
+         {
+            result.add(inmost);
+            remove(inmost, sortItemsList);
+         }
+      }
+      while ( !sortItemsList.isEmpty() && inmost!=null );
+      
+      if ( !sortItemsList.isEmpty() )
+      {
+         throw new IllegalArgumentException("Can not sort list:" + sortItemsList);
+      }
+      
+      
+      List<T> sorted = new ArrayList<T>();
+      for (SortItem<T> sortItem : result)
+      {
+         sorted.add( sortItem.getObject() );
+      }
+      return sorted;
+	}
+	
+	
+	
 }




More information about the seam-commits mailing list