KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > ui > ModelMap


1 /*
2  * Copyright 2002-2006 the original author or authors.
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
17 package org.springframework.ui;
18
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.springframework.core.Conventions;
25 import org.springframework.util.Assert;
26
27 /**
28  * Implementation of {@link java.util.Map} for use when building model data for use
29  * with UI tools. Supports chained calls and generation of model attribute names.
30  *
31  * <p>This class serves as generic model holder for both Servlet and Portlet MVC,
32  * but is tied to neither of those.
33  *
34  * @author Rob Harrop
35  * @since 2.0
36  * @see Conventions#getVariableName
37  * @see org.springframework.web.servlet.ModelAndView
38  * @see org.springframework.web.portlet.ModelAndView
39  */

40 public class ModelMap extends HashMap JavaDoc {
41
42     /**
43      * Construct a new, empty <code>ModelMap</code>.
44      */

45     public ModelMap() {
46     }
47
48     /**
49      * Construct a new <code>ModelMap</code> containing the supplied model object
50      * under the supplied name.
51      * @see #addObject(String, Object)
52      */

53     public ModelMap(String JavaDoc modelName, Object JavaDoc modelObject) {
54         addObject(modelName, modelObject);
55     }
56
57     /**
58      * Construct a new <code>ModelMap</code> containing the supplied model object.
59      * Uses attribute name generation to generate the key for the supplied model
60      * object.
61      * @see #addObject(Object)
62      */

63     public ModelMap(Object JavaDoc modelObject) {
64         addObject(modelObject);
65     }
66
67
68     /**
69      * Add the supplied <code>Object</code> under the supplied name.
70      * @param modelName the name of the model attribute (never <code>null</code>)
71      * @param modelObject the model attribute object (can be <code>null</code>)
72      */

73     public ModelMap addObject(String JavaDoc modelName, Object JavaDoc modelObject) {
74         Assert.notNull(modelName, "Model name must not be null");
75         this.put(modelName, modelObject);
76         return this;
77     }
78
79     /**
80      * Add the supplied <code>Object</code> to this <code>Map</code> used a
81      * {@link org.springframework.core.Conventions#getVariableName generated name}.
82      * <p/><emphasis>Note: Empty {@link Collection Collections} are not added to
83      * the model when using this method because we cannot correctly determine
84      * the true convention name. View code should check for <code>null</code> rather than
85      * for empty collections as is already done by JSTL tags</emphasis>.
86      * @param modelObject the model attribute object (never <code>null</code>)
87      */

88     public ModelMap addObject(Object JavaDoc modelObject) {
89         Assert.notNull(modelObject, "Model object must not be null");
90         if (modelObject instanceof Collection JavaDoc && ((Collection JavaDoc) modelObject).isEmpty()) {
91             return this;
92         }
93         return addObject(Conventions.getVariableName(modelObject), modelObject);
94     }
95
96     /**
97      * Copy all objects in the supplied <code>Map</code> into this <code>Map</code>.
98      */

99     public ModelMap addAllObjects(Map JavaDoc objects) {
100         if (objects != null) {
101             this.putAll(objects);
102         }
103         return this;
104     }
105
106     /**
107      * Copy all objects in the supplied <code>Collection</code> into this <code>Map</code>,
108      * using attribute name generation for each element.
109      * @see #addObject(Object)
110      */

111     public ModelMap addAllObjects(Collection JavaDoc objects) {
112         if (objects != null) {
113             for (Iterator JavaDoc it = objects.iterator(); it.hasNext();) {
114                 addObject(it.next());
115             }
116         }
117         return this;
118     }
119
120 }
121
Popular Tags