001    /*
002     *  Copyright 2001-2011 Stephen Colebourne
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.joda.beans.impl.map;
017    
018    import java.lang.annotation.Annotation;
019    import java.util.Collections;
020    import java.util.List;
021    
022    import org.joda.beans.Bean;
023    import org.joda.beans.MetaBean;
024    import org.joda.beans.Property;
025    import org.joda.beans.PropertyReadWrite;
026    import org.joda.beans.impl.BasicMetaProperty;
027    import org.joda.beans.impl.BasicProperty;
028    
029    /**
030     * A meta-property using a {@code MapBean} for storage.
031     * 
032     * @author Stephen Colebourne
033     */
034    public class MapBeanMetaProperty extends BasicMetaProperty<Object> {
035    
036        /** The bean. */
037        private MapBean bean;
038    
039        /**
040         * Factory to create a meta-property.
041         * 
042         * @param mapBean  the {@code MapBean}, not null
043         * @param propertyName  the property name, not empty
044         */
045        public static MapBeanMetaProperty of(MapBean mapBean, String propertyName) {
046            return new MapBeanMetaProperty(mapBean, propertyName);
047        }
048    
049        /**
050         * Constructor.
051         * 
052         * @param mapBean  the {@code MapBean}, not null
053         * @param propertyName  the property name, not empty
054         */
055        private MapBeanMetaProperty(MapBean mapBean, String propertyName) {
056            super(propertyName);
057            this.bean = mapBean;
058        }
059    
060        //-----------------------------------------------------------------------
061        @Override
062        public Property<Object> createProperty(Bean bean) {
063            return BasicProperty.of(bean, this);
064        }
065    
066        @Override
067        public MetaBean metaBean() {
068            return bean;
069        }
070    
071        @Override
072        public Class<Object> propertyType() {
073            return Object.class;
074        }
075    
076        @Override
077        public Class<Object> propertyGenericType() {
078            return Object.class;
079        }
080    
081        @Override
082        public PropertyReadWrite readWrite() {
083            return PropertyReadWrite.READ_WRITE;
084        }
085    
086        @Override
087        public List<Annotation> annotations() {
088            return Collections.emptyList();
089        }
090    
091        //-----------------------------------------------------------------------
092        @Override
093        public Object get(Bean bean) {
094            if (bean != this.bean) {
095                throw new ClassCastException("Bean is a MapBean, but not the correct MapBean");
096            }
097            return this.bean.get(name());
098        }
099    
100        @Override
101        public void set(Bean bean, Object value) {
102            if (bean != this.bean) {
103                throw new ClassCastException("Bean is a MapBean, but not the correct MapBean");
104            }
105            this.bean.put(name(), value);
106        }
107    
108    }