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.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentMap;

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

    private WeldCollections() {
    }

    /**
     * Returns an immutable view of a given set.
     */
    public static  Set immutableSetView(Set set) {
        if (set instanceof ImmutableSet) {
            return set;
        }
        return Collections.unmodifiableSet(set);
    }


    /**
     * Returns an immutable view of a given set.
     */
    public static  List immutableListView(List list) {
        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 map.
     */
    public static  Map immutableMapView(Map map) {
        if (map instanceof ImmutableMap) {
            return map;
        }
        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 builder = new StringBuilder("\n - "); for (Iterator iterator = collection.iterator(); iterator.hasNext();) { builder.append(iterator.next()); if (iterator.hasNext()) { builder.append(",\n - "); } } return builder.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); } /** * Utility method for working with maps. Unlike {@link Map#putIfAbsent(Object, Object)} this method always returns the value that ends up store in the map * which is either the old value (if any was present) or the new value (if it was stored in the map). * * @param map the map * @param key the key * @param value the value * @return the value that ends up store in the map which is either the old value (if any was present) or the new value (if it was stored in the map) */ public static V putIfAbsent(ConcurrentMap map, K key, V value) { V old = map.putIfAbsent(key, value); if (old != null) { return old; } return value; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy