diff --git a/SimpleActivityStack/.classpath b/SimpleActivityStack/.classpath new file mode 100644 index 0000000..be55723 --- /dev/null +++ b/SimpleActivityStack/.classpath @@ -0,0 +1,7 @@ + + + + + + + diff --git a/SimpleActivityStack/.project b/SimpleActivityStack/.project new file mode 100644 index 0000000..19501f2 --- /dev/null +++ b/SimpleActivityStack/.project @@ -0,0 +1,17 @@ + + + SimpleActivityStack + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/SimpleActivityStack/src/solution/SimpleActivityStack.java b/SimpleActivityStack/src/solution/SimpleActivityStack.java new file mode 100644 index 0000000..a183c0c --- /dev/null +++ b/SimpleActivityStack/src/solution/SimpleActivityStack.java @@ -0,0 +1,38 @@ +package solution; + +import testat.SimpleActivity; + +public class SimpleActivityStack extends SimpleStack { + + @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); + } + +} diff --git a/SimpleActivityStack/src/solution/SimpleActivityStackTest.java b/SimpleActivityStack/src/solution/SimpleActivityStackTest.java new file mode 100644 index 0000000..73d60b8 --- /dev/null +++ b/SimpleActivityStack/src/solution/SimpleActivityStackTest.java @@ -0,0 +1,14 @@ +package solution; + +import testat.ASimpleActivityStackTest; +import testat.ISimpleStack; +import testat.SimpleActivity; + +public class SimpleActivityStackTest extends ASimpleActivityStackTest { + + @Override + protected ISimpleStack getInstance() { + return new SimpleActivityStack(); + } + +} diff --git a/SimpleActivityStack/src/solution/SimpleStack.java b/SimpleActivityStack/src/solution/SimpleStack.java new file mode 100644 index 0000000..3ed4186 --- /dev/null +++ b/SimpleActivityStack/src/solution/SimpleStack.java @@ -0,0 +1,64 @@ +package solution; + +import java.util.LinkedList; +import java.util.List; + +import testat.IAction; +import testat.ISimpleStack; + +public class SimpleStack implements ISimpleStack { + + private List list; + + public SimpleStack() { + super(); + list = new LinkedList(); + } + + @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 action) { + if (action != null) { + list.forEach(t -> action.execute(t)); + } + } +} diff --git a/SimpleActivityStack/src/solution/SimpleStackTest.java b/SimpleActivityStack/src/solution/SimpleStackTest.java new file mode 100644 index 0000000..84e2a48 --- /dev/null +++ b/SimpleActivityStack/src/solution/SimpleStackTest.java @@ -0,0 +1,13 @@ +package solution; + +import testat.ASimpleStackTest; +import testat.ISimpleStack; + +public class SimpleStackTest extends ASimpleStackTest { + + @Override + protected ISimpleStack getInstance() { + return new SimpleStack<>(); + } + +}