1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 package org.apache.commons.collections4.bag; 18 19 import java.util.Set; 20 21 import org.apache.commons.collections4.Bag; 22 import org.apache.commons.collections4.Predicate; 23 import org.apache.commons.collections4.collection.PredicatedCollection; 24 25 /** 26 * Decorates another {@link Bag} to validate that additions 27 * match a specified predicate. 28 * <p> 29 * This bag exists to provide validation for the decorated bag. 30 * It is normally created to decorate an empty bag. 31 * If an object cannot be added to the bag, an {@link IllegalArgumentException} is thrown. 32 * </p> 33 * <p> 34 * One usage would be to ensure that no null entries are added to the bag. 35 * </p> 36 * <pre> 37 * Bag bag = PredicatedBag.predicatedBag(new HashBag(), NotNullPredicate.INSTANCE); 38 * </pre> 39 * <p> 40 * This class is Serializable from Commons Collections 3.1. 41 * </p> 42 * 43 * @param <E> the type of elements in this bag 44 * @since 3.0 45 */ 46 public class PredicatedBag<E> extends PredicatedCollection<E> implements Bag<E> { 47 48 /** Serialization version */ 49 private static final long serialVersionUID = -2575833140344736876L; 50 51 /** 52 * Factory method to create a predicated (validating) bag. 53 * <p> 54 * If there are any elements already in the bag being decorated, they 55 * are validated. 56 * 57 * @param <E> the type of the elements in the bag 58 * @param bag the bag to decorate, must not be null 59 * @param predicate the predicate to use for validation, must not be null 60 * @return a new predicated Bag 61 * @throws NullPointerException if bag or predicate is null 62 * @throws IllegalArgumentException if the bag contains invalid elements 63 * @since 4.0 64 */ 65 public static <E> PredicatedBag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate) { 66 return new PredicatedBag<>(bag, predicate); 67 } 68 69 /** 70 * Constructor that wraps (not copies). 71 * <p> 72 * If there are any elements already in the bag being decorated, they 73 * are validated. 74 * 75 * @param bag the bag to decorate, must not be null 76 * @param predicate the predicate to use for validation, must not be null 77 * @throws NullPointerException if bag or predicate is null 78 * @throws IllegalArgumentException if the bag contains invalid elements 79 */ 80 protected PredicatedBag(final Bag<E> bag, final Predicate<? super E> predicate) { 81 super(bag, predicate); 82 } 83 84 @Override 85 public boolean add(final E object, final int count) { 86 validate(object); 87 return decorated().add(object, count); 88 } 89 90 /** 91 * Gets the decorated bag. 92 * 93 * @return the decorated bag 94 */ 95 @Override 96 protected Bag<E> decorated() { 97 return (Bag<E>) super.decorated(); 98 } 99 100 @Override 101 public boolean equals(final Object object) { 102 return object == this || decorated().equals(object); 103 } 104 105 @Override 106 public int getCount(final Object object) { 107 return decorated().getCount(object); 108 } 109 110 @Override 111 public int hashCode() { 112 return decorated().hashCode(); 113 } 114 115 @Override 116 public boolean remove(final Object object, final int count) { 117 return decorated().remove(object, count); 118 } 119 120 @Override 121 public Set<E> uniqueSet() { 122 return decorated().uniqueSet(); 123 } 124 125 }