001: /*
002: * Copyright 2003-2004 The Apache Software Foundation
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.commons.collections.bag;
017:
018: import java.util.Comparator;
019:
020: import org.apache.commons.collections.Predicate;
021: import org.apache.commons.collections.SortedBag;
022:
023: /**
024: * Decorates another <code>SortedBag</code> to validate that additions
025: * match a specified predicate.
026: * <p>
027: * This bag exists to provide validation for the decorated bag.
028: * It is normally created to decorate an empty bag.
029: * If an object cannot be added to the bag, an IllegalArgumentException is thrown.
030: * <p>
031: * One usage would be to ensure that no null entries are added to the bag.
032: * <pre>SortedBag bag = PredicatedSortedBag.decorate(new TreeBag(), NotNullPredicate.INSTANCE);</pre>
033: * <p>
034: * This class is Serializable from Commons Collections 3.1.
035: *
036: * @since Commons Collections 3.0
037: * @version $Revision: 155406 $ $Date: 2005-02-26 12:55:26 +0000 (Sat, 26 Feb 2005) $
038: *
039: * @author Stephen Colebourne
040: * @author Paul Jack
041: */
042: public class PredicatedSortedBag extends PredicatedBag implements
043: SortedBag {
044:
045: /** Serialization version */
046: private static final long serialVersionUID = 3448581314086406616L;
047:
048: /**
049: * Factory method to create a predicated (validating) bag.
050: * <p>
051: * If there are any elements already in the bag being decorated, they
052: * are validated.
053: *
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 IllegalArgumentException if bag or predicate is null
058: * @throws IllegalArgumentException if the bag contains invalid elements
059: */
060: public static SortedBag decorate(SortedBag bag, Predicate predicate) {
061: return new PredicatedSortedBag(bag, predicate);
062: }
063:
064: //-----------------------------------------------------------------------
065: /**
066: * Constructor that wraps (not copies).
067: * <p>
068: * If there are any elements already in the bag being decorated, they
069: * are validated.
070: *
071: * @param bag the bag to decorate, must not be null
072: * @param predicate the predicate to use for validation, must not be null
073: * @throws IllegalArgumentException if bag or predicate is null
074: * @throws IllegalArgumentException if the bag contains invalid elements
075: */
076: protected PredicatedSortedBag(SortedBag bag, Predicate predicate) {
077: super (bag, predicate);
078: }
079:
080: /**
081: * Gets the decorated sorted bag.
082: *
083: * @return the decorated bag
084: */
085: protected SortedBag getSortedBag() {
086: return (SortedBag) getCollection();
087: }
088:
089: //-----------------------------------------------------------------------
090: public Object first() {
091: return getSortedBag().first();
092: }
093:
094: public Object last() {
095: return getSortedBag().last();
096: }
097:
098: public Comparator comparator() {
099: return getSortedBag().comparator();
100: }
101:
102: }
|