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