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