Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/**
 * Copyright 2005-2015 The Kuali Foundation
 *
 * Licensed under the Educational Community 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.opensource.org/licenses/ecl2.php
 *
 * 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.lang.reflect.Field;

import java.util.Collection;
import java.util.Collections;

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

public class Main {
    /**
     * This method will iterate over all the fields on an object searching for declared fields defined as
     * Collection, List, Set, Map type.  If those fields are null they will be set to empty unmodifiable
     * instances.  If those fields are not null then it will force them to be unmodifiable.
     *
     * This method does not handle nested types.
     *
     * @param o the object to modify.  a null object will do nothing.
     */
    public static void makeUnmodifiableAndNullSafe(Object o) throws IllegalAccessException {
        if (o == null) {
            return;
        }

        Class<?> targetClass = o.getClass();
        for (Field f : targetClass.getDeclaredFields()) {
            f.setAccessible(true);
            try {
                if (f.getType().isAssignableFrom(List.class)) {
                    f.set(o, unmodifiableListNullSafe((List<?>) f.get(o)));
                } else if (f.getType().isAssignableFrom(Set.class)) {
                    f.set(o, unmodifiableSetNullSafe((Set<?>) f.get(o)));
                } else if (f.getType().isAssignableFrom(Collection.class)) {
                    f.set(o, unmodifiableCollectionNullSafe((Collection<?>) f.get(o)));
                } else if (f.getType().isAssignableFrom(Map.class)) {
                    f.set(o, unmodifiableMapNullSafe((Map<?, ?>) f.get(o)));
                }
            } finally {
                f.setAccessible(false);
            }
        }
    }

    /**
     * Functions as per {@link Collections#unmodifiableList(List)} with the exception that if the
     * given list is null, this method will return an unmodifiable empty list as per
     * {@link Collections#emptyList()}.
     * 
     * @param list the list for which an unmodifiable view is to be returned
     * @return an unmodifiable view of the specified list, or an unmodifiable empty list if the
     *         given list is null
     */
    public static <T> List<T> unmodifiableListNullSafe(List<? extends T> list) {
        if (list == null) {
            return Collections.emptyList();
        }
        return Collections.unmodifiableList(list);
    }

    /**
     * Functions as per {@link Collections#unmodifiableSet(Set)} with the exception that if the
     * given set is null, this method will return an unmodifiable empty set as per
     * {@link Collections#emptySet()}.
     * 
     * @param set the set for which an unmodifiable view is to be returned
     * @return an unmodifiable view of the specified set, or an unmodifiable empty set if the given
     *         set is null
     */
    public static <T> Set<T> unmodifiableSetNullSafe(Set<? extends T> set) {
        if (set == null) {
            return Collections.emptySet();
        }
        return Collections.unmodifiableSet(set);
    }

    /**
     * Functions as per {@link Collections#unmodifiableList(Collection)} with the exception that if the
     * given collection is null, this method will return an unmodifiable empty collection as per
     * {@link Collections#emptyList()}.
     *
     * @param collection the collection for which an unmodifiable view is to be returned
     * @return an unmodifiable view of the specified collection, or an unmodifiable empty collection if the
     *         given collection is null
     */
    public static <T> Collection<T> unmodifiableCollectionNullSafe(Collection<? extends T> collection) {
        if (collection == null) {
            return Collections.emptyList();
        }
        return Collections.unmodifiableCollection(collection);
    }

    /**
     * Functions as per {@link Collections#unmodifiableMap(Map)} with the exception that if the
     * given map is null, this method will return an unmodifiable empty map as per
     * {@link Collections#emptyMap()}.
     * 
     * @param map the map for which an unmodifiable view is to be returned
     * @return an unmodifiable view of the specified set, or an unmodifiable empty set if the given
     *         set is null
     */
    public static <K, V> Map<K, V> unmodifiableMapNullSafe(Map<? extends K, ? extends V> map) {
        if (map == null) {
            return Collections.emptyMap();
        }
        return Collections.unmodifiableMap(map);
    }
}