Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you 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.lang.reflect.Array;

import java.util.Collections;

import java.util.EnumSet;

import java.util.Set;

public class Main {
    private static final Class<? extends Set> _EMPTY_SET = Collections.emptySet().getClass();
    private static final Class<? extends Set> _SINGLETON_SET = Collections.singleton(null).getClass();

    /**
     * Returns an unmodifiable versions of the Set of Enums.  If the contents of the set are known
     * to be unmodifiable by the caller in any way, the set itself will be retured, otherwise an
     * unmodifiable copy of the Set will be returned.
     * @param s Set to get the tamper-proof version of
     * @return An unmodifiable tamper-proof version of the set
     */
    public static <E extends Enum<E>> Set<E> unmodifiableCopyOfEnumSet(Set<E> s) {
        Class<? extends Set> copyClass = s.getClass();

        if ((_EMPTY_SET == copyClass) || (_SINGLETON_SET == copyClass)) {
            // these classes are already unmodifiable, so just return
            return s;
        } else {
            return Collections.unmodifiableSet(EnumSet.copyOf(s));
        }
    }

    protected static <T> T[] copyOf(T[] original, int newLength) {
        return (T[]) copyOf(original, newLength, original.getClass());
    }

    protected static <T, U> T[] copyOf(U[] original, int newLength, Class<? extends T[]> newType) {
        T[] copy = ((Object) newType == (Object) Object[].class) ? (T[]) new Object[newLength]
                : (T[]) Array.newInstance(newType.getComponentType(), newLength);
        System.arraycopy(original, 0, copy, 0, Math.min(original.length, newLength));
        return copy;
    }
}