Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright (C) 2014-2017 Philip Helger (www.helger.com)
 * philip[at]helger[dot]com
 *
 * 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.
 */

import java.util.Collection;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import java.util.function.Predicate;

import javax.annotation.Nullable;

public class Main {
    @Nullable
    public static <ELEMENTTYPE> ELEMENTTYPE findFirst(@Nullable final Iterable<? extends ELEMENTTYPE> aCollection,
            @Nullable final Predicate<? super ELEMENTTYPE> aFilter) {
        return findFirst(aCollection, aFilter, (ELEMENTTYPE) null);
    }

    @Nullable
    public static <ELEMENTTYPE> ELEMENTTYPE findFirst(@Nullable final Iterable<? extends ELEMENTTYPE> aCollection,
            @Nullable final Predicate<? super ELEMENTTYPE> aFilter, @Nullable final ELEMENTTYPE aDefault) {
        if (aFilter == null)
            return getFirstElement(aCollection);

        if (isNotEmpty(aCollection))
            for (final ELEMENTTYPE aElement : aCollection)
                if (aFilter.test(aElement))
                    return aElement;

        return aDefault;
    }

    /**
     * Get the first element of the passed list.
     *
     * @param <ELEMENTTYPE>
     *        List element type
     * @param aList
     *        The list. May be <code>null</code>.
     * @return <code>null</code> if the list is <code>null</code> or empty, the
     *         first element otherwise.
     */
    @Nullable
    public static <ELEMENTTYPE> ELEMENTTYPE getFirstElement(@Nullable final List<ELEMENTTYPE> aList) {
        return isEmpty(aList) ? null : aList.get(0);
    }

    /**
     * Get the first element of the passed collection.
     *
     * @param <ELEMENTTYPE>
     *        Collection element type
     * @param aCollection
     *        The collection. May be <code>null</code>.
     * @return <code>null</code> if the collection is <code>null</code> or empty,
     *         the first element otherwise.
     */
    @Nullable
    public static <ELEMENTTYPE> ELEMENTTYPE getFirstElement(@Nullable final Collection<ELEMENTTYPE> aCollection) {
        return isEmpty(aCollection) ? null : aCollection.iterator().next();
    }

    /**
     * Get the first element of the passed iterable.
     *
     * @param <ELEMENTTYPE>
     *        Iterable element type
     * @param aIterable
     *        The iterable. May be <code>null</code>.
     * @return <code>null</code> if the iterable is <code>null</code> or empty,
     *         the first element otherwise.
     */
    @Nullable
    public static <ELEMENTTYPE> ELEMENTTYPE getFirstElement(@Nullable final Iterable<ELEMENTTYPE> aIterable) {
        if (aIterable == null)
            return null;
        final Iterator<ELEMENTTYPE> it = aIterable.iterator();
        return it.hasNext() ? it.next() : null;
    }

    public static boolean isNotEmpty(@Nullable final Iterable<?> aCont) {
        return aCont != null && aCont.iterator().hasNext();
    }

    public static boolean isNotEmpty(@Nullable final Collection<?> aCont) {
        return aCont != null && !aCont.isEmpty();
    }

    public static boolean isNotEmpty(@Nullable final Map<?, ?> aCont) {
        return aCont != null && !aCont.isEmpty();
    }

    public static boolean isEmpty(@Nullable final Iterable<?> aCont) {
        return aCont == null || !aCont.iterator().hasNext();
    }

    public static boolean isEmpty(@Nullable final Collection<?> aCont) {
        return aCont == null || aCont.isEmpty();
    }

    public static boolean isEmpty(@Nullable final Map<?, ?> aCont) {
        return aCont == null || aCont.isEmpty();
    }
}