PredicatedMultiSet.java

  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. import java.util.Set;

  19. import org.apache.commons.collections4.MultiSet;
  20. import org.apache.commons.collections4.Predicate;
  21. import org.apache.commons.collections4.collection.PredicatedCollection;

  22. /**
  23.  * Decorates another {@link MultiSet} to validate that additions
  24.  * match a specified predicate.
  25.  * <p>
  26.  * This multiset exists to provide validation for the decorated multiset.
  27.  * It is normally created to decorate an empty multiset.
  28.  * If an object cannot be added to the multiset, an {@link IllegalArgumentException}
  29.  * is thrown.
  30.  * </p>
  31.  * <p>
  32.  * One usage would be to ensure that no null entries are added to the multiset.
  33.  * </p>
  34.  * <pre>
  35.  * MultiSet&lt;E&gt; set =
  36.  *      PredicatedMultiSet.predicatedMultiSet(new HashMultiSet&lt;E&gt;(),
  37.  *                                            NotNullPredicate.notNullPredicate());
  38.  * </pre>
  39.  *
  40.  * @param <E> the type held in the multiset
  41.  * @since 4.1
  42.  */
  43. public class PredicatedMultiSet<E> extends PredicatedCollection<E> implements MultiSet<E> {

  44.     /** Serialization version */
  45.     private static final long serialVersionUID = 20150629L;

  46.     /**
  47.      * Factory method to create a predicated (validating) multiset.
  48.      * <p>
  49.      * If there are any elements already in the multiset being decorated, they
  50.      * are validated.
  51.      *
  52.      * @param <E> the type of the elements in the multiset
  53.      * @param multiset  the multiset to decorate, must not be null
  54.      * @param predicate  the predicate to use for validation, must not be null
  55.      * @return a new predicated MultiSet
  56.      * @throws NullPointerException if multiset or predicate is null
  57.      * @throws IllegalArgumentException if the multiset contains invalid elements
  58.      */
  59.     public static <E> PredicatedMultiSet<E> predicatedMultiSet(final MultiSet<E> multiset,
  60.                                                                final Predicate<? super E> predicate) {
  61.         return new PredicatedMultiSet<>(multiset, predicate);
  62.     }

  63.     /**
  64.      * Constructor that wraps (not copies).
  65.      * <p>
  66.      * If there are any elements already in the multiset being decorated, they
  67.      * are validated.
  68.      * </p>
  69.      *
  70.      * @param multiset  the multiset to decorate, must not be null
  71.      * @param predicate  the predicate to use for validation, must not be null
  72.      * @throws NullPointerException if multiset or predicate is null
  73.      * @throws IllegalArgumentException if the multiset contains invalid elements
  74.      */
  75.     protected PredicatedMultiSet(final MultiSet<E> multiset, final Predicate<? super E> predicate) {
  76.         super(multiset, predicate);
  77.     }

  78.     @Override
  79.     public int add(final E object, final int count) {
  80.         validate(object);
  81.         return decorated().add(object, count);
  82.     }

  83.     /**
  84.      * Gets the decorated multiset.
  85.      *
  86.      * @return the decorated multiset
  87.      */
  88.     @Override
  89.     protected MultiSet<E> decorated() {
  90.         return (MultiSet<E>) super.decorated();
  91.     }

  92.     @Override
  93.     public Set<MultiSet.Entry<E>> entrySet() {
  94.         return decorated().entrySet();
  95.     }

  96.     @Override
  97.     public boolean equals(final Object object) {
  98.         return object == this || decorated().equals(object);
  99.     }

  100.     @Override
  101.     public int getCount(final Object object) {
  102.         return decorated().getCount(object);
  103.     }

  104.     @Override
  105.     public int hashCode() {
  106.         return decorated().hashCode();
  107.     }

  108.     @Override
  109.     public int remove(final Object object, final int count) {
  110.         return decorated().remove(object, count);
  111.     }

  112.     @Override
  113.     public int setCount(final E object, final int count) {
  114.         validate(object);
  115.         return decorated().setCount(object, count);
  116.     }

  117.     @Override
  118.     public Set<E> uniqueSet() {
  119.         return decorated().uniqueSet();
  120.     }

  121. }