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.lang3.ArrayUtils;
022
023import net.sf.morph.Defaults;
024import net.sf.morph.transform.DecoratedConverter;
025import net.sf.morph.transform.transformers.BaseTransformer;
026import net.sf.morph.util.ContainerUtils;
027
028/**
029 * Text to byte converter.
030 * @version $Revision: 1301233 $ $Date: 2012-03-15 17:06:02 -0500 (Thu, 15 Mar 2012) $
031 */
032public class TextToByteConverter extends BaseTransformer implements
033        DecoratedConverter {
034    private DecoratedConverter textConverter;
035
036    /**
037     * {@inheritDoc}
038     */
039    @Override
040    protected Object convertImpl(@SuppressWarnings("rawtypes") Class destinationClass, Object source,
041            Locale locale) throws Exception {
042        if (isByte(destinationClass)) {
043            byte[] b = (byte[]) getTextConverter().convert(byte[].class,
044                    source, locale);
045            return b == null || b.length == 0 ? null : new Byte(b[0]);
046        }
047        byte[] b = source == null ? null : new byte[] { ((Byte) source)
048                .byteValue() };
049        return getTextConverter().convert(destinationClass, b, locale);
050    }
051
052    /**
053     * {@inheritDoc}
054     */
055    @Override
056    protected Class<?>[] getDestinationClassesImpl() throws Exception {
057        return ArrayUtils.addAll(getTextConverter().getDestinationClasses(),
058            byte.class, Byte.class);
059    }
060
061    /**
062     * {@inheritDoc}
063     */
064    @Override
065    protected Class<?>[] getSourceClassesImpl() throws Exception {
066        return ArrayUtils.addAll(getTextConverter().getSourceClasses(),
067            byte.class, Byte.class);
068    }
069
070    /**
071     * {@inheritDoc}
072     */
073    @Override
074    protected boolean isTransformableImpl(@SuppressWarnings("rawtypes") Class destinationType,
075            @SuppressWarnings("rawtypes") Class sourceType) throws Exception {
076        DecoratedConverter cnv = getTextConverter();
077        return (isByte(sourceType) && ContainerUtils.contains(
078            cnv.getDestinationClasses(), destinationType))
079            || (isByte(destinationType) && ContainerUtils.contains(
080                cnv.getSourceClasses(), sourceType));
081    }
082
083    /**
084     * Learn whether the given class is Byte or byte.
085     * @param c to check
086     * @return boolean
087     */
088    private boolean isByte(Class<?> c) {
089        return byte.class.equals(c) || Byte.class.equals(c);
090    }
091
092    /**
093     * Get the DecoratedConverter textConverter.
094     * @return DecoratedConverter
095     */
096    public synchronized DecoratedConverter getTextConverter() {
097        if (textConverter == null) {
098            setTextConverter(Defaults.createTextConverter());
099        }
100        return textConverter;
101    }
102
103    /**
104     * Set the DecoratedConverter textConverter.
105     * @param textConverter DecoratedConverter
106     */
107    public synchronized void setTextConverter(DecoratedConverter textConverter) {
108        this.textConverter = textConverter;
109    }
110
111}