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

org.jboss.weld.util.collections.WeldCollections Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2012, Red Hat, Inc., and individual contributors
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * 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.jboss.weld.util.collections;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jboss.weld.util.reflection.Reflections;

import com.google.common.base.Joiner;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;

/**
 * Collection utilities.
 *
 * @author Jozef Hartinger
 *
 */
public class WeldCollections {

    public static final Map> EMPTY_ARRAY_SET_MULTIMAP = Collections.unmodifiableMap(new ArraySetMultimap().trimToSize());

    private WeldCollections() {
    }

    /**
     * Returns an immutable view of a given set. If the given set is empty, a shared instance is returned. If the given set is
     * an instance of {@link ArraySet}, it is trimmed.
     */
    public static  Set immutableSet(Set set) {
        if (set.isEmpty()) {
            return Collections.emptySet();
        }
        if (set instanceof ImmutableSet) {
            return set;
        }
        if (set instanceof ArraySet) {
            ArraySet.class.cast(set).trimToSize();
        }
        return Collections.unmodifiableSet(set);
    }

    /**
     * Returns an immutable view of a given set. If the given set is empty, a shared instance is returned.
     */
    public static  Set immutableGuavaSet(Set set) {
        if (set.isEmpty()) {
            return Collections.emptySet();
        }
        if (set instanceof ImmutableSet) {
            return set;
        }
        return ImmutableSet.copyOf(set);
    }


    /**
     * Returns an immutable view of a given list. If the given list is empty, a shared instance is returned. If the given list
     * is an instance of {@link ArrayList}, it is trimmed.
     */
    public static  List immutableList(List list) {
        if (list.isEmpty()) {
            return Collections.emptyList();
        }
        if (list instanceof ImmutableList) {
            return list;
        }
        if (list instanceof ArrayList) {
            ArrayList.class.cast(list).trimToSize();
        }
        return Collections.unmodifiableList(list);
    }

    /**
     * Returns an immutable view of a given list. If the given list is empty, a shared instance is returned.
     */
    public static  List immutableGuavaList(List list) {
        if (list.isEmpty()) {
            return Collections.emptyList();
        }
        if (list instanceof ImmutableList) {
            return list;
        }
        return ImmutableList.copyOf(list);
    }

    /**
     * Returns an immutable view of a given map. If the given map is empty, a shared instance is returned.
     */
    public static  Map immutableMap(Map map) {
        if (map.isEmpty()) {
            if (map instanceof ArraySetMultimap) {
                return Reflections.cast(EMPTY_ARRAY_SET_MULTIMAP);
            }
            return Collections.emptyMap();
        }
        if (map instanceof ImmutableMap) {
            return map;
        }
        if (map instanceof ArraySetMultimap) {
            ArraySetMultimap.class.cast(map).trimToSize();
        }
        return Collections.unmodifiableMap(map);
    }

    /**
     * Fluent version of {@link Collections#sort(List, Comparator)}
     */
    public static  List sort(List list, Comparator comparator) {
        Collections.sort(list, comparator);
        return list;
    }

    /**
     * Returns the supplied collection as a multi-row string with every toString() of every element of the collection
     * in its own row.
     * Example: toMultiRowString(Arrays.asList("aaa", "bbb", "ccc")) will return:
     * 
     *   - aaa,
     *   - bbb,
     *   - ccc
     * 
*/ public static String toMultiRowString(Collection collection) { if (collection == null) { return null; } if (collection.isEmpty()) { return "(empty collection)"; } StringBuilder sb = new StringBuilder("\n - "); Joiner.on(",\n - ").appendTo(sb, collection); return sb.toString(); } /** * * @param collection * @param elements * @return true if the collection changed as a result of the call */ public static boolean addAll(Collection collection, E... elements) { boolean modified = false; for (E element : elements) { modified = modified | collection.add(element); } return modified; } public static boolean addIfNotNull(Collection collection, T element) { if (element == null) { return false; } return collection.add(element); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy