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 java.util.Locale;
020
021import org.apache.commons.flatfile.FieldOption;
022
023import net.sf.morph.Defaults;
024import net.sf.morph.transform.DecoratedConverter;
025import net.sf.morph.transform.transformers.BaseTransformer;
026
027/**
028 * Convert text to FieldOption instances from class constants.
029 * @version $Revision: 1301237 $ $Date: 2012-03-15 17:08:23 -0500 (Thu, 15 Mar 2012) $
030 */
031@SuppressWarnings("unchecked")
032public class FieldOptionConstantConverter extends BaseTransformer implements
033        DecoratedConverter {
034    private DecoratedConverter textConverter;
035
036    /**
037     * {@inheritDoc}
038     */
039    @SuppressWarnings("rawtypes")
040    @Override
041    protected Class[] getDestinationClassesImpl() throws Exception {
042        return new Class[] { FieldOption.class };
043    }
044
045    /**
046     * {@inheritDoc}
047     */
048    @SuppressWarnings("rawtypes")
049    @Override
050    protected Class[] getSourceClassesImpl() throws Exception {
051        return getTextConverter().getSourceClasses();
052    }
053
054    /**
055     * {@inheritDoc}
056     */
057    @Override
058    protected Object convertImpl(@SuppressWarnings("rawtypes") Class destinationClass, Object source,
059            Locale locale) throws Exception {
060        String constant = (String) getTextConverter().convert(String.class,
061                source, locale);
062        if (destinationClass.isEnum()) {
063            return Enum.valueOf(destinationClass, constant);
064        }
065        // return static field
066        return destinationClass.getField(constant).get(null);
067    }
068
069    /**
070     * Get the DecoratedConverter textConverter.
071     * @return DecoratedConverter
072     */
073    public synchronized DecoratedConverter getTextConverter() {
074        if (textConverter == null) {
075            setTextConverter(Defaults.createTextConverter());
076        }
077        return textConverter;
078    }
079
080    /**
081     * Set the DecoratedConverter textConverter.
082     * @param textConverter DecoratedConverter
083     */
084    public synchronized void setTextConverter(DecoratedConverter textConverter) {
085        this.textConverter = textConverter;
086    }
087
088}