org.gradle.util.internal.WrapUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of gradle-api Show documentation
Show all versions of gradle-api Show documentation
Gradle 6.9.1 API redistribution.
The newest version!
/*
* Copyright 2021 the original author or authors.
*
* 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 org.gradle.util.internal;
import org.gradle.api.DomainObjectSet;
import org.gradle.api.internal.CollectionCallbackActionDecorator;
import org.gradle.api.internal.DefaultDomainObjectSet;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
/**
* Common methods to wrap objects in generic collections.
*/
public class WrapUtil {
/**
* Wraps the given items in a mutable unordered set.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static Set toSet(T... items) {
Set coll = new HashSet();
Collections.addAll(coll, items);
return coll;
}
/**
* Wraps the given items in a mutable domain object set.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static DomainObjectSet toDomainObjectSet(Class type, T... items) {
DefaultDomainObjectSet set = new DefaultDomainObjectSet(type, CollectionCallbackActionDecorator.NOOP);
set.addAll(Arrays.asList(items));
return set;
}
/**
* Wraps the given items in a mutable ordered set.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static Set toLinkedSet(T... items) {
Set coll = new LinkedHashSet();
Collections.addAll(coll, items);
return coll;
}
/**
* Wraps the given items in a mutable sorted set.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static SortedSet toSortedSet(T... items) {
SortedSet coll = new TreeSet();
Collections.addAll(coll, items);
return coll;
}
/**
* Wraps the given items in a mutable sorted set using the given comparator.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static SortedSet toSortedSet(Comparator comp, T... items) {
SortedSet coll = new TreeSet(comp);
Collections.addAll(coll, items);
return coll;
}
/**
* Wraps the given items in a mutable list.
*/
@SafeVarargs
@SuppressWarnings("varargs")
public static List toList(T... items) {
ArrayList coll = new ArrayList();
Collections.addAll(coll, items);
return coll;
}
/**
* Wraps the given items in a mutable list.
*/
public static List toList(Iterable extends T> items) {
ArrayList coll = new ArrayList();
for (T item : items) {
coll.add(item);
}
return coll;
}
/**
* Wraps the given key and value in a mutable unordered map.
*/
public static Map toMap(K key, V value) {
Map map = new HashMap();
map.put(key, value);
return map;
}
@SafeVarargs
@SuppressWarnings("varargs")
public static T[] toArray(T... items) {
return items;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy