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 */ 017 package org.apache.commons.collections.bag; 018 019 import java.util.Set; 020 021 import org.apache.commons.collections.Bag; 022 import org.apache.commons.collections.Predicate; 023 import org.apache.commons.collections.collection.PredicatedCollection; 024 025 /** 026 * Decorates another <code>Bag</code> to validate that additions 027 * match a specified predicate. 028 * <p> 029 * This bag exists to provide validation for the decorated bag. 030 * It is normally created to decorate an empty bag. 031 * If an object cannot be added to the bag, an IllegalArgumentException is thrown. 032 * <p> 033 * One usage would be to ensure that no null entries are added to the bag. 034 * <pre>Bag bag = PredicatedBag.decorate(new HashBag(), NotNullPredicate.INSTANCE);</pre> 035 * <p> 036 * This class is Serializable from Commons Collections 3.1. 037 * 038 * @since Commons Collections 3.0 039 * @version $Revision: 646777 $ $Date: 2008-04-10 13:33:15 +0100 (Thu, 10 Apr 2008) $ 040 * 041 * @author Stephen Colebourne 042 * @author Paul Jack 043 */ 044 public class PredicatedBag 045 extends PredicatedCollection implements Bag { 046 047 /** Serialization version */ 048 private static final long serialVersionUID = -2575833140344736876L; 049 050 /** 051 * Factory method to create a predicated (validating) bag. 052 * <p> 053 * If there are any elements already in the bag being decorated, they 054 * are validated. 055 * 056 * @param bag the bag to decorate, must not be null 057 * @param predicate the predicate to use for validation, must not be null 058 * @return a new predicated Bag 059 * @throws IllegalArgumentException if bag or predicate is null 060 * @throws IllegalArgumentException if the bag contains invalid elements 061 */ 062 public static Bag decorate(Bag bag, Predicate predicate) { 063 return new PredicatedBag(bag, predicate); 064 } 065 066 //----------------------------------------------------------------------- 067 /** 068 * Constructor that wraps (not copies). 069 * <p> 070 * If there are any elements already in the bag being decorated, they 071 * are validated. 072 * 073 * @param bag the bag to decorate, must not be null 074 * @param predicate the predicate to use for validation, must not be null 075 * @throws IllegalArgumentException if bag or predicate is null 076 * @throws IllegalArgumentException if the bag contains invalid elements 077 */ 078 protected PredicatedBag(Bag bag, Predicate predicate) { 079 super(bag, predicate); 080 } 081 082 /** 083 * Gets the decorated bag. 084 * 085 * @return the decorated bag 086 */ 087 protected Bag getBag() { 088 return (Bag) getCollection(); 089 } 090 091 //----------------------------------------------------------------------- 092 public boolean add(Object object, int count) { 093 validate(object); 094 return getBag().add(object, count); 095 } 096 097 public boolean remove(Object object, int count) { 098 return getBag().remove(object, count); 099 } 100 101 public Set uniqueSet() { 102 return getBag().uniqueSet(); 103 } 104 105 public int getCount(Object object) { 106 return getBag().getCount(object); 107 } 108 109 }