Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import com.google.common.base.Objects;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;

public class Main {

    public static <K, V> Collection<Map<K, V>> filterListMap(Collection<Map<K, V>> unfiltered, final K key,
            final V value) {
        return filter(unfiltered, new Predicate<Map<K, V>>() {
            @Override
            public boolean apply(Map<K, V> input) {
                return Objects.equal(input.get(key), value);
            }
        });
    }

    public static <K, V> Collection<Map<K, V>> filterListMap(Collection<Map<K, V>> unfiltered,
            final Map<K, V> map) {
        return filter(unfiltered, new Predicate<Map<K, V>>() {
            @Override
            public boolean apply(Map<K, V> input) {
                if (null == map || map.isEmpty())
                    return true;
                Iterator<Map.Entry<K, V>> iter = map.entrySet().iterator();
                boolean flag = true;
                while (iter.hasNext()) {
                    Map.Entry<K, V> entry = iter.next();
                    V iV = input.get(entry.getKey());
                    V cV = entry.getValue();
                    flag &= Objects.equal(iV, cV);
                    if (!flag)
                        break;
                }
                return flag;
            }
        });
    }

    public static <T> Collection<T> filter(Collection<T> unfiltered, Predicate<? super T> predicate) {
        return ImmutableList.copyOf(Iterables.filter(unfiltered, predicate));
    }

    public static <T> Collection<T> filter(Collection<T> unfiltered, final T value) {
        return filter(unfiltered, new Predicate<T>() {
            @Override
            public boolean apply(T input) {
                if (value.getClass().equals(String.class)) {
                    String in = null == input ? "" : (String) input;
                    return in.indexOf((String) value) != -1;
                }
                return Objects.equal(input, value);
            }
        });
    }
}