labs-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From simo...@apache.org
Subject svn commit: r740944 - in /labs/magma/trunk/foundation-basics/src: main/java/org/apache/magma/basics/context/ test/java/org/apache/magma/basics/context/
Date Thu, 05 Feb 2009 00:10:21 GMT
Author: simoneg
Date: Thu Feb  5 00:10:21 2009
New Revision: 740944

URL: http://svn.apache.org/viewvc?rev=740944&view=rev
Log:
LABS-279 : better handling of nested subcontext and relevant tests

Modified:
    labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/basics/context/RunningContext.java
    labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/basics/context/SubRunningContext.java
    labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/basics/context/RunningContextTest.java

Modified: labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/basics/context/RunningContext.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/basics/context/RunningContext.java?rev=740944&r1=740943&r2=740944&view=diff
==============================================================================
--- labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/basics/context/RunningContext.java
(original)
+++ labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/basics/context/RunningContext.java
Thu Feb  5 00:10:21 2009
@@ -76,9 +76,7 @@
 		RunningContext context = getMain();
 		SubRunningContext sr = context.saveds.get(co);
 		if (sr == null) {
-			sr = new SubRunningContext();
-			sr.addAll(context.currents.peek());
-			sr.setOwner(co);
+			sr = new SubRunningContext(context.currents.peek(), co);
 			context.saveds.put(co, sr);
 		}
 		context.currents.push(sr);
@@ -102,9 +100,7 @@
 	public static void createFor(ContextOwner co) {
 		RunningContext context = getMain();
 		if (!context.saveds.containsKey(co)) {
-			SubRunningContext sr = new SubRunningContext();
-			sr.addAll(context.currents.peek());
-			sr.setOwner(co);
+			SubRunningContext sr = new SubRunningContext(context.currents.peek(), co);
 			context.saveds.put(co, sr);
 		}		
 	}

Modified: labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/basics/context/SubRunningContext.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/basics/context/SubRunningContext.java?rev=740944&r1=740943&r2=740944&view=diff
==============================================================================
--- labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/basics/context/SubRunningContext.java
(original)
+++ labs/magma/trunk/foundation-basics/src/main/java/org/apache/magma/basics/context/SubRunningContext.java
Thu Feb  5 00:10:21 2009
@@ -2,6 +2,7 @@
 
 import java.lang.reflect.Method;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.EmptyStackException;
 import java.util.List;
 import java.util.Stack;
@@ -18,9 +19,16 @@
 public class SubRunningContext extends Stack<ContextElement> {
 
 	private ContextOwner owner;
+	private int prevpart = -1;
 	
 	protected SubRunningContext() {
 	}
+	
+	protected SubRunningContext(SubRunningContext prev, ContextOwner owner) {
+		this.addAll(prev);
+		this.owner = owner;
+		this.prevpart = prev.size();
+	}
 
 	/**
 	 * Deprecates the normal {@link Stack#pop()}, the overridden pop methos should be used to
check for leaks.
@@ -56,8 +64,23 @@
 	 */
 	@SuppressWarnings("unchecked")
 	public <SubElement extends ContextElement> List<SubElement> getElements(Class<SubElement>
clazz) {
+		return getElements(clazz, false);
+	}
+
+	/**
+	 * Searches for {@link ContextElement}s on the stack of the given class or implementing
the given interface.
+	 * @param <SubElement> The {@link ContextElement} subclass or interface to search
for.
+	 * @param clazz The {@link ContextElement} subclass or interface to search for.
+	 * @param stopping If true, stop where the last {@link SavePointContextElement} is found,
to limit the scope to the common part
+	 * @return A {@link List} of found {@link ContextElement}s.
+	 */
+	@SuppressWarnings("unchecked")
+	private <SubElement extends ContextElement> List<SubElement> getElements(Class<SubElement>
clazz, boolean stopping) {
 		List<SubElement> ret = null;
-		for (ContextElement ele : this) {
+		int lim = this.size();
+		if (stopping) lim = this.prevpart;
+		for (int i = 0; i < lim; i++) {
+			ContextElement ele = get(i);
 			if (clazz.isAssignableFrom(ele.getClass())) {
 				if (ret == null) {
 					ret = new ArrayList<SubElement>();
@@ -65,9 +88,43 @@
 				ret.add((SubElement)ele);
 			}
 		}
+		if (ret == null) return Collections.EMPTY_LIST;
 		return ret;
 	}
 	
+	
+	
+	/**
+	 * Searches for {@link ContextElement}s on the stack of the given class or implementing
the given interface up to
+	 * the last {@link SavePointContextElement}, which means only the part common with all parent
context.
+	 * @param <SubElement> The {@link ContextElement} subclass or interface to search
for.
+	 * @param clazz The {@link ContextElement} subclass or interface to search for.
+	 * @return A {@link List} of found {@link ContextElement}s.
+	 */
+	@SuppressWarnings("unchecked")
+	public <SubElement extends ContextElement> List<SubElement> getCommonElements(Class<SubElement>
clazz) {
+		return getElements(clazz, true);
+	}
+	
+	
+	/**
+	 * Searches for the most recent {@link ContextElement} on the stack implementing or extending
the given class or interface.
+	 * @param <SubElement> The {@link ContextElement} subclass of interface to search
for.
+	 * @param clazz The {@link ContextElement} subclass of interface to search for.
+	 * @return The most recent element, or null if not found.
+	 */
+	public <SubElement extends ContextElement> SubElement getLast(Class<SubElement>
clazz) {
+		SubElement ret = null;
+		for (ContextElement ele : this) {
+			if (clazz.isAssignableFrom(ele.getClass())) {
+				ret = (SubElement)ele;
+			}
+		}
+		return ret;
+	}
+	
+	
+	
 	/**
 	 * Shortcut method for popping a {@link StringContextElement} from the stack.
 	 */

Modified: labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/basics/context/RunningContextTest.java
URL: http://svn.apache.org/viewvc/labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/basics/context/RunningContextTest.java?rev=740944&r1=740943&r2=740944&view=diff
==============================================================================
--- labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/basics/context/RunningContextTest.java
(original)
+++ labs/magma/trunk/foundation-basics/src/test/java/org/apache/magma/basics/context/RunningContextTest.java
Thu Feb  5 00:10:21 2009
@@ -31,7 +31,20 @@
 		context2.push("in sub context");
 		
 		assertEquals(3, context2.getElements(StringContextElement.class).size());
+		assertEquals(2, context2.getCommonElements(StringContextElement.class).size());
 
+		FakeContextOwner co2 = new FakeContextOwner();
+		RunningContext.entering(co2);
+		
+		SubRunningContext context3 = RunningContext.get();
+		assertNotSame(context, context3);
+		assertNotSame(context2, context3);
+		
+		context3.push("in sub sub context");
+		assertEquals(4, context3.getElements(StringContextElement.class).size());
+		assertEquals(3, context3.getCommonElements(StringContextElement.class).size());
+		
+		RunningContext.exiting(co2);
 		RunningContext.exiting(co);
 		
 		context2 = RunningContext.get();
@@ -44,4 +57,41 @@
 		assertEquals("in base;still in base;in sub context;", contextString);
 	}
 	
+	@Test
+	public void cornerCaseBefore() throws Exception {
+		SubRunningContext context = RunningContext.get();
+		
+		FakeContextOwner co = new FakeContextOwner();
+		RunningContext.entering(co);
+		
+		SubRunningContext context2 = RunningContext.get();
+		assertNotSame(context, context2);
+		context2.push("in sub context");
+		
+		assertEquals(1, context2.getElements(StringContextElement.class).size());
+		assertEquals(0, context2.getCommonElements(StringContextElement.class).size());
+		
+		RunningContext.exiting(co);
+		
+	}
+
+	@Test
+	public void cornerCaseAfter() throws Exception {
+		SubRunningContext context = RunningContext.get();
+		context.push("in context");
+		
+		FakeContextOwner co = new FakeContextOwner();
+		RunningContext.entering(co);
+		
+		SubRunningContext context2 = RunningContext.get();
+		assertNotSame(context, context2);
+		
+		assertEquals(1, context2.getElements(StringContextElement.class).size());
+		assertEquals(1, context2.getCommonElements(StringContextElement.class).size());
+		
+		RunningContext.exiting(co);
+		
+	}
+	
+	
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@labs.apache.org
For additional commands, e-mail: commits-help@labs.apache.org


Mime
View raw message