All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.netflix.servo.util.UnmodifiableList Maven / Gradle / Ivy

/*
 * Copyright 2014 Netflix, Inc.
 *
 * 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.
 */

package com.netflix.servo.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/** Utility class to create umodifiable views for lists. */
public final class UnmodifiableList {
    private UnmodifiableList() {
    }

    /**
     * Returns an unmodifiable view of the list composed of elements.
     * @param elements Array of elements.
     * @param  Type of the elements of the list.
     * @return an unmodifiable view of the list composed of elements.
     */
    public static  List of(E... elements) {
        Preconditions.checkNotNull(elements, "elements");
        return Collections.unmodifiableList(Arrays.asList(elements));
    }

    /**
     * Returns an unmodifiable view of the list composed of elements.
     *
     * @param elements Array of elements.
     * @param       Type of the elements of the list.
     * @return an unmodifiable view of the list composed of elements.
     */
    public static  List copyOf(E[] elements) {
        Preconditions.checkNotNull(elements, "elements");
        List result = new ArrayList(elements.length);
        Collections.addAll(result, elements);
        return Collections.unmodifiableList(result);
    }

    // hack to simplify casting
    static  Collection cast(Iterable iterable) {
        return (Collection) iterable;
    }

    /**
     * Returns an unmodifiable view of the list composed of elements.
     *
     * @param elements Iterable of elements.
     * @param       Type of the elements of the list.
     * @return an unmodifiable view of the list composed of elements.
     */
    public static  List copyOf(Iterable elements) {
        Preconditions.checkNotNull(elements, "elements");
        List result = (elements instanceof Collection)
                // can pre-allocate the array
                ? new ArrayList(cast(elements).size())
                // cannot
                : new ArrayList();
        for (E e : elements) {
            result.add(e);
        }
        return Collections.unmodifiableList(result);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy