6 changed files with 153 additions and 0 deletions
			
			
		| @ -0,0 +1,7 @@ | |||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||
|  | <classpath> | ||||
|  | 	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/> | ||||
|  | 	<classpathentry kind="src" path="src"/> | ||||
|  | 	<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/5"/> | ||||
|  | 	<classpathentry kind="output" path="bin"/> | ||||
|  | </classpath> | ||||
| @ -0,0 +1,17 @@ | |||||
|  | <?xml version="1.0" encoding="UTF-8"?> | ||||
|  | <projectDescription> | ||||
|  | 	<name>SimpleActivityStack</name> | ||||
|  | 	<comment></comment> | ||||
|  | 	<projects> | ||||
|  | 	</projects> | ||||
|  | 	<buildSpec> | ||||
|  | 		<buildCommand> | ||||
|  | 			<name>org.eclipse.jdt.core.javabuilder</name> | ||||
|  | 			<arguments> | ||||
|  | 			</arguments> | ||||
|  | 		</buildCommand> | ||||
|  | 	</buildSpec> | ||||
|  | 	<natures> | ||||
|  | 		<nature>org.eclipse.jdt.core.javanature</nature> | ||||
|  | 	</natures> | ||||
|  | </projectDescription> | ||||
| @ -0,0 +1,38 @@ | |||||
|  | package solution; | ||||
|  | 
 | ||||
|  | import testat.SimpleActivity; | ||||
|  | 
 | ||||
|  | public class SimpleActivityStack extends SimpleStack<SimpleActivity> { | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	public int push(SimpleActivity activity) { | ||||
|  | 		if (size() > 0) { | ||||
|  | 			super.get(size() - 1).passivate(); | ||||
|  | 		} | ||||
|  | 		activity.activate(); | ||||
|  | 		return super.push(activity); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	public SimpleActivity pop() { | ||||
|  | 		SimpleActivity activity = super.pop(); | ||||
|  | 		if (activity != null) { | ||||
|  | 			activity.passivate(); | ||||
|  | 			activity.destroy(); | ||||
|  | 			if (size() > 0) { | ||||
|  | 				super.get(size() - 1).activate(); | ||||
|  | 			} | ||||
|  | 		} | ||||
|  | 		return activity; | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	public SimpleActivity remove(int i) { | ||||
|  | 		if (i == super.size() - 1) { | ||||
|  | 			return this.pop(); | ||||
|  | 		} | ||||
|  | 		super.get(i).destroy(); | ||||
|  | 		return super.remove(i); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | } | ||||
| @ -0,0 +1,14 @@ | |||||
|  | package solution; | ||||
|  | 
 | ||||
|  | import testat.ASimpleActivityStackTest; | ||||
|  | import testat.ISimpleStack; | ||||
|  | import testat.SimpleActivity; | ||||
|  | 
 | ||||
|  | public class SimpleActivityStackTest extends ASimpleActivityStackTest { | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	protected ISimpleStack<SimpleActivity> getInstance() { | ||||
|  | 		return new SimpleActivityStack(); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | } | ||||
| @ -0,0 +1,64 @@ | |||||
|  | package solution; | ||||
|  | 
 | ||||
|  | import java.util.LinkedList; | ||||
|  | import java.util.List; | ||||
|  | 
 | ||||
|  | import testat.IAction; | ||||
|  | import testat.ISimpleStack; | ||||
|  | 
 | ||||
|  | public class SimpleStack<T> implements ISimpleStack<T> { | ||||
|  | 
 | ||||
|  | 	private List<T> list; | ||||
|  | 
 | ||||
|  | 	public SimpleStack() { | ||||
|  | 		super(); | ||||
|  | 		list = new LinkedList<T>(); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	public int push(T t) { | ||||
|  | 		if (t != null) { | ||||
|  | 			if (list.contains(t)) { | ||||
|  | 				list.remove(t); | ||||
|  | 			} | ||||
|  | 			list.add(t); | ||||
|  | 			return list.size(); | ||||
|  | 		} | ||||
|  | 		return 0; | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	public T pop() { | ||||
|  | 		if (list.size() == 0) { | ||||
|  | 			return null; | ||||
|  | 		} | ||||
|  | 		return list.remove(list.size() - 1); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	public T remove(int i) { | ||||
|  | 		return list.remove(i); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	public int size() { | ||||
|  | 		return list.size(); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	public boolean contains(Object o) { | ||||
|  | 		return list.contains(o); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	public T get(int i) { | ||||
|  | 		return list.get(i); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	public void forAll(IAction<T> action) { | ||||
|  | 		if (action != null) { | ||||
|  | 			list.forEach(t -> action.execute(t)); | ||||
|  | 		} | ||||
|  | 	} | ||||
|  | } | ||||
| @ -0,0 +1,13 @@ | |||||
|  | package solution; | ||||
|  | 
 | ||||
|  | import testat.ASimpleStackTest; | ||||
|  | import testat.ISimpleStack; | ||||
|  | 
 | ||||
|  | public class SimpleStackTest extends ASimpleStackTest { | ||||
|  | 
 | ||||
|  | 	@Override | ||||
|  | 	protected ISimpleStack<String> getInstance() { | ||||
|  | 		return new SimpleStack<>(); | ||||
|  | 	} | ||||
|  | 
 | ||||
|  | } | ||||
					Loading…
					
					
				
		Reference in new issue