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 */
017 package org.apache.commons.flatfile.morph;
018
019 import java.util.Arrays;
020 import java.util.HashSet;
021 import java.util.Locale;
022
023 import net.sf.morph.Defaults;
024 import net.sf.morph.transform.DecoratedConverter;
025 import net.sf.morph.transform.transformers.BaseTransformer;
026 import net.sf.morph.util.ContainerUtils;
027
028 /**
029 * Text to byte converter.
030 * @version $Revision: 758023 $ $Date: 2009-03-24 16:09:19 -0500 (Tue, 24 Mar 2009) $
031 */
032 public class TextToByteConverter extends BaseTransformer implements
033 DecoratedConverter {
034 private DecoratedConverter textConverter;
035
036 /**
037 * {@inheritDoc}
038 */
039 @SuppressWarnings("unchecked")
040 protected Object convertImpl(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 protected Class<?>[] getDestinationClassesImpl() throws Exception {
056 HashSet<Class<?>> s = new HashSet<Class<?>>();
057 s.addAll(Arrays.asList((Class<?>[]) getTextConverter().getDestinationClasses()));
058 s.add(byte.class);
059 s.add(Byte.class);
060 return s.toArray(new Class[s.size()]);
061 }
062
063 /**
064 * {@inheritDoc}
065 */
066 protected Class<?>[] getSourceClassesImpl() throws Exception {
067 HashSet<Class<?>> s = new HashSet<Class<?>>();
068 s.addAll(Arrays.asList((Class<?>[]) getTextConverter().getSourceClasses()));
069 s.add(byte.class);
070 s.add(Byte.class);
071 return s.toArray(new Class[s.size()]);
072 }
073
074 /**
075 * {@inheritDoc}
076 */
077 @SuppressWarnings("unchecked")
078 protected boolean isTransformableImpl(Class destinationType,
079 Class sourceType) throws Exception {
080 DecoratedConverter cnv = getTextConverter();
081 return (isByte(sourceType) && ContainerUtils.contains(cnv
082 .getDestinationClasses(), destinationType))
083 || (isByte(destinationType) && ContainerUtils.contains(cnv
084 .getSourceClasses(), sourceType));
085 }
086
087 /**
088 * Learn whether the given class is Byte or byte.
089 * @param c to check
090 * @return boolean
091 */
092 private boolean isByte(Class<?> c) {
093 return c == byte.class || c == Byte.class;
094 }
095
096 /**
097 * Get the DecoratedConverter textConverter.
098 * @return DecoratedConverter
099 */
100 public synchronized DecoratedConverter getTextConverter() {
101 if (textConverter == null) {
102 setTextConverter(Defaults.createTextConverter());
103 }
104 return textConverter;
105 }
106
107 /**
108 * Set the DecoratedConverter textConverter.
109 * @param textConverter DecoratedConverter
110 */
111 public synchronized void setTextConverter(DecoratedConverter textConverter) {
112 this.textConverter = textConverter;
113 }
114
115 }