KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > bag > PredicatedSortedBag


1 /*
2  * Copyright 2003-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.bag;
17
18 import java.util.Comparator JavaDoc;
19
20 import org.apache.commons.collections.Predicate;
21 import org.apache.commons.collections.SortedBag;
22
23 /**
24  * Decorates another <code>SortedBag</code> to validate that additions
25  * match a specified predicate.
26  * <p>
27  * This bag exists to provide validation for the decorated bag.
28  * It is normally created to decorate an empty bag.
29  * If an object cannot be added to the bag, an IllegalArgumentException is thrown.
30  * <p>
31  * One usage would be to ensure that no null entries are added to the bag.
32  * <pre>SortedBag bag = PredicatedSortedBag.decorate(new TreeBag(), NotNullPredicate.INSTANCE);</pre>
33  * <p>
34  * This class is Serializable from Commons Collections 3.1.
35  *
36  * @since Commons Collections 3.0
37  * @version $Revision: 1.8 $ $Date: 2004/06/03 22:02:12 $
38  *
39  * @author Stephen Colebourne
40  * @author Paul Jack
41  */

42 public class PredicatedSortedBag
43         extends PredicatedBag implements SortedBag {
44
45     /** Serialization version */
46     private static final long serialVersionUID = 3448581314086406616L;
47
48     /**
49      * Factory method to create a predicated (validating) bag.
50      * <p>
51      * If there are any elements already in the bag being decorated, they
52      * are validated.
53      *
54      * @param bag the bag to decorate, must not be null
55      * @param predicate the predicate to use for validation, must not be null
56      * @return a new predicated SortedBag
57      * @throws IllegalArgumentException if bag or predicate is null
58      * @throws IllegalArgumentException if the bag contains invalid elements
59      */

60     public static SortedBag decorate(SortedBag bag, Predicate predicate) {
61         return new PredicatedSortedBag(bag, predicate);
62     }
63
64     //-----------------------------------------------------------------------
65
/**
66      * Constructor that wraps (not copies).
67      * <p>
68      * If there are any elements already in the bag being decorated, they
69      * are validated.
70      *
71      * @param bag the bag to decorate, must not be null
72      * @param predicate the predicate to use for validation, must not be null
73      * @throws IllegalArgumentException if bag or predicate is null
74      * @throws IllegalArgumentException if the bag contains invalid elements
75      */

76     protected PredicatedSortedBag(SortedBag bag, Predicate predicate) {
77         super(bag, predicate);
78     }
79
80     /**
81      * Gets the decorated sorted bag.
82      *
83      * @return the decorated bag
84      */

85     protected SortedBag getSortedBag() {
86         return (SortedBag) getCollection();
87     }
88     
89     //-----------------------------------------------------------------------
90
public Object JavaDoc first() {
91         return getSortedBag().first();
92     }
93
94     public Object JavaDoc last() {
95         return getSortedBag().last();
96     }
97
98     public Comparator JavaDoc comparator() {
99         return getSortedBag().comparator();
100     }
101
102 }
103
Popular Tags