Commits

Igor Baidiuk  committed bf04268 Draft

Refactored events so EventList only stores them, and FireEvent performs actual reordering and execution

  • Participants
  • Parent commits d46e9af

Comments (0)

Files changed (3)

 		</sequential>
 		
 		<fire-event name="bar" />
+		
+		<!-- Test context closure, complex one -->
+		
+		
 	</target>
 	
 	<property name="global.text" value="Global text" />

File src/org/antcontrib/events/EventList.java

 package org.antcontrib.events;
 
-import java.util.Collections;
 import java.util.List;
 import java.util.Map;
-import java.util.Iterator;
-import java.util.Comparator;
 import java.util.ArrayList;
 import java.util.HashMap;
 
 		events.add(e);
 	}
 	
-	public void execute(String order)
+	public List getEvents()
 	{
-		if (order == null)
-			order = "";
-		
-		List l = new ArrayList();
-		l.addAll(events);
-		
-		if (order.equals("ascending"))
-		{
-			Collections.sort(l, new Comparator()
-				{
-					public int compare(Object left, Object right)
-					{
-						return ((Event)left).getPriority() - ((Event)right).getPriority();
-					}
-				}
-			);
-		}
-		else if (order.equals("descending"))
-		{
-			Collections.sort(l, new Comparator()
-				{
-					public int compare(Object left, Object right)
-					{
-						return ((Event)right).getPriority() - ((Event)left).getPriority();
-					}
-				}
-			);
-		}
-		else if (!order.isEmpty() && !order.equals("unordered"))
-		{
-			throw new BuildException("Order must be either 'unordered', 'ascending' or 'descending'");
-		}
-		
-		for (Iterator i = l.iterator(); i.hasNext(); )
-			((Event)i.next()).performTasks();
+		return new ArrayList(events);
 	}  
 }

File src/org/antcontrib/events/FireEvent.java

 package org.antcontrib.events;
 
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Comparator;
+
 import org.apache.tools.ant.BuildException;
 import org.apache.tools.ant.Task;
 
 		if (name == null || name.isEmpty())
 			throw new BuildException("Event list name must be a nonempty string");
 		
-		EventList.get(getProject(), name).execute(order);
+		if (order == null)
+			order = "";
+		
+		List events = EventList.get(getProject(), name).getEvents();
+		
+		if (order.equals("ascending"))
+		{
+			Collections.sort(events, new Comparator()
+				{
+					public int compare(Object left, Object right)
+					{
+						return ((Event)left).getPriority() - ((Event)right).getPriority();
+					}
+				}
+			);
+		}
+		else if (order.equals("descending"))
+		{
+			Collections.sort(events, new Comparator()
+				{
+					public int compare(Object left, Object right)
+					{
+						return ((Event)right).getPriority() - ((Event)left).getPriority();
+					}
+				}
+			);
+		}
+		else if (!order.isEmpty() && !order.equals("unordered"))
+		{
+			throw new BuildException("Order must be either 'unordered', 'ascending' or 'descending'");
+		}
+		
+		for (Iterator i = events.iterator(); i.hasNext(); )
+			((Event)i.next()).performTasks();
 	}
 }