Java tutorial
//package com.java2s; /* * This file is part of the swblocks-jbl library. * * 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.Collections; import java.util.Map; public class Main { private static final Map exampleUnmodifiableMap = Collections.unmodifiableMap(Collections.emptyMap()); /** * Wraps a Map with the {@code Collections.unmodifiableMap}, but only once. * * <p>Checks the {@link Map} passed to ensure that it is not already a {@code Collections.unmodifiableMap}. * If the parameter is a null or empty, then it returns {@code Collections.emptyMap}. * * @param map {@link Map} to wrap with {@code Collections.unmodifiableMap} * @param <K> Key type * @param <V> Value type * @return An unmodifiable Map */ public static <K, V> Map<K, V> unmodifiableMap(final Map<K, V> map) { if (isNotEmpty(map)) { if (!(exampleUnmodifiableMap.getClass().equals(map.getClass()))) { return Collections.unmodifiableMap(map); } return map; } return Collections.emptyMap(); } /** * Utility method to check for a not empty {@link Collection} including a null check. * * @param collection {@link Collection} to test for non empty * @return true if collection is not null and has at least one entry. */ public static boolean isNotEmpty(final Collection<?> collection) { return collection != null && !collection.isEmpty(); } /** * Utility method to check for a not empty {@link Map} including a null check. * * @param map {@link Map} to test for non empty * @param <K> Type of Key in Map * @param <V> Type of Value in Map * @return true if map is not null and has at least one entry. */ public static <K, V> boolean isNotEmpty(final Map<K, V> map) { return map != null && !map.isEmpty(); } }