001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util; 017 018import java.util.HashSet; 019import java.util.Set; 020 021public class SetUtils { 022 023 /** 024 * Return a new <code>Set</code> containing only those elements that appear in both <code>a</code> and <code>b</code> 025 */ 026 public static <T> Set<T> intersection(Set<T> a, Set<T> b) { 027 Set<T> result = new HashSet<T>(); 028 result.addAll(a); 029 result.retainAll(b); 030 return result; 031 } 032 033 /** 034 * Return a new <code>Set</code> containing all of the elements from both <code>a</code> and <code>b</code> 035 */ 036 public static <T> Set<T> union(Set<T> a, Set<T> b) { 037 Set<T> result = new HashSet<T>(); 038 result.addAll(a); 039 result.addAll(b); 040 return result; 041 } 042 043 /** 044 * Return a new <code>Set</code> containing only those elements that appear in <code>a</code> but not <code>b</code> 045 */ 046 public static <T> Set<T> difference(Set<T> a, Set<T> b) { 047 Set<T> result = new HashSet<T>(); 048 result.addAll(a); 049 result.removeAll(b); 050 return result; 051 } 052 053}