org.mapstruct.ap.internal.util.Collections Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of mapstruct-processor Show documentation
Show all versions of mapstruct-processor Show documentation
An annotation processor for generating type-safe bean mappers
/*
* Copyright MapStruct Authors.
*
* Licensed under the Apache License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package org.mapstruct.ap.internal.util;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Provides utility methods around collections.
*
* @author Gunnar Morling
*/
public class Collections {
private Collections() {
}
@SafeVarargs
public static Set asSet(T... elements) {
Set set = new HashSet<>( elements.length );
java.util.Collections.addAll( set, elements );
return set;
}
@SafeVarargs
public static Set asSet(Collection collection, T... elements) {
Set set = new HashSet<>( collection.size() + elements.length );
java.util.Collections.addAll( set, elements );
return set;
}
@SafeVarargs
public static Set asSet(Collection collection, Collection... elements) {
Set set = new HashSet<>( collection );
for ( Collection element : elements ) {
set.addAll( element );
}
return set;
}
public static T first(Collection collection) {
return collection.iterator().next();
}
public static T last(List list) {
return list.get( list.size() - 1 );
}
public static List join(List a, List b) {
List result = new ArrayList<>( a.size() + b.size() );
result.addAll( a );
result.addAll( b );
return result;
}
public static Map.Entry first(Map map) {
return map.entrySet().iterator().next();
}
public static V firstValue(Map map) {
return first( map ).getValue();
}
public static K firstKey(Map map) {
return first( map ).getKey();
}
}