001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.flatfile.morph;
018
019import net.sf.morph.reflect.BeanReflector;
020import net.sf.morph.reflect.MutableIndexedContainerReflector;
021import net.sf.morph.reflect.SizableReflector;
022
023import org.apache.commons.flatfile.Entity;
024import org.apache.commons.flatfile.EntityArray;
025import org.apache.commons.flatfile.IndexedEntityCollection;
026
027/**
028 * IndexedEntityCollection Reflector.
029 * @version $Revision: 1301237 $ $Date: 2012-03-15 17:08:23 -0500 (Thu, 15 Mar 2012) $
030 */
031public class IndexedEntityCollectionReflector extends
032        BaseEntityCollectionReflector implements
033        MutableIndexedContainerReflector, SizableReflector, BeanReflector {
034    /**
035     * Prototype property constant.
036     */
037    public static final String IMPLICIT_PROPERTY_PROTOTYPE = "prototype";
038
039    /**
040     * {@inheritDoc}
041     */
042    @Override
043    protected Entity getEntity(Object container, int index) throws Exception {
044        return ((IndexedEntityCollection) container).getChild(index);
045    }
046
047    /**
048     * {@inheritDoc}
049     */
050    @Override
051    protected Class<?>[] getReflectableClassesImpl() throws Exception {
052        return new Class[] { IndexedEntityCollection.class };
053    }
054
055    /**
056     * {@inheritDoc}
057     */
058    @Override
059    protected boolean isEntity(Object bean, String propertyName)
060            throws Exception {
061        if (bean instanceof EntityArray
062                && IMPLICIT_PROPERTY_PROTOTYPE.equals(propertyName)) {
063            return true;
064        }
065        try {
066            return Integer.parseInt(propertyName) < getSize(bean);
067        } catch (Exception e) {
068            return false;
069        }
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    protected Object getImpl(Object bean, String propertyName) throws Exception {
077        return super.getImpl(bean, propertyName);
078    }
079
080    /**
081     * {@inheritDoc}
082     */
083    @Override
084    protected Entity getEntity(Object bean, String propertyName)
085            throws Exception {
086        if (bean instanceof EntityArray
087                && IMPLICIT_PROPERTY_PROTOTYPE.equals(propertyName)) {
088            return ((EntityArray) bean).getPrototype();
089        }
090        return super.getEntity(bean, propertyName);
091    }
092
093    /**
094     * {@inheritDoc}
095     */
096    @Override
097    protected int getSizeImpl(Object o) throws Exception {
098        int size = ((IndexedEntityCollection) o).size();
099        return size < 0 ? 0 : size;
100    }
101}