io.sidecar.util.CollectionUtils.java Source code

Java tutorial

Introduction

Here is the source code for io.sidecar.util.CollectionUtils.java

Source

/*
 * Copyright 2015 QSense, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.sidecar.util;

import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;

import java.util.List;
import java.util.Set;

public class CollectionUtils {

    public static <T> ImmutableList<T> filterNulls(List<T> orig) {
        if (orig == null) {
            return ImmutableList.of();
        }
        return ImmutableList.copyOf(Iterables.filter(orig, new Predicate<T>() {
            @Override
            public boolean apply(T t) {
                return t != null;
            }
        }));
    }

    public static <T> ImmutableSet<T> filterNulls(Set<T> orig) {
        if (orig == null) {
            return ImmutableSet.of();
        }
        return ImmutableSet.copyOf(Iterables.filter(orig, new Predicate<T>() {
            @Override
            public boolean apply(T t) {
                return t != null;
            }
        }));
    }

}