KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > PredicatedSortedMap


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

42 public class PredicatedSortedMap
43         extends PredicatedMap
44         implements SortedMap JavaDoc {
45
46     /** Serialization version */
47     private static final long serialVersionUID = 3359846175935304332L;
48
49     /**
50      * Factory method to create a predicated (validating) sorted map.
51      * <p>
52      * If there are any elements already in the list being decorated, they
53      * are validated.
54      *
55      * @param map the map to decorate, must not be null
56      * @param keyPredicate the predicate to validate the keys, null means no check
57      * @param valuePredicate the predicate to validate to values, null means no check
58      * @throws IllegalArgumentException if the map is null
59      */

60     public static SortedMap JavaDoc decorate(SortedMap JavaDoc map, Predicate keyPredicate, Predicate valuePredicate) {
61         return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
62     }
63
64     //-----------------------------------------------------------------------
65
/**
66      * Constructor that wraps (not copies).
67      *
68      * @param map the map to decorate, must not be null
69      * @param keyPredicate the predicate to validate the keys, null means no check
70      * @param valuePredicate the predicate to validate to values, null means no check
71      * @throws IllegalArgumentException if the map is null
72      */

73     protected PredicatedSortedMap(SortedMap JavaDoc map, Predicate keyPredicate, Predicate valuePredicate) {
74         super(map, keyPredicate, valuePredicate);
75     }
76
77     //-----------------------------------------------------------------------
78
/**
79      * Gets the map being decorated.
80      *
81      * @return the decorated map
82      */

83     protected SortedMap JavaDoc getSortedMap() {
84         return (SortedMap JavaDoc) map;
85     }
86
87     //-----------------------------------------------------------------------
88
public Object JavaDoc firstKey() {
89         return getSortedMap().firstKey();
90     }
91
92     public Object JavaDoc lastKey() {
93         return getSortedMap().lastKey();
94     }
95
96     public Comparator JavaDoc comparator() {
97         return getSortedMap().comparator();
98     }
99
100     public SortedMap JavaDoc subMap(Object JavaDoc fromKey, Object JavaDoc toKey) {
101         SortedMap JavaDoc map = getSortedMap().subMap(fromKey, toKey);
102         return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
103     }
104
105     public SortedMap JavaDoc headMap(Object JavaDoc toKey) {
106         SortedMap JavaDoc map = getSortedMap().headMap(toKey);
107         return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
108     }
109
110     public SortedMap JavaDoc tailMap(Object JavaDoc fromKey) {
111         SortedMap JavaDoc map = getSortedMap().tailMap(fromKey);
112         return new PredicatedSortedMap(map, keyPredicate, valuePredicate);
113     }
114
115 }
116
Popular Tags