KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > binding > mapping > DefaultAttributeMapper


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 package org.springframework.binding.mapping;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.Arrays JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import org.springframework.core.style.ToStringCreator;
25
26 /**
27  * Generic attributes mapper implementation that allows mappings to be
28  * configured programatically.
29  *
30  * @author Erwin Vervaet
31  * @author Keith Donald
32  * @author Colin Sampaleanu
33  */

34 public class DefaultAttributeMapper implements AttributeMapper, Serializable JavaDoc {
35
36     /**
37      * The ordered list of mappings to apply.
38      */

39     private List JavaDoc mappings = new LinkedList JavaDoc();
40
41     /**
42      * Add a mapping to this mapper.
43      * @param mapping the mapping to add (as an AttributeMapper)
44      * @return this, to support convenient call chaining.
45      */

46     public DefaultAttributeMapper addMapping(AttributeMapper mapping) {
47         mappings.add(mapping);
48         return this;
49     }
50
51     /**
52      * Add a set of mappings.
53      * @param mappings the mappings
54      */

55     public void addMappings(AttributeMapper[] mappings) {
56         if (mappings == null) {
57             return;
58         }
59         this.mappings.addAll(Arrays.asList(mappings));
60     }
61
62     /**
63      * Returns this mapper's list of mappings.
64      * @return the list of mappings
65      */

66     public AttributeMapper[] getMappings() {
67         return (AttributeMapper[])mappings.toArray(new AttributeMapper[mappings.size()]);
68     }
69
70     public void map(Object JavaDoc source, Object JavaDoc target, MappingContext context) {
71         if (mappings != null) {
72             Iterator JavaDoc it = mappings.iterator();
73             while (it.hasNext()) {
74                 AttributeMapper mapping = (AttributeMapper)it.next();
75                 mapping.map(source, target, context);
76             }
77         }
78     }
79
80     public String JavaDoc toString() {
81         return new ToStringCreator(this).append("mappings", mappings).toString();
82     }
83 }
Popular Tags