001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      https://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.collections4.bag;
018
019import java.util.Comparator;
020
021import org.apache.commons.collections4.Predicate;
022import org.apache.commons.collections4.SortedBag;
023import org.apache.commons.collections4.multiset.PredicatedSortedMultiSet;
024
025/**
026 * Decorates another {@link SortedBag} to validate that additions
027 * match a specified predicate.
028 * <p>
029 * This bag exists to provide validation for the decorated bag.
030 * It is normally created to decorate an empty bag.
031 * If an object cannot be added to the bag, an {@link IllegalArgumentException} is thrown.
032 * </p>
033 * <p>
034 * One usage would be to ensure that no null entries are added to the bag.
035 * <pre>
036 * SortedBag bag = PredicatedSortedBag.predicatedSortedBag(new TreeBag(), NotNullPredicate.INSTANCE);
037 * </pre>
038 * <p>
039 * This class is Serializable from Commons Collections 3.1.
040 * </p>
041 *
042 * @param <E> The type of elements in this bag
043 * @since 3.0
044 * @deprecated Since 4.6.0, use {@link PredicatedSortedMultiSet} instead.
045 */
046@Deprecated
047public class PredicatedSortedBag<E> extends PredicatedBag<E> implements SortedBag<E> {
048
049    /** Serialization version */
050    private static final long serialVersionUID = 3448581314086406616L;
051
052    /**
053     * Factory method to create a predicated (validating) bag.
054     * <p>
055     * If there are any elements already in the bag being decorated, they
056     * are validated.
057     *
058     * @param <E> The type of the elements in the bag
059     * @param bag  The bag to decorate, must not be null
060     * @param predicate  The predicate to use for validation, must not be null
061     * @return A new predicated SortedBag
062     * @throws NullPointerException if bag or predicate is null
063     * @throws IllegalArgumentException if the bag contains invalid elements
064     * @since 4.0
065     */
066    public static <E> PredicatedSortedBag<E> predicatedSortedBag(final SortedBag<E> bag,
067                                                                 final Predicate<? super E> predicate) {
068        return new PredicatedSortedBag<>(bag, predicate);
069    }
070
071    /**
072     * Constructor that wraps (not copies).
073     * <p>If there are any elements already in the bag being decorated, they
074     * are validated.
075     *
076     * @param bag  The bag to decorate, must not be null
077     * @param predicate  The predicate to use for validation, must not be null
078     * @throws NullPointerException if bag or predicate is null
079     * @throws IllegalArgumentException if the bag contains invalid elements
080     */
081    protected PredicatedSortedBag(final SortedBag<E> bag, final Predicate<? super E> predicate) {
082        super(bag, predicate);
083    }
084
085    @Override
086    public Comparator<? super E> comparator() {
087        return decorated().comparator();
088    }
089
090    /**
091     * Gets the decorated sorted bag.
092     *
093     * @return The decorated bag
094     */
095    @Override
096    protected SortedBag<E> decorated() {
097        return (SortedBag<E>) super.decorated();
098    }
099
100    @Override
101    public E first() {
102        return decorated().first();
103    }
104
105    @Override
106    public E last() {
107        return decorated().last();
108    }
109
110}