Last active
December 18, 2018 10:26
-
-
Save ipekt/91db286abc5cc0f958cb07095aa8c554 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class AnotherStack<E> { | |
| private Either<? super E, String>[] elements; | |
| private int size = 0; | |
| private static final int DEFAULT_INITIAL_CAPACITY = 16; | |
| public AnotherStack() { | |
| elements = (Either<? super E, String>[]) new Object[DEFAULT_INITIAL_CAPACITY]; | |
| } | |
| public boolean isEmpty() { | |
| return size == 0; | |
| } | |
| public Either<? super E, String> pop() { | |
| if (size == 0) throw new EmptyStackException(); | |
| Either<? super E, String> result = elements[--size]; | |
| elements[size] = null; // Eliminate obsolete reference | |
| return result; | |
| } | |
| public void popAll(Collection<Either<? super E, String>> dst) { | |
| while (!isEmpty()) { | |
| dst.add(pop()); | |
| } | |
| } | |
| } | |
| // Not working: `Collection<Either<? super ValidationError,String>>` can not be applied to Collection<Either<ServerError, String>> | |
| public void test() { | |
| AnotherStack<ValidationError> stack = new AnotherStack<>(); | |
| Collection<Either<ServiceError, String>> errors = | |
| Arrays.asList(Either.left(new ValidationError("A")), Either.left(new ValidationError("B"))); | |
| stack.popAll(errors); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment