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.InstantiatingReflector;
020import net.sf.morph.reflect.Reflector;
021import net.sf.morph.reflect.reflectors.BaseReflector;
022import net.sf.morph.reflect.reflectors.SimpleDelegatingReflector;
023
024import org.apache.commons.flatfile.Entity;
025import org.apache.commons.flatfile.EntityArray;
026import org.apache.commons.flatfile.IndexedEntityCollection;
027
028/**
029 * IndexedEntityCollection InstantiatingReflector.
030 * @version $Revision: 1301237 $ $Date: 2012-03-15 17:08:23 -0500 (Thu, 15 Mar 2012) $
031 */
032public class IndexedEntityCollectionInstantiatingReflector extends
033        BaseReflector implements InstantiatingReflector {
034
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 InstantiatingReflector componentReflector;
043
044    /**
045     * {@inheritDoc}
046     */
047    @Override
048    protected Class<?>[] getReflectableClassesImpl() throws Exception {
049        return new Class[] { IndexedEntityCollection.class };
050    }
051
052    /**
053     * Get the componentReflector.
054     * @return InstantiatingReflector.
055     */
056    public synchronized InstantiatingReflector getComponentReflector() {
057        return componentReflector;
058    }
059
060    /**
061     * Set the componentReflector.  Essential for use of this class as an InstantiatingReflector.
062     * @param componentReflector The InstantiatingReflector componentReflector to set.
063     */
064    public synchronized void setComponentReflector(
065            InstantiatingReflector componentReflector) {
066        this.componentReflector = componentReflector;
067    }
068
069    /**
070     * {@inheritDoc}
071     */
072    @Override
073    protected Object newInstanceImpl(@SuppressWarnings("rawtypes") Class destType, Object source)
074            throws Exception {
075        int size = CONTAINER_REFLECTOR.getSize(source);
076        if (size == 0) {
077            return new EntityArray(0);
078        }
079        Class<?> containedType = source == null ? null : CONTAINER_REFLECTOR
080                .getContainedType(source.getClass());
081        Entity prototype = (Entity) getComponentReflector().newInstance(
082                destType, containedType);
083        return new EntityArray(prototype, size);
084    }
085
086}