001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.collections4.multiset; 018 019import java.util.Set; 020 021import org.apache.commons.collections4.MultiSet; 022import org.apache.commons.collections4.Predicate; 023import org.apache.commons.collections4.collection.PredicatedCollection; 024 025/** 026 * Decorates another {@link MultiSet} to validate that additions 027 * match a specified predicate. 028 * <p> 029 * This multiset exists to provide validation for the decorated multiset. 030 * It is normally created to decorate an empty multiset. 031 * If an object cannot be added to the multiset, an {@link IllegalArgumentException} 032 * is thrown. 033 * </p> 034 * <p> 035 * One usage would be to ensure that no null entries are added to the multiset. 036 * </p> 037 * <pre> 038 * MultiSet<E> set = 039 * PredicatedMultiSet.predicatedMultiSet(new HashMultiSet<E>(), 040 * NotNullPredicate.notNullPredicate()); 041 * </pre> 042 * 043 * @param <E> the type held in the multiset 044 * @since 4.1 045 */ 046public class PredicatedMultiSet<E> extends PredicatedCollection<E> implements MultiSet<E> { 047 048 /** Serialization version */ 049 private static final long serialVersionUID = 20150629L; 050 051 /** 052 * Factory method to create a predicated (validating) multiset. 053 * <p> 054 * If there are any elements already in the multiset being decorated, they 055 * are validated. 056 * 057 * @param <E> the type of the elements in the multiset 058 * @param multiset the multiset to decorate, must not be null 059 * @param predicate the predicate to use for validation, must not be null 060 * @return a new predicated MultiSet 061 * @throws NullPointerException if multiset or predicate is null 062 * @throws IllegalArgumentException if the multiset contains invalid elements 063 */ 064 public static <E> PredicatedMultiSet<E> predicatedMultiSet(final MultiSet<E> multiset, 065 final Predicate<? super E> predicate) { 066 return new PredicatedMultiSet<>(multiset, predicate); 067 } 068 069 /** 070 * Constructor that wraps (not copies). 071 * <p> 072 * If there are any elements already in the multiset being decorated, they 073 * are validated. 074 * </p> 075 * 076 * @param multiset the multiset to decorate, must not be null 077 * @param predicate the predicate to use for validation, must not be null 078 * @throws NullPointerException if multiset or predicate is null 079 * @throws IllegalArgumentException if the multiset contains invalid elements 080 */ 081 protected PredicatedMultiSet(final MultiSet<E> multiset, final Predicate<? super E> predicate) { 082 super(multiset, predicate); 083 } 084 085 @Override 086 public int add(final E object, final int count) { 087 validate(object); 088 return decorated().add(object, count); 089 } 090 091 /** 092 * Gets the decorated multiset. 093 * 094 * @return the decorated multiset 095 */ 096 @Override 097 protected MultiSet<E> decorated() { 098 return (MultiSet<E>) super.decorated(); 099 } 100 101 @Override 102 public Set<MultiSet.Entry<E>> entrySet() { 103 return decorated().entrySet(); 104 } 105 106 @Override 107 public boolean equals(final Object object) { 108 return object == this || decorated().equals(object); 109 } 110 111 @Override 112 public int getCount(final Object object) { 113 return decorated().getCount(object); 114 } 115 116 @Override 117 public int hashCode() { 118 return decorated().hashCode(); 119 } 120 121 @Override 122 public int remove(final Object object, final int count) { 123 return decorated().remove(object, count); 124 } 125 126 @Override 127 public int setCount(final E object, final int count) { 128 validate(object); 129 return decorated().setCount(object, count); 130 } 131 132 @Override 133 public Set<E> uniqueSet() { 134 return decorated().uniqueSet(); 135 } 136 137}