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 org.apache.commons.flatfile.Entity;
020import org.apache.commons.flatfile.EntityFactory;
021import org.apache.commons.flatfile.IndexedEntityCollection;
022
023import net.sf.morph.reflect.InstantiatingReflector;
024import net.sf.morph.reflect.Reflector;
025import net.sf.morph.reflect.reflectors.BaseReflector;
026import net.sf.morph.reflect.reflectors.SimpleDelegatingReflector;
027
028/**
029 * Instantiates basic entities by sending the source object as a cue object
030 * to a configured EntityFactory.
031 * @version $Revision: 1301237 $ $Date: 2012-03-15 17:08:23 -0500 (Thu, 15 Mar 2012) $
032 */
033public class EntityInstantiatingReflector extends BaseReflector implements
034        InstantiatingReflector {
035    // use a SimpleDelegatingReflector prepended with an
036    // EntityCollectionReflector to get the
037    // size and contained type of source objects when implementing
038    // InstantiatingReflector
039    private static final SimpleDelegatingReflector CONTAINER_REFLECTOR = new SimpleDelegatingReflector(
040            new Reflector[] { new EntityCollectionReflector() }, true);
041
042    private EntityFactory entityFactory;
043
044    /**
045     * Create a new EntityInstantiatingReflector.
046     */
047    public EntityInstantiatingReflector() {
048        super();
049    }
050
051    /**
052     * Create a new EntityInstantiatingReflector.
053     * @param entityFactory to use
054     */
055    public EntityInstantiatingReflector(EntityFactory entityFactory) {
056        setEntityFactory(entityFactory);
057    }
058
059    /**
060     * {@inheritDoc}
061     */
062    @Override
063    protected Class<?>[] getReflectableClassesImpl() throws Exception {
064        return new Class[] { Entity.class };
065    }
066
067    /**
068     * Get the entityFactory.
069     * @return EntityFactory.
070     */
071    public EntityFactory getEntityFactory() {
072        return entityFactory;
073    }
074
075    /**
076     * Set the entityFactory.
077     * @param entityFactory The EntityFactory entityFactory to set.
078     */
079    public void setEntityFactory(EntityFactory entityFactory) {
080        this.entityFactory = entityFactory;
081    }
082
083    /**
084     * {@inheritDoc}
085     */
086    @Override
087    protected Object newInstanceImpl(@SuppressWarnings("rawtypes") Class destType, Object source)
088            throws Exception {
089        Entity e = getEntityFactory().getEntity(source);
090        if (e instanceof IndexedEntityCollection) {
091            IndexedEntityCollection c = (IndexedEntityCollection) e;
092            if (c.isSizable()) {
093                c.setSize(CONTAINER_REFLECTOR.getSize(source));
094            }
095        }
096        return e;
097    }
098
099}