KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > set > PredicatedSortedSet


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.set;
17
18 import java.util.Comparator JavaDoc;
19 import java.util.SortedSet JavaDoc;
20
21 import org.apache.commons.collections.Predicate;
22
23 /**
24  * Decorates another <code>SortedSet</code> to validate that all additions
25  * match a specified predicate.
26  * <p>
27  * This set exists to provide validation for the decorated set.
28  * It is normally created to decorate an empty set.
29  * If an object cannot be added to the set, an IllegalArgumentException is thrown.
30  * <p>
31  * One usage would be to ensure that no null entries are added to the set.
32  * <pre>SortedSet set = PredicatedSortedSet.decorate(new TreeSet(), 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.6 $ $Date: 2004/06/03 22:02:13 $
38  *
39  * @author Stephen Colebourne
40  * @author Paul Jack
41  */

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

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

74     protected PredicatedSortedSet(SortedSet JavaDoc set, Predicate predicate) {
75         super(set, predicate);
76     }
77
78     /**
79      * Gets the sorted set being decorated.
80      *
81      * @return the decorated sorted set
82      */

83     private SortedSet JavaDoc getSortedSet() {
84         return (SortedSet JavaDoc) getCollection();
85     }
86
87     //-----------------------------------------------------------------------
88
public SortedSet JavaDoc subSet(Object JavaDoc fromElement, Object JavaDoc toElement) {
89         SortedSet JavaDoc sub = getSortedSet().subSet(fromElement, toElement);
90         return new PredicatedSortedSet(sub, predicate);
91     }
92
93     public SortedSet JavaDoc headSet(Object JavaDoc toElement) {
94         SortedSet JavaDoc sub = getSortedSet().headSet(toElement);
95         return new PredicatedSortedSet(sub, predicate);
96     }
97
98     public SortedSet JavaDoc tailSet(Object JavaDoc fromElement) {
99         SortedSet JavaDoc sub = getSortedSet().tailSet(fromElement);
100         return new PredicatedSortedSet(sub, predicate);
101     }
102
103     public Object JavaDoc first() {
104         return getSortedSet().first();
105     }
106
107     public Object JavaDoc last() {
108         return getSortedSet().last();
109     }
110
111     public Comparator JavaDoc comparator() {
112         return getSortedSet().comparator();
113     }
114
115 }
116
Popular Tags