Ensures that all elements of the given set can be cast to a desired type. - Java java.util

Java examples for java.util:Set Operation

Description

Ensures that all elements of the given set can be cast to a desired type.

Demo Code


//package com.java2s;

import java.util.Set;

public class Main {
    /** Indicates whether collection elements should be actually checked. */
    private static final boolean DEBUG = true;

    /**/*from  w  w w.j a  v  a 2 s  . c  o  m*/
     * Ensures that all elements of the given set can be cast to a desired type.
     * 
     * @param list
     *            the set to check
     * @param type
     *            the desired type
     * @return a set of the desired type
     * @throws ClassCastException
     *             if an element cannot be cast to the desired type
     */
    @SuppressWarnings("unchecked")
    public static <E> Set<E> checkSet(Set<?> set, Class<E> type) {
        if (DEBUG) {
            for (Object element : set) {
                type.cast(element);
            }
        }
        return (Set<E>) set;
    }
}

Related Tutorials