com.anathema_roguelike.main.utilities.datastructures.CollectionUtils.java Source code

Java tutorial

Introduction

Here is the source code for com.anathema_roguelike.main.utilities.datastructures.CollectionUtils.java

Source

/*******************************************************************************
 * This file is part of AnathemaRL.
 *
 *     AnathemaRL is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     AnathemaRL is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with AnathemaRL.  If not, see <http://www.gnu.org/licenses/>.
 *******************************************************************************/
package com.anathema_roguelike.main.utilities.datastructures;

import java.util.Collection;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;

public abstract class CollectionUtils {

    @SuppressWarnings(value = { "unchecked" })
    public static <U, F extends U> Collection<F> filterByClass(Collection<U> unfiltered, final Class<F> cls) {
        return (Collection<F>) Collections2.filter(unfiltered, new Predicate<U>() {

            @Override
            public boolean apply(U superclass) {
                return cls.isAssignableFrom(superclass.getClass());
            }
        });
    }
}