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>SortedBag bag = PredicatedSortedBag.decorate(new TreeBag(), NotNullPredicate.INSTANCE);</pre>
034 * <p>
035 * This class is Serializable from Commons Collections 3.1.
036 *
037 * @since 3.0
038 * @version $Id: PredicatedSortedBag.html 972421 2015-11-14 20:00:04Z tn $
039 */
040public class PredicatedSortedBag<E> extends PredicatedBag<E> implements SortedBag<E> {
041
042    /** Serialization version */
043    private static final long serialVersionUID = 3448581314086406616L;
044
045    /**
046     * Factory method to create a predicated (validating) bag.
047     * <p>
048     * If there are any elements already in the bag being decorated, they
049     * are validated.
050     *
051     * @param <E> the type of the elements in the bag
052     * @param bag  the bag to decorate, must not be null
053     * @param predicate  the predicate to use for validation, must not be null
054     * @return a new predicated SortedBag
055     * @throws IllegalArgumentException if bag or predicate is null
056     * @throws IllegalArgumentException if the bag contains invalid elements
057     * @since 4.0
058     */
059    public static <E> PredicatedSortedBag<E> predicatedSortedBag(final SortedBag<E> bag,
060                                                                 final Predicate<? super E> predicate) {
061        return new PredicatedSortedBag<E>(bag, predicate);
062    }
063
064    //-----------------------------------------------------------------------
065    /**
066     * Constructor that wraps (not copies).
067     * <p>If there are any elements already in the bag being decorated, they
068     * are validated.
069     *
070     * @param bag  the bag to decorate, must not be null
071     * @param predicate  the predicate to use for validation, must not be null
072     * @throws IllegalArgumentException if bag or predicate is null
073     * @throws IllegalArgumentException if the bag contains invalid elements
074     */
075    protected PredicatedSortedBag(final SortedBag<E> bag, final Predicate<? super E> predicate) {
076        super(bag, predicate);
077    }
078
079    /**
080     * Gets the decorated sorted bag.
081     *
082     * @return the decorated bag
083     */
084    @Override
085    protected SortedBag<E> decorated() {
086        return (SortedBag<E>) super.decorated();
087    }
088
089    //-----------------------------------------------------------------------
090
091    public E first() {
092        return decorated().first();
093    }
094
095    public E last() {
096        return decorated().last();
097    }
098
099    public Comparator<? super E> comparator() {
100        return decorated().comparator();
101    }
102
103}