diff options
author | Brian Vosburgh | 2015-07-20 17:13:19 +0000 |
---|---|---|
committer | Brian Vosburgh | 2015-07-20 17:13:19 +0000 |
commit | d8c09ef8f40b9acf7054fb40eb8ddbd9aa0b3363 (patch) | |
tree | e6139a36efade2bde9e470f3ce143a6c3f0c05ab /common | |
parent | 02d54c0adaf6772fbed162bfd6664de4a33b0d1b (diff) | |
download | webtools.dali-d8c09ef8f40b9acf7054fb40eb8ddbd9aa0b3363.tar.gz webtools.dali-d8c09ef8f40b9acf7054fb40eb8ddbd9aa0b3363.tar.xz webtools.dali-d8c09ef8f40b9acf7054fb40eb8ddbd9aa0b3363.zip |
add ListStack
Diffstat (limited to 'common')
3 files changed, 111 insertions, 0 deletions
diff --git a/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/ListStack.java b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/ListStack.java new file mode 100644 index 0000000000..bb4d1d5ae8 --- /dev/null +++ b/common/plugins/org.eclipse.jpt.common.utility/src/org/eclipse/jpt/common/utility/internal/collection/ListStack.java @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2015 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.common.utility.internal.collection; + +import java.io.Serializable; +import java.util.EmptyStackException; +import java.util.List; +import org.eclipse.jpt.common.utility.collection.Stack; + +/** + * Adapt a {@link List} to the {@link Stack} interface. + * Elements are popped from the end of the list (i.e. index size - 1). + * @param <E> the type of elements maintained by the stack + */ +public class ListStack<E> + implements Stack<E>, Serializable +{ + private List<E> list; + + private static final long serialVersionUID = 1L; + + + // ********** constructors ********** + + /** + * Construct a stack, adapting the specified list. + * The stack will pop its elements in the reverse + * order they are returned by the list's iterator (i.e. the + * last element returned by the list's iterator will be the + * first element returned by {@link #pop()}). + */ + public ListStack(List<E> list) { + super(); + this.list = list; + } + + + // ********** Stack implementation ********** + + public void push(E element) { + this.list.add(element); + } + + public E pop() { + try { + return this.list.remove(this.list.size() - 1); + } catch (IndexOutOfBoundsException ex) { + throw new EmptyStackException(); + } + } + + public E peek() { + try { + return this.list.get(this.list.size() - 1); + } catch (IndexOutOfBoundsException ex) { + throw new EmptyStackException(); + } + } + + public boolean isEmpty() { + return this.list.isEmpty(); + } + + + // ********** standard methods ********** + + @Override + public String toString() { + return this.list.toString(); + } +} diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/JptCommonUtilityCollectionTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/JptCommonUtilityCollectionTests.java index 9c674f4a96..67a7c49947 100644 --- a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/JptCommonUtilityCollectionTests.java +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/JptCommonUtilityCollectionTests.java @@ -32,6 +32,7 @@ public class JptCommonUtilityCollectionTests { suite.addTestSuite(LinkedQueueTests.class); suite.addTestSuite(LinkedStackTests.class); suite.addTestSuite(ListQueueTests.class); + suite.addTestSuite(ListStackTests.class); suite.addTestSuite(ListToolsTests.class); suite.addTestSuite(MapToolsTests.class); suite.addTestSuite(NullElementListTests.class); diff --git a/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/ListStackTests.java b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/ListStackTests.java new file mode 100644 index 0000000000..4012ee2763 --- /dev/null +++ b/common/tests/org.eclipse.jpt.common.utility.tests/src/org/eclipse/jpt/common/utility/tests/internal/collection/ListStackTests.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * Copyright (c) 2015 Oracle. All rights reserved. + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0, which accompanies this distribution + * and is available at http://www.eclipse.org/legal/epl-v10.html. + * + * Contributors: + * Oracle - initial API and implementation + ******************************************************************************/ +package org.eclipse.jpt.common.utility.tests.internal.collection; + +import java.util.ArrayList; +import org.eclipse.jpt.common.utility.collection.Stack; +import org.eclipse.jpt.common.utility.internal.collection.ListStack; + +public class ListStackTests + extends StackTests +{ + public ListStackTests(String name) { + super(name); + } + + @Override + Stack<String> buildStack() { + return new ListStack<String>(new ArrayList<String>()); + } + + @Override + public void testClone() { + // unsupported + } +} |