View Javadoc
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    *      https://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  import org.apache.commons.collections4.multiset.PredicatedMultiSet;
25  
26  /**
27   * Decorates another {@link Bag} to validate that additions
28   * match a specified predicate.
29   * <p>
30   * This bag exists to provide validation for the decorated bag.
31   * It is normally created to decorate an empty bag.
32   * If an object cannot be added to the bag, an {@link IllegalArgumentException} is thrown.
33   * </p>
34   * <p>
35   * One usage would be to ensure that no null entries are added to the bag.
36   * </p>
37   * <pre>
38   * Bag bag = PredicatedBag.predicatedBag(new HashBag(), NotNullPredicate.INSTANCE);
39   * </pre>
40   * <p>
41   * This class is Serializable from Commons Collections 3.1.
42   * </p>
43   *
44   * @param <E> The type of elements in this bag
45   * @since 3.0
46   * @deprecated Since 4.6.0, use {@link PredicatedMultiSet} instead.
47   */
48  @Deprecated
49  public class PredicatedBag<E> extends PredicatedCollection<E> implements Bag<E> {
50  
51      /** Serialization version */
52      private static final long serialVersionUID = -2575833140344736876L;
53  
54      /**
55       * Factory method to create a predicated (validating) bag.
56       * <p>
57       * If there are any elements already in the bag being decorated, they
58       * are validated.
59       *
60       * @param <E> The type of the elements in the bag
61       * @param bag  The bag to decorate, must not be null
62       * @param predicate  The predicate to use for validation, must not be null
63       * @return A new predicated Bag
64       * @throws NullPointerException if bag or predicate is null
65       * @throws IllegalArgumentException if the bag contains invalid elements
66       * @since 4.0
67       */
68      public static <E> PredicatedBag<E> predicatedBag(final Bag<E> bag, final Predicate<? super E> predicate) {
69          return new PredicatedBag<>(bag, predicate);
70      }
71  
72      /**
73       * Constructor that wraps (not copies).
74       * <p>
75       * If there are any elements already in the bag being decorated, they
76       * are validated.
77       *
78       * @param bag  The bag to decorate, must not be null
79       * @param predicate  The predicate to use for validation, must not be null
80       * @throws NullPointerException if bag or predicate is null
81       * @throws IllegalArgumentException if the bag contains invalid elements
82       */
83      protected PredicatedBag(final Bag<E> bag, final Predicate<? super E> predicate) {
84          super(bag, predicate);
85      }
86  
87      @Override
88      public boolean add(final E object, final int count) {
89          validate(object);
90          return decorated().add(object, count);
91      }
92  
93      /**
94       * Gets the decorated bag.
95       *
96       * @return The decorated bag
97       */
98      @Override
99      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 }