Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.awt.Component;
import java.awt.Container;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public class Main {
    public static <T> List<T> getAllOfExclude(Container container, Class<T> clazz,
            Collection<? extends Component> exclude) {
        if (container == null) {
            throw new NullPointerException("container == null");
        }
        if (clazz == null) {
            throw new NullPointerException("clazz == null");
        }
        if (exclude == null) {
            throw new NullPointerException("exclude == null");
        }
        List<T> components = new ArrayList<>();
        addAllOfExclude(container, clazz, components, exclude);
        return components;
    }

    @SuppressWarnings("unchecked")
    private static <T> void addAllOfExclude(Container container, Class<T> clazz, List<T> all,
            Collection<? extends Component> exclude) {
        if (exclude.contains(container)) {
            return;
        }
        if (container.getClass().equals(clazz)) {
            all.add((T) container);
        }
        int count = container.getComponentCount();
        for (int i = 0; i < count; i++) {
            Component component = container.getComponent(i);
            if (exclude.contains(component)) {
                continue;
            }
            if (component instanceof Container) {
                addAllOfExclude((Container) component, clazz, all, exclude); // Recursive
            } else if (component.getClass().equals(clazz)) {
                all.add((T) component);
            }
        }
    }
}