Use generic method to test generic Stack : Generic Class « Generics « Java Tutorial






// Generic class Stack.

class Stack<E> {
  private final int size;

  private int top;

  private E[] elements;

  public Stack() {
    this(10);
  }

  public Stack(int s) {
    size = s > 0 ? s : 10;
    top = -1;

    elements = (E[]) new Object[size]; // create array
  }

  public void push(E pushValue) {
    if (top == size - 1) // if stack is full
      throw new FullStackException(String.format("Stack is full, cannot push %s", pushValue));

    elements[++top] = pushValue; // place pushValue on Stack
  }

  public E pop() {
    if (top == -1) // if stack is empty
      throw new EmptyStackException("Stack is empty, cannot pop");

    return elements[top--]; // remove and return top element of Stack
  }
}

class EmptyStackException extends RuntimeException {
  public EmptyStackException() {
    this("Stack is empty");
  }

  public EmptyStackException(String exception) {
    super(exception);
  }
}

class FullStackException extends RuntimeException {
  public FullStackException() {
    this("Stack is full");
  }

  public FullStackException(String exception) {
    super(exception);
  }
}

public class MainClass {
  private static Double[] doubleElements = { 1.1, 2.2, 3.3, 4.4, 5.5, 6.6 };

  private static Integer[] integerElements = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };

  private static Stack<Double> doubleStack = new Stack<Double>(5); // Stack of
                                                                    // Doubles

  private static Stack<Integer> integerStack = new Stack<Integer>(10); // Stack
                                                                        // of
                                                                        // Integers

  // generic method testPush pushes elements onto a Stack
  public static <T> void testPush(String name, Stack<T> stack, T[] elements) {
    try {
      System.out.printf("\nPushing elements onto %s\n", name);

      for (T element : elements) {
        System.out.printf("%s ", element);
        stack.push(element);
      }
    } catch (FullStackException fullStackException) {
      System.out.println();
      fullStackException.printStackTrace();
    }
  }

  // generic method testPop pops elements from a Stack
  public static <T> void testPop(String name, Stack<T> stack) {
    try {
      System.out.printf("\nPopping elements from %s\n", name);
      T popValue;
      while (true) {
        popValue = stack.pop();
        System.out.printf("%s ", popValue);
      }
    } catch (EmptyStackException emptyStackException) {
      System.out.println();
      emptyStackException.printStackTrace();
    }
  }

  public static void main(String args[]) {
    testPush("doubleStack", doubleStack, doubleElements);
    testPop("doubleStack", doubleStack);
    testPush("integerStack", integerStack, integerElements);
    testPop("integerStack", integerStack);

  }
}








12.6.Generic Class
12.6.1.Defining a Generic Class Type
12.6.2.Using Primitive Type Wrapper Class Types as Arguments
12.6.3.The Run-Time Types of Generic Type Instances
12.6.4.Generic class Stack
12.6.5.Use generic method to test generic Stack
12.6.6.Raw type test for a generic Stack