1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17 package org.apache.commons.collections.set;
18 
19 import java.util.Comparator;
20 import java.util.SortedSet;
21 
22 import org.apache.commons.collections.Predicate;
23 
24 /**
25  * Decorates another <code>SortedSet</code> to validate that all additions
26  * match a specified predicate.
27  * <p>
28  * This set exists to provide validation for the decorated set.
29  * It is normally created to decorate an empty set.
30  * If an object cannot be added to the set, an IllegalArgumentException is thrown.
31  * <p>
32  * One usage would be to ensure that no null entries are added to the set.
33  * <pre>SortedSet set = PredicatedSortedSet.decorate(new TreeSet(), NotNullPredicate.INSTANCE);</pre>
34  * <p>
35  * This class is Serializable from Commons Collections 3.1.
36  *
37  * @since Commons Collections 3.0
38  * @version $Revision: 646777 $ $Date: 2008-04-10 14:33:15 +0200 (Thu, 10 Apr 2008) $
39  *
40  * @author Stephen Colebourne
41  * @author Paul Jack
42  */
43 public class PredicatedSortedSet extends PredicatedSet implements SortedSet {
44 
45     /** Serialization version */
46     private static final long serialVersionUID = -9110948148132275052L;
47 
48     /**
49      * Factory method to create a predicated (validating) sorted set.
50      * <p>
51      * If there are any elements already in the set being decorated, they
52      * are validated.
53      *
54      * @param set  the set to decorate, must not be null
55      * @param predicate  the predicate to use for validation, must not be null
56      * @throws IllegalArgumentException if set or predicate is null
57      * @throws IllegalArgumentException if the set contains invalid elements
58      */
decorate(SortedSet set, Predicate predicate)59     public static SortedSet decorate(SortedSet set, Predicate predicate) {
60         return new PredicatedSortedSet(set, predicate);
61     }
62 
63     //-----------------------------------------------------------------------
64     /**
65      * Constructor that wraps (not copies).
66      * <p>
67      * If there are any elements already in the set being decorated, they
68      * are validated.
69      *
70      * @param set  the set to decorate, must not be null
71      * @param predicate  the predicate to use for validation, must not be null
72      * @throws IllegalArgumentException if set or predicate is null
73      * @throws IllegalArgumentException if the set contains invalid elements
74      */
PredicatedSortedSet(SortedSet set, Predicate predicate)75     protected PredicatedSortedSet(SortedSet set, Predicate predicate) {
76         super(set, predicate);
77     }
78 
79     /**
80      * Gets the sorted set being decorated.
81      *
82      * @return the decorated sorted set
83      */
getSortedSet()84     private SortedSet getSortedSet() {
85         return (SortedSet) getCollection();
86     }
87 
88     //-----------------------------------------------------------------------
subSet(Object fromElement, Object toElement)89     public SortedSet subSet(Object fromElement, Object toElement) {
90         SortedSet sub = getSortedSet().subSet(fromElement, toElement);
91         return new PredicatedSortedSet(sub, predicate);
92     }
93 
headSet(Object toElement)94     public SortedSet headSet(Object toElement) {
95         SortedSet sub = getSortedSet().headSet(toElement);
96         return new PredicatedSortedSet(sub, predicate);
97     }
98 
tailSet(Object fromElement)99     public SortedSet tailSet(Object fromElement) {
100         SortedSet sub = getSortedSet().tailSet(fromElement);
101         return new PredicatedSortedSet(sub, predicate);
102     }
103 
first()104     public Object first() {
105         return getSortedSet().first();
106     }
107 
last()108     public Object last() {
109         return getSortedSet().last();
110     }
111 
comparator()112     public Comparator comparator() {
113         return getSortedSet().comparator();
114     }
115 
116 }
117