Indicates if the collection has been wrapped by Collections.unmodifiableCollection(...) - Java java.util

Java examples for java.util:Collection Operation

Description

Indicates if the collection has been wrapped by Collections.unmodifiableCollection(...)

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        Object collection = "java2s.com";
        System.out.println(isUnmodifiableCollection(collection));
    }/*  w ww .  j a  v  a 2s  .co m*/

    /**
     * Indicates if the collection has been wrapped by Collections.unmodifiableCollection(...)
     * 
     * @param collection
     * @return
     */
    public static boolean isUnmodifiableCollection(Object collection) {
        // Precondition checking
        //
        if (collection == null) {
            return false;
        }

        // Get class
        //
        Class<?> collectionClass = collection.getClass();
        return (collectionClass.getName()
                .startsWith("java.util.Collections$Unmodifiable"));
    }
}

Related Tutorials