View Javadoc

1   /*
2    * Copyright 2002-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.clazz.bean;
17  
18  import java.util.HashMap;
19  import java.util.Iterator;
20  import java.util.Map;
21  
22  import org.apache.commons.clazz.Clazz;
23  
24  /***
25   * A trivial implementation of the Bean interface based on a HashMap.
26   *
27   * @author Dmitri Plotnikov
28   * @version $Revision: 1.5 $ $Date: 2004/02/19 23:58:38 $
29   */
30  public class BasicBean implements Bean {
31  
32      private Clazz clazz;
33      private Map properties = new HashMap();
34      
35      /***
36       * Constructor for BasicBean.
37       */
38      public BasicBean(Clazz clazz) {
39          this.clazz = clazz;
40      }
41  
42      /***
43       * Constructor that initializes properties of the bean.
44       */
45      public BasicBean(Clazz clazz, Map initialPropertyValues) {
46          this(clazz);
47          for (Iterator iter = initialPropertyValues.entrySet().iterator();
48              iter.hasNext();
49              ) {
50              Map.Entry element = (Map.Entry) iter.next();
51              set((String) element.getKey(), element.getValue());
52          }
53      }
54  
55  
56      /***
57       * @see org.apache.commons.clazz.bean.Bean#getClazz()
58       */
59      public Clazz getClazz() {
60          return clazz;
61      }
62  
63      /***
64       * @see org.apache.commons.clazz.bean.Bean#get(java.lang.String)
65       */
66      public Object get(String propertyName) {
67          // @todo validate property name using the clazz
68          // @todo possible provide a default value based on the clazz
69          return properties.get(propertyName);
70      }
71  
72      /***
73       * @see Bean#set(String, Object)
74       */
75      public void set(String propertyName, Object value) {
76          // @todo validate property name and value type using the clazz
77          properties.put(propertyName, value);       
78      }
79  }