    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 *      http://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;
    023
    024/**
    025 * Decorates another {@link SortedBag} to validate that additions
    026 * match a specified predicate.
    027 * <p>
    028 * This bag exists to provide validation for the decorated bag.
    029 * It is normally created to decorate an empty bag.
    030 * If an object cannot be added to the bag, an {@link IllegalArgumentException} is thrown.
    031 * <p>
    032 * One usage would be to ensure that no null entries are added to the bag.
    033 * <pre>
    034 * SortedBag bag = PredicatedSortedBag.predicatedSortedBag(new TreeBag(), NotNullPredicate.INSTANCE);
    035 * </pre>
    036 * <p>
    037 * This class is Serializable from Commons Collections 3.1.
    038 *
    039 * @param <E> the type of elements in this bag
    040 * @since 3.0
    041 */
    042public class PredicatedSortedBag<E> extends PredicatedBag<E> implements SortedBag<E> {
    043
    044    /** Serialization version */
    045    private static final long serialVersionUID = 3448581314086406616L;
    046
    047    /**
    048     * Factory method to create a predicated (validating) bag.
    049     * <p>
    050     * If there are any elements already in the bag being decorated, they
    051     * are validated.
    052     *
    053     * @param <E> the type of the elements in the bag
    054     * @param bag  the bag to decorate, must not be null
    055     * @param predicate  the predicate to use for validation, must not be null
    056     * @return a new predicated SortedBag
    057     * @throws NullPointerException if bag or predicate is null
    058     * @throws IllegalArgumentException if the bag contains invalid elements
    059     * @since 4.0
    060     */
    061    public static <E> PredicatedSortedBag<E> predicatedSortedBag(final SortedBag<E> bag,
    062                                                                 final Predicate<? super E> predicate) {
    063        return new PredicatedSortedBag<>(bag, predicate);
    064    }
    065
    066    //-----------------------------------------------------------------------
    067    /**
    068     * Constructor that wraps (not copies).
    069     * <p>If there are any elements already in the bag being decorated, they
    070     * are validated.
    071     *
    072     * @param bag  the bag to decorate, must not be null
    073     * @param predicate  the predicate to use for validation, must not be null
    074     * @throws NullPointerException if bag or predicate is null
    075     * @throws IllegalArgumentException if the bag contains invalid elements
    076     */
    077    protected PredicatedSortedBag(final SortedBag<E> bag, final Predicate<? super E> predicate) {
    078        super(bag, predicate);
    079    }
    080
    081    /**
    082     * Gets the decorated sorted bag.
    083     *
    084     * @return the decorated bag
    085     */
    086    @Override
    087    protected SortedBag<E> decorated() {
    088        return (SortedBag<E>) super.decorated();
    089    }
    090
    091    //-----------------------------------------------------------------------
    092
    093    @Override
    094    public E first() {
    095        return decorated().first();
    096    }
    097
    098    @Override
    099    public E last() {
    100        return decorated().last();
    101    }
    102
    103    @Override
    104    public Comparator<? super E> comparator() {
    105        return decorated().comparator();
    106    }
    107
    108}