Java Stack Create checkStack(Object object, Class type)

Here you can find the source of checkStack(Object object, Class type)

Description

check Stack

License

Apache License

Declaration

public static <T> Stack<T> checkStack(Object object, Class<T> type) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.Collection;

import java.util.Stack;

public class Main {
    @SuppressWarnings("unchecked")
    public static <T> Stack<T> checkStack(Object object) {
        return (Stack<T>) checkCollectionCast(object, Stack.class);
    }/*from   ww w . ja va2s.com*/

    public static <T> Stack<T> checkStack(Object object, Class<T> type) {
        checkCollectionContainment(object, Stack.class, type);
        return checkStack(object);
    }

    private static <C extends Collection<?>> C checkCollectionCast(Object object, Class<C> clz) {
        return clz.cast(object);
    }

    public static <C extends Collection<?>> void checkCollectionContainment(Object object, Class<C> clz,
            Class<?> type) {
        if (object != null) {
            if (!(clz.isInstance(object)))
                throw new ClassCastException("Not a " + clz.getName());
            int i = 0;
            for (Object value : (Collection<?>) object) {
                if (value != null && !type.isInstance(value)) {
                    throw new IllegalArgumentException(
                            "Value(" + i + "), with value(" + value + ") is not a " + type.getName());
                }
                i++;
            }
        }
    }

    @SuppressWarnings("unchecked")
    public static <V> V cast(Object object) {
        return (V) object;
    }
}

Related

  1. getCharStack()
  2. listToStack(List list)
  3. toStack(int[] input)