components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/misc/CollectionUtil.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.misc;
       
    27 
       
    28 import java.util.*;
       
    29 import com.oracle.solaris.vp.util.misc.converter.Converter;
       
    30 import com.oracle.solaris.vp.util.misc.predicate.Predicate;
       
    31 
       
    32 public class CollectionUtil {
       
    33     public static <T> void addAll(Collection<T> collection, T... array) {
       
    34 	for (T t : array) {
       
    35 	    collection.add(t);
       
    36 	}
       
    37     }
       
    38 
       
    39     public static <T> Set<T> clone(Set<T> set) {
       
    40 	Set<T> clone = new HashSet<T>();
       
    41 	clone.addAll(set);
       
    42 	return clone;
       
    43     }
       
    44 
       
    45     public static <T> List<T> clone(List<T> list) {
       
    46 	List<T> clone = new ArrayList<T>(list.size());
       
    47 	clone.addAll(list);
       
    48 	return clone;
       
    49     }
       
    50 
       
    51     public static <F, T> List<T> convert(
       
    52 	Collection<? extends F> from, Converter<F, T> converter) {
       
    53 
       
    54 	if (from == null) {
       
    55 	    return null;
       
    56 	}
       
    57 
       
    58 	List<T> to = new ArrayList<T>(from.size());
       
    59 	for (F f : from) {
       
    60 	    to.add(converter.convert(f));
       
    61 	}
       
    62 
       
    63 	return to;
       
    64     }
       
    65 
       
    66     /**
       
    67      * Returns a three-element array of mutable {@code List}s:
       
    68      * <ol>
       
    69      *	 </li>
       
    70      *	   the intersection of {@code a} and {@code b}
       
    71      *	 <li>
       
    72      *	 </li>
       
    73      *	   the {@code a} - {@code b} difference
       
    74      *	 <li>
       
    75      *	 </li>
       
    76      *	   the {@code b} - {@code a} difference
       
    77      *	 <li>
       
    78      * </ol>
       
    79      *
       
    80      * @param	    comparator
       
    81      *		    a comparator to sort results, or {@code null} to remain
       
    82      *		    unsorted
       
    83      */
       
    84     public static <T> List<T>[] getIntersectAndDiffs(
       
    85 	Collection<? extends T> a, Collection<? extends T> b,
       
    86 	Comparator<T> comparator) {
       
    87 
       
    88 	int aSize = a.size();
       
    89 	List<T> aDiff = new ArrayList<T>(aSize);
       
    90 	aDiff.addAll(a);
       
    91 
       
    92 	int bSize = b.size();
       
    93 	List<T> bDiff = new ArrayList<T>(bSize);
       
    94 	bDiff.addAll(b);
       
    95 
       
    96 	if (comparator != null) {
       
    97 	    Collections.sort(aDiff, comparator);
       
    98 	    Collections.sort(bDiff, comparator);
       
    99 	}
       
   100 
       
   101 	List<T> abInter = new ArrayList<T>(Math.min(aSize, bSize));
       
   102 
       
   103 	for (int i = aDiff.size() - 1; i >= 0; i--) {
       
   104 	    T element = aDiff.get(i);
       
   105 
       
   106 	    int index = comparator == null ? bDiff.indexOf(element) :
       
   107 		Collections.binarySearch(bDiff, element, comparator);
       
   108 
       
   109 	    if (index != -1) {
       
   110 		bDiff.remove(index);
       
   111 		aDiff.remove(i);
       
   112 		abInter.add(element);
       
   113 	    }
       
   114 	}
       
   115 
       
   116 	@SuppressWarnings({"unchecked"})
       
   117 	List<T>[] array = (List<T>[])new List[] {
       
   118 	    abInter, aDiff, bDiff
       
   119 	};
       
   120 
       
   121 	return array;
       
   122     }
       
   123 
       
   124     /**
       
   125      * Returns a modifiable {@code List} of matching elements.
       
   126      */
       
   127     public static <T> List<T> filter(
       
   128 	Collection<? extends T> collection, Predicate<T> matcher) {
       
   129 
       
   130 	List<T> subset = new ArrayList<T>(collection.size());
       
   131 	for (T element : collection) {
       
   132 	    if (matcher.test(element)) {
       
   133 		subset.add(element);
       
   134 	    }
       
   135 	}
       
   136 
       
   137 	return subset;
       
   138     }
       
   139 
       
   140     public static <T> void removeAll(Collection<T> collection, T... array) {
       
   141 	for (T t : array) {
       
   142 	    collection.remove(t);
       
   143 	}
       
   144     }
       
   145 }