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

Home
Java Tutorial
1.Language
2.Data Type
3.Operators
4.Statement Control
5.Class Definition
6.Development
7.Reflection
8.Regular Expressions
9.Collections
10.Thread
11.File
12.Generics
13.I18N
14.Swing
15.Swing Event
16.2D Graphics
17.SWT
18.SWT 2D Graphics
19.Network
20.Database
21.Hibernate
22.JPA
23.JSP
24.JSTL
25.Servlet
26.Web Services SOA
27.EJB3
28.Spring
29.PDF
30.Email
31.J2ME
32.J2EE Application
33.XML
34.Design Pattern
35.Log
36.Security
37.Apache Common
38.Ant
39.JUnit
Java Tutorial » Generics » Generic Class 
12.6.5.Use generic method to test generic Stack
// 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 > ? 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.12.23.34.45.56.6 };

  private static Integer[] integerElements = 1234567891011 };

  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
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.