package me.mattlogan.library;

import android.os.Bundle;
import android.view.ViewGroup;

import java.util.Stack;

public final class ViewStack {

    private static final String STACK_TAG = "stack";

    private final Stack<ViewFactory> stack = new Stack<>();
    private final ViewGroup container;
    private final ViewStackDelegate delegate;

    public static ViewStack create(ViewGroup container) {
        return new ViewStack(container, ViewStackDelegate.DEFAULT);
    }

    public static ViewStack create(ViewGroup container, ViewStackDelegate delegate) {
        return new ViewStack(container, delegate);
    }

    private ViewStack(ViewGroup container, ViewStackDelegate delegate) {
        this.container = container;
        this.delegate = delegate;
    }

    public void push(ViewFactory viewFactory) {
        if (!delegate.shouldUpdateViewStack(size(), size() + 1)) return;
        stack.push(viewFactory);
        updateContainer();
        delegate.onViewStackUpdated(size());
    }

    @SuppressWarnings("all")
    public void rebuildFromBundle(Bundle bundle) {
        Stack<ViewFactory> savedStack = (Stack<ViewFactory>) bundle.getSerializable(STACK_TAG);
        for (ViewFactory viewFactory : savedStack) {
            if (delegate.shouldUpdateViewStack(size(), size() + 1)) {
                stack.push(viewFactory);
            }
        }
        updateContainer();
        delegate.onViewStackUpdated(size());
    }

    public void pop() {
        if (!delegate.shouldUpdateViewStack(size(), size() - 1)) return;
        stack.pop();
        updateContainer();
        delegate.onViewStackUpdated(size());
    }

    public int size() {
        return stack.size();
    }

    private void updateContainer() {
        container.removeAllViews();
        if (stack.size() > 0) {
            container.addView(stack.peek().createView(container.getContext()));
        }
    }

    public void saveToBundle(Bundle bundle) {
        bundle.putSerializable(STACK_TAG, stack);
    }
}

@@ -777,20 +777,27 @@
  public 
-void
+ViewFactory
  push(Vi
@@ -884,32 +884,37 @@
 e() + 1)) return
+ null
 ;%0A        stack.
@@ -996,32 +996,60 @@
 pdated(size());%0A
+        return viewFactory;%0A
     %7D%0A%0A    @Supp
@@ -1493,20 +1493,27 @@
  public 
-void
+ViewFactory
  pop() %7B
@@ -1584,16 +1584,21 @@
 ) return
+ null
 ;%0A      
@@ -1590,32 +1590,58 @@
 rn null;%0A       
+ ViewFactory viewFactory =
  stack.pop();%0A  
@@ -1702,32 +1702,128 @@
 pdated(size());%0A
+        return viewFactory;%0A    %7D%0A%0A    public ViewFactory peek() %7B%0A        return stack.peek();%0A
     %7D%0A%0A    publi

Update ViewStack methods to conform to actual Stack