| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
package org.apache.commons.flatfile.morph; |
| 18 | |
|
| 19 | |
import java.util.Arrays; |
| 20 | |
import java.util.HashSet; |
| 21 | |
import java.util.Locale; |
| 22 | |
|
| 23 | |
import net.sf.morph.Defaults; |
| 24 | |
import net.sf.morph.transform.DecoratedConverter; |
| 25 | |
import net.sf.morph.transform.transformers.BaseTransformer; |
| 26 | |
import net.sf.morph.util.ContainerUtils; |
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | 2 | public class TextToByteConverter extends BaseTransformer implements |
| 33 | |
DecoratedConverter { |
| 34 | |
private DecoratedConverter textConverter; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
@SuppressWarnings("unchecked") |
| 40 | |
protected Object convertImpl(Class destinationClass, Object source, |
| 41 | |
Locale locale) throws Exception { |
| 42 | 19 | if (isByte(destinationClass)) { |
| 43 | 18 | byte[] b = (byte[]) getTextConverter().convert(byte[].class, |
| 44 | |
source, locale); |
| 45 | 18 | return b == null || b.length == 0 ? null : new Byte(b[0]); |
| 46 | |
} |
| 47 | 1 | byte[] b = source == null ? null : new byte[] { ((Byte) source) |
| 48 | |
.byteValue() }; |
| 49 | 1 | return getTextConverter().convert(destinationClass, b, locale); |
| 50 | |
} |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
protected Class<?>[] getDestinationClassesImpl() throws Exception { |
| 56 | 2 | HashSet<Class<?>> s = new HashSet<Class<?>>(); |
| 57 | 2 | s.addAll(Arrays.asList((Class<?>[]) getTextConverter().getDestinationClasses())); |
| 58 | 2 | s.add(byte.class); |
| 59 | 2 | s.add(Byte.class); |
| 60 | 2 | return s.toArray(new Class[s.size()]); |
| 61 | |
} |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
protected Class<?>[] getSourceClassesImpl() throws Exception { |
| 67 | 2 | HashSet<Class<?>> s = new HashSet<Class<?>>(); |
| 68 | 2 | s.addAll(Arrays.asList((Class<?>[]) getTextConverter().getSourceClasses())); |
| 69 | 2 | s.add(byte.class); |
| 70 | 2 | s.add(Byte.class); |
| 71 | 2 | return s.toArray(new Class[s.size()]); |
| 72 | |
} |
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
@SuppressWarnings("unchecked") |
| 78 | |
protected boolean isTransformableImpl(Class destinationType, |
| 79 | |
Class sourceType) throws Exception { |
| 80 | 5 | DecoratedConverter cnv = getTextConverter(); |
| 81 | 5 | return (isByte(sourceType) && ContainerUtils.contains(cnv |
| 82 | |
.getDestinationClasses(), destinationType)) |
| 83 | |
|| (isByte(destinationType) && ContainerUtils.contains(cnv |
| 84 | |
.getSourceClasses(), sourceType)); |
| 85 | |
} |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
private boolean isByte(Class<?> c) { |
| 93 | 29 | return c == byte.class || c == Byte.class; |
| 94 | |
} |
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
|
| 100 | |
public synchronized DecoratedConverter getTextConverter() { |
| 101 | 28 | if (textConverter == null) { |
| 102 | 2 | setTextConverter(Defaults.createTextConverter()); |
| 103 | |
} |
| 104 | 28 | return textConverter; |
| 105 | |
} |
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
public synchronized void setTextConverter(DecoratedConverter textConverter) { |
| 112 | 2 | this.textConverter = textConverter; |
| 113 | 2 | } |
| 114 | |
|
| 115 | |
} |