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

org.mapstruct.ap.internal.util.Collections Maven / Gradle / Ivy

/*
 * 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();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy