com.netflix.servo.util.UnmodifiableSet 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.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* Utility class to create unmodifiable sets.
*/
public final class UnmodifiableSet {
private UnmodifiableSet() {
}
/**
* Returns an unmodifiable view of the set created from the given elements.
*
* @param elements Array of elements
* @param type of the elements
* @return an unmodifiable view of the set created from the given elements.
*/
@SafeVarargs
public static Set of(E... elements) {
Set result = new HashSet<>();
Collections.addAll(result, elements);
return Collections.unmodifiableSet(result);
}
/**
* Returns an unmodifiable view of the set created from the given elements.
*
* @param elementsIterator iterator to get the elements of the set.
* @param type of the elements
* @return an unmodifiable view of the set created from the given elements.
*/
public static Set copyOf(Iterator extends E> elementsIterator) {
Set result = new HashSet<>();
while (elementsIterator.hasNext()) {
result.add(elementsIterator.next());
}
return Collections.unmodifiableSet(result);
}
}