PredicatedSet.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.set;

  18. import java.util.Set;

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

  21. /**
  22.  * Decorates another {@code Set} to validate that all additions
  23.  * match a specified predicate.
  24.  * <p>
  25.  * This set exists to provide validation for the decorated set.
  26.  * It is normally created to decorate an empty set.
  27.  * If an object cannot be added to the set, an IllegalArgumentException is thrown.
  28.  * </p>
  29.  * <p>
  30.  * One usage would be to ensure that no null entries are added to the set.
  31.  * </p>
  32.  * <pre>Set set = PredicatedSet.decorate(new HashSet(), NotNullPredicate.INSTANCE);</pre>
  33.  * <p>
  34.  * This class is Serializable from Commons Collections 3.1.
  35.  * </p>
  36.  *
  37.  * @param <E> the type of the elements in this set
  38.  * @since 3.0
  39.  */
  40. public class PredicatedSet<E> extends PredicatedCollection<E> implements Set<E> {

  41.     /** Serialization version */
  42.     private static final long serialVersionUID = -684521469108685117L;

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

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

  74.     /**
  75.      * Gets the set being decorated.
  76.      *
  77.      * @return the decorated set
  78.      */
  79.     @Override
  80.     protected Set<E> decorated() {
  81.         return (Set<E>) super.decorated();
  82.     }

  83.     @Override
  84.     public boolean equals(final Object object) {
  85.         return object == this || decorated().equals(object);
  86.     }

  87.     @Override
  88.     public int hashCode() {
  89.         return decorated().hashCode();
  90.     }

  91. }