001/* 002 * Syncany, www.syncany.org 003 * Copyright (C) 2011-2016 Philipp C. Heckel <philipp.heckel@gmail.com> 004 * 005 * This program is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU General Public License as published by 007 * the Free Software Foundation, either version 3 of the License, or 008 * (at your option) any later version. 009 * 010 * This program is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU General Public License for more details. 014 * 015 * You should have received a copy of the GNU General Public License 016 * along with this program. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package org.syncany.util; 019 020import java.util.Arrays; 021import java.util.Collection; 022 023public abstract class CollectionUtil { 024 /** 025 * Checks if the collection contains only the given allowed items. <code>list</code> 026 * may contain zero to max(allowedItems) items from allowedItems. 027 * 028 * <p><b>Examples:</b><br> 029 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 })) --> true</code><br> 030 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 }), 1, 2) --> false, 3 missing</code><br> 031 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 }), 1, 99) --> false, 3 missing</code> 032 */ 033 @SafeVarargs 034 public static <T> boolean containsOnly(Collection<T> list, T... allowedItems) { 035 return containsOnly(list, Arrays.asList(allowedItems)); 036 } 037 038 /** 039 * Checks if the collection contains only the given allowed items. <code>list</code> 040 * may contain zero to max(allowedItems) items from allowedItems. 041 * 042 * <p><b>Examples:</b><br> 043 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 }), Arrays.asList(new Integer[] { })) --> true</code><br> 044 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 }), Arrays.asList(new Integer[] { 1, 2 })) --> false, 3 missing</code><br> 045 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 }), Arrays.asList(new Integer[] { 1, 99 })) --> false, 3 missing</code> 046 */ 047 public static <T> boolean containsOnly(Collection<T> list, Collection<T> allowedItems) { 048 for (T item : list) { 049 if (!allowedItems.contains(item)) { 050 return false; 051 } 052 } 053 054 return true; 055 } 056 057 /** 058 * Checks if the collection contains exactly the given <code>mustHaveItems</code>. 059 * 060 * <p><b>Examples:</b><br> 061 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 })) --> false</code><br> 062 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 }), 1, 99) --> false</code><br> 063 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 }), 1, 2, 3) --> true</code> 064 */ 065 @SafeVarargs 066 public static <T> boolean containsExactly(Collection<T> list, T... mustHaveItems) { 067 return containsExactly(list, Arrays.asList(mustHaveItems)); 068 } 069 070 /** 071 * Checks if the collection contains exactly the given <code>mustHaveItems</code>. 072 * 073 * <p><b>Examples:</b><br> 074 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 }), Arrays.asList(new Integer[] { })) --> false</code><br> 075 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 }), Arrays.asList(new Integer[] { 1, 99 })) --> false</code><br> 076 * <code>containsOnly(Arrays.asList(new Integer[] { 1, 2, 3 }), Arrays.asList(new Integer[] { 1, 2, 3 })) --> true</code> 077 */ 078 public static <T> boolean containsExactly(Collection<T> list, Collection<T> mustHaveItems) { 079 return list.containsAll(mustHaveItems) && mustHaveItems.containsAll(list); 080 } 081}