org.gradle.util.WrapUtil Maven / Gradle / Ivy
/*
* Copyright 2007-2008 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;
import org.gradle.api.DomainObjectSet;
import org.gradle.api.Named;
import org.gradle.api.NamedDomainObjectSet;
import org.gradle.api.internal.DefaultDomainObjectSet;
import org.gradle.api.internal.DefaultNamedDomainObjectSet;
import org.gradle.internal.reflect.DirectInstantiator;
import java.util.*;
/**
* Common methods to wrap objects in generic collections.
*
*/
public class WrapUtil {
/**
* Wraps the given items in a mutable unordered set.
*/
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.
*/
public static DomainObjectSet toDomainObjectSet(Class type, T... items) {
return new DefaultDomainObjectSet(type, toSet(items));
}
/**
* Wraps the given items in a named domain object set.
*/
public static NamedDomainObjectSet toNamedDomainObjectSet(Class type, T... items) {
DefaultNamedDomainObjectSet domainObjectSet = new DefaultNamedDomainObjectSet(type, DirectInstantiator.INSTANCE);
CollectionUtils.addAll(domainObjectSet, items);
return domainObjectSet;
}
/**
* Wraps the given items in a mutable ordered set.
*/
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.
*/
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.
*/
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.
*/
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;
}
/**
* Wraps the given key and value in a mutable sorted map.
*/
public static SortedMap toSortedMap(K key, V value) {
SortedMap map = new TreeMap();
map.put(key, value);
return map;
}
/**
* Wraps the given key and value in a mutable ordered map.
*/
public static Map toLinkedMap(K key, V value) {
Map map = new LinkedHashMap();
map.put(key, value);
return map;
}
/**
* Wraps the given key and value in a mutable properties instance.
*/
public static Properties toProperties(String key, String value) {
Properties props = new Properties();
props.setProperty(key, value);
return props;
}
public static T[] toArray(T... items) {
return items;
}
public static Set asSet(Collection c) {
return new LinkedHashSet(c);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy