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 * https://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.bag; 018 019import java.util.Set; 020 021import org.apache.commons.collections4.Bag; 022import org.apache.commons.collections4.Predicate; 023import org.apache.commons.collections4.collection.PredicatedCollection; 024import org.apache.commons.collections4.multiset.PredicatedMultiSet; 025 026/** 027 * Decorates another {@link Bag} to validate that additions 028 * match a specified predicate. 029 * <p> 030 * This bag exists to provide validation for the decorated bag. 031 * It is normally created to decorate an empty bag. 032 * If an object cannot be added to the bag, an {@link IllegalArgumentException} is thrown. 033 * </p> 034 * <p> 035 * One usage would be to ensure that no null entries are added to the bag. 036 * </p> 037 * <pre> 038 * Bag bag = PredicatedBag.predicatedBag(new HashBag(), NotNullPredicate.INSTANCE); 039 * </pre> 040 * <p> 041 * This class is Serializable from Commons Collections 3.1. 042 * </p> 043 * 044 * @param <E> The type of elements in this bag 045 * @since 3.0 046 * @deprecated Since 4.6.0, use {@link PredicatedMultiSet} instead. 047 */ 048@Deprecated 049public class PredicatedBag<E> extends PredicatedCollection<E> implements Bag<E> { 050 051 /** Serialization version */ 052 private static final long serialVersionUID = -2575833140344736876L; 053 054 /** 055 * Factory method to create a predicated (validating) bag. 056 * <p> 057 * If there are any elements already in the bag being decorated, they 058 * are validated. 059 * 060 * @param <E> The type of the elements in the bag 061 * @param bag The bag to decorate, must not be null 062 * @param predicate The predicate to use for validation, must not be null 063 * @return A new predicated Bag 064 * @throws NullPointerException if bag or predicate is null 065 * @throws IllegalArgumentException if the bag contains invalid elements 066 * @since 4.0 067 */ 068 public static <E> PredicatedBag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate) { 069 return new PredicatedBag<>(bag, predicate); 070 } 071 072 /** 073 * Constructor that wraps (not copies). 074 * <p> 075 * If there are any elements already in the bag being decorated, they 076 * are validated. 077 * 078 * @param bag The bag to decorate, must not be null 079 * @param predicate The predicate to use for validation, must not be null 080 * @throws NullPointerException if bag or predicate is null 081 * @throws IllegalArgumentException if the bag contains invalid elements 082 */ 083 protected PredicatedBag(final Bag<E> bag, final Predicate<? super E> predicate) { 084 super(bag, predicate); 085 } 086 087 @Override 088 public boolean add(final E object, final int count) { 089 validate(object); 090 return decorated().add(object, count); 091 } 092 093 /** 094 * Gets the decorated bag. 095 * 096 * @return The decorated bag 097 */ 098 @Override 099 protected Bag<E> decorated() { 100 return (Bag<E>) super.decorated(); 101 } 102 103 @Override 104 public boolean equals(final Object object) { 105 return object == this || decorated().equals(object); 106 } 107 108 @Override 109 public int getCount(final Object object) { 110 return decorated().getCount(object); 111 } 112 113 @Override 114 public int hashCode() { 115 return decorated().hashCode(); 116 } 117 118 @Override 119 public boolean remove(final Object object, final int count) { 120 return decorated().remove(object, count); 121 } 122 123 @Override 124 public Set<E> uniqueSet() { 125 return decorated().uniqueSet(); 126 } 127 128}