Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

import java.util.Collection;

import java.util.HashSet;

public class Main {
    /**
     * @param c
     * @param values
     * @return <code>true</code> if the collection contains one of the values.
     */
    public static <T> boolean containsAny(Collection<T> c, Collection<? extends T> values) {
        if (values == null || c == null) {
            return false;
        }
        HashSet<T> set = hashSet(c);
        for (T value : values) {
            if (set.contains(value)) {
                return true;
            }
        }
        return false;
    }

    /**
     * @param c
     * @param values
     * @return <code>true</code> if the collection contains one of the values.
     */
    public static <T> boolean containsAny(Collection<T> c, T... values) {
        if (values == null || c == null) {
            return false;
        }
        HashSet<T> set = hashSet(c);
        for (T value : values) {
            if (set.contains(value)) {
                return true;
            }
        }
        return false;
    }

    /**
     * Null safe creation of a {@link HashSet} out of a given collection. The returned {@link HashSet} is modifiable and
     * never null.
     *
     * @param c
     * @return an {@link HashSet} containing the given collection's elements. Never null.
     */
    public static <T> HashSet<T> hashSet(Collection<? extends T> c) {
        if (c != null) {
            return new HashSet<T>(c);
        }
        return new HashSet<T>(0);
    }

    /**
     * Set factory
     */
    public static <T> HashSet<T> hashSet(T... values) {
        if (values != null) {
            HashSet<T> set = new HashSet<T>(values.length);
            for (T v : values) {
                set.add(v);
            }
            return set;
        }
        return new HashSet<T>(0);
    }

    public static <T> HashSet<T> hashSet(T value) {
        HashSet<T> set = new HashSet<T>();
        if (value != null) {
            set.add(value);
        }
        return set;
    }
}