Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: LGPL 

import java.util.Collection;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import java.util.Objects;

import java.util.function.Function;

public class Main {
    /**
     * Transform the source collection using the mapping function to convert the elements to the output type. The result
     * is written to a list.
     *
     * @param <T>
     *            the source type type
     * @param <R>
     *            the result type
     * @param source
     *            the source
     * @param mapping
     *            the mapping used for the value transformation
     * @return the list
     */
    public static <T, R> List<R> transformToList(Collection<T> source, Function<T, R> mapping) {
        Objects.requireNonNull(mapping, "Mapping functions is required!");
        if (isEmpty(source)) {
            return new LinkedList<>();
        }
        List<R> result = new LinkedList<>();
        for (T item : source) {
            result.add(mapping.apply(item));
        }
        return result;
    }

    /**
     * Check whether a collection is null or empty.
     *
     * @param collection
     *            Collection to check.
     * @return {@code true} if the collection is {@code null} or {@link Collection#isEmpty()} returns {@code true}.
     */
    public static boolean isEmpty(Collection<?> collection) {
        return collection == null || collection.isEmpty();
    }

    /**
     * Checks if is null or empty.
     *
     * @param map
     *            the map
     * @return true, if is empty
     */
    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.isEmpty();
    }

    /**
     * Checks if given array is empty or null.
     *
     * @param objects
     *            array to check
     * @return true if null or its length is 0, false otherwise
     */
    public static boolean isEmpty(Object[] objects) {
        return objects == null || objects.length == 0;
    }
}