KickJava   Java API By Example, From Geeks To Geeks.

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


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

46 public class PredicatedMap
47         extends AbstractInputCheckedMapDecorator
48         implements Serializable JavaDoc {
49
50     /** Serialization version */
51     private static final long serialVersionUID = 7412622456128415156L;
52
53     /** The key predicate to use */
54     protected final Predicate keyPredicate;
55     /** The value predicate to use */
56     protected final Predicate valuePredicate;
57
58     /**
59      * Factory method to create a predicated (validating) map.
60      * <p>
61      * If there are any elements already in the list being decorated, they
62      * are validated.
63      *
64      * @param map the map to decorate, must not be null
65      * @param keyPredicate the predicate to validate the keys, null means no check
66      * @param valuePredicate the predicate to validate to values, null means no check
67      * @throws IllegalArgumentException if the map is null
68      */

69     public static Map JavaDoc decorate(Map JavaDoc map, Predicate keyPredicate, Predicate valuePredicate) {
70         return new PredicatedMap(map, keyPredicate, valuePredicate);
71     }
72
73     //-----------------------------------------------------------------------
74
/**
75      * Constructor that wraps (not copies).
76      *
77      * @param map the map to decorate, must not be null
78      * @param keyPredicate the predicate to validate the keys, null means no check
79      * @param valuePredicate the predicate to validate to values, null means no check
80      * @throws IllegalArgumentException if the map is null
81      */

82     protected PredicatedMap(Map JavaDoc map, Predicate keyPredicate, Predicate valuePredicate) {
83         super(map);
84         this.keyPredicate = keyPredicate;
85         this.valuePredicate = valuePredicate;
86         
87         Iterator JavaDoc it = map.entrySet().iterator();
88         while (it.hasNext()) {
89             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
90             Object JavaDoc key = entry.getKey();
91             Object JavaDoc value = entry.getValue();
92             validate(key, value);
93         }
94     }
95
96     //-----------------------------------------------------------------------
97
/**
98      * Write the map out using a custom routine.
99      *
100      * @param out the output stream
101      * @throws IOException
102      * @since Commons Collections 3.1
103      */

104     private void writeObject(ObjectOutputStream JavaDoc out) throws IOException JavaDoc {
105         out.defaultWriteObject();
106         out.writeObject(map);
107     }
108
109     /**
110      * Read the map in using a custom routine.
111      *
112      * @param in the input stream
113      * @throws IOException
114      * @throws ClassNotFoundException
115      * @since Commons Collections 3.1
116      */

117     private void readObject(ObjectInputStream JavaDoc in) throws IOException JavaDoc, ClassNotFoundException JavaDoc {
118         in.defaultReadObject();
119         map = (Map JavaDoc) in.readObject();
120     }
121
122     //-----------------------------------------------------------------------
123
/**
124      * Validates a key value pair.
125      *
126      * @param key the key to validate
127      * @param value the value to validate
128      * @throws IllegalArgumentException if invalid
129      */

130     protected void validate(Object JavaDoc key, Object JavaDoc value) {
131         if (keyPredicate != null && keyPredicate.evaluate(key) == false) {
132             throw new IllegalArgumentException JavaDoc("Cannot add key - Predicate rejected it");
133         }
134         if (valuePredicate != null && valuePredicate.evaluate(value) == false) {
135             throw new IllegalArgumentException JavaDoc("Cannot add value - Predicate rejected it");
136         }
137     }
138
139     /**
140      * Override to validate an object set into the map via <code>setValue</code>.
141      *
142      * @param value the value to validate
143      * @throws IllegalArgumentException if invalid
144      * @since Commons Collections 3.1
145      */

146     protected Object JavaDoc checkSetValue(Object JavaDoc value) {
147         if (valuePredicate.evaluate(value) == false) {
148             throw new IllegalArgumentException JavaDoc("Cannot set value - Predicate rejected it");
149         }
150         return value;
151     }
152
153     /**
154      * Override to only return true when there is a value transformer.
155      *
156      * @return true if a value predicate is in use
157      * @since Commons Collections 3.1
158      */

159     protected boolean isSetValueChecking() {
160         return (valuePredicate != null);
161     }
162
163     //-----------------------------------------------------------------------
164
public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
165         validate(key, value);
166         return map.put(key, value);
167     }
168
169     public void putAll(Map JavaDoc mapToCopy) {
170         Iterator JavaDoc it = mapToCopy.entrySet().iterator();
171         while (it.hasNext()) {
172             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
173             Object JavaDoc key = entry.getKey();
174             Object JavaDoc value = entry.getValue();
175             validate(key, value);
176         }
177         map.putAll(mapToCopy);
178     }
179
180 }
181
Popular Tags