001    /*
002     * Copyright 2002-2004 The Apache Software Foundation
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *     http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.apache.commons.clazz.bean;
017    
018    import java.util.HashMap;
019    import java.util.Iterator;
020    import java.util.Map;
021    
022    import org.apache.commons.clazz.Clazz;
023    
024    /**
025     * A trivial implementation of the Bean interface based on a HashMap.
026     *
027     * @author Dmitri Plotnikov
028     * @version $Revision: 155436 $ $Date: 2005-02-26 13:17:48 +0000 (Sat, 26 Feb 2005) $
029     */
030    public class BasicBean implements Bean {
031    
032        private Clazz clazz;
033        private Map properties = new HashMap();
034        
035        /**
036         * Constructor for BasicBean.
037         */
038        public BasicBean(Clazz clazz) {
039            this.clazz = clazz;
040        }
041    
042        /**
043         * Constructor that initializes properties of the bean.
044         */
045        public BasicBean(Clazz clazz, Map initialPropertyValues) {
046            this(clazz);
047            for (Iterator iter = initialPropertyValues.entrySet().iterator();
048                iter.hasNext();
049                ) {
050                Map.Entry element = (Map.Entry) iter.next();
051                set((String) element.getKey(), element.getValue());
052            }
053        }
054    
055    
056        /**
057         * @see org.apache.commons.clazz.bean.Bean#getClazz()
058         */
059        public Clazz getClazz() {
060            return clazz;
061        }
062    
063        /**
064         * @see org.apache.commons.clazz.bean.Bean#get(java.lang.String)
065         */
066        public Object get(String propertyName) {
067            // @todo validate property name using the clazz
068            // @todo possible provide a default value based on the clazz
069            return properties.get(propertyName);
070        }
071    
072        /**
073         * @see Bean#set(String, Object)
074         */
075        public void set(String propertyName, Object value) {
076            // @todo validate property name and value type using the clazz
077            properties.put(propertyName, value);       
078        }
079    }