Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

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

import java.util.Collections;
import java.util.HashMap;

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

public class Main {
    /**
     * Wraps a possibly null map in an immutable wrapper.
     *
     * @param <K> the key type
     * @param <V> the value type
     * @param source Nullable map to wrap.
     * @return {@link Collections#unmodifiableMap(java.util.Map)} if given map is not null, otherwise
     * {@link java.util.Collections#emptyMap()}.
     */
    public static <K, V> Map<K, V> wrap(final Map<K, V> source) {
        if (source != null) {
            return new HashMap<>(source);
        }
        return Collections.emptyMap();
    }

    /**
     * Wraps a possibly null list in an immutable wrapper.
     *
     * @param <T>    the type parameter
     * @param source Nullable list to wrap.
     * @return {@link Collections#unmodifiableList(List)} if given list is not null, otherwise {@link java.util.Collections#EMPTY_LIST}.
     */
    public static <T> List<T> wrap(final List<T> source) {
        if (source != null) {
            return Collections.unmodifiableList(source);
        }
        return Collections.emptyList();
    }
}