@SuppressWarnings({ "unchecked", "rawtypes" }) public PartScript(String prefixPattern, String ... propertyNames) { this.prefixFormat = (prefixPattern != null ? new MessageFormat(prefixPattern) : null); this.propertyNames = propertyNames; int length = propertyNames.length; Converter[] propertyConverters = new Converter[length]; for (int i = 0; i < length; i++) propertyConverters[i] = new ToStringConverter(); this.converter = new ConverterChain( new BeanToPropertyArrayConverter(propertyNames), new ArrayConverter(Object.class, String.class, propertyConverters) ); this.elementCount = 0; this.buffer = new StringBuffer(); }
/** * If there are no converters, it only transforms the array type; * if there is a single converter, all elements are converted with the same converter; * if there are several converters, the number of converters and array elements are * assumed to be equal and each element is converted with the converter of the same index. */ @Override public T[] convert(S[] sourceValues) throws ConversionException { if (sourceValues == null) return null; if (components.length == 0) return convertWith(null, targetComponentType, sourceValues); else if (components.length == 1) return convertWith(components[0], targetComponentType, sourceValues); else { if (sourceValues.length != components.length) throw new IllegalArgumentException("Array has a different size than the converter list"); T[] result = ArrayUtil.newInstance(targetComponentType, components.length); for (int i = 0; i < components.length; i++) result[i] = components[i].convert(sourceValues[i]); return result; } }
@SuppressWarnings({ "unchecked", "rawtypes" }) public PartScript(String prefixPattern, String ... propertyNames) { this.prefixFormat = (prefixPattern != null ? new MessageFormat(prefixPattern) : null); this.propertyNames = propertyNames; int length = propertyNames.length; Converter[] propertyConverters = new Converter[length]; for (int i = 0; i < length; i++) propertyConverters[i] = new ToStringConverter(); this.converter = new ConverterChain( new BeanToPropertyArrayConverter(propertyNames), new ArrayConverter(Object.class, String.class, propertyConverters) ); this.elementCount = 0; this.buffer = new StringBuffer(); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public BeanCSVScript(String[] propertyNames, char separator) { this.separator = separator; int length = propertyNames.length; Converter[] propertyConverters = new Converter[length]; for (int i = 0; i < length; i++) propertyConverters[i] = new ToStringConverter(); this.converter = new ConverterChain( new BeanToPropertyArrayConverter(propertyNames), new ArrayConverter(Object.class, String.class, propertyConverters) ); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public BeanCSVScript(String[] propertyNames, char separator) { this.separator = separator; int length = propertyNames.length; Converter[] propertyConverters = new Converter[length]; for (int i = 0; i < length; i++) propertyConverters[i] = new ToStringConverter(); this.converter = new ConverterChain( new BeanToFeatureArrayConverter(propertyNames.clone()), new ArrayConverter(Object.class, String.class, propertyConverters) ); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public BeanFixedWidthScript(FixedWidthColumnDescriptor[] descriptors) { int length = descriptors.length; String[] propertyNames = new String[length]; Converter[] propertyConverters = new Converter[length]; for (int i = 0; i < length; i++) { FixedWidthColumnDescriptor descriptor = descriptors[i]; propertyNames[i] = descriptor.getName(); propertyConverters[i] = new ConverterChain( new ToStringConverter(), new FormatFormatConverter(String.class, descriptor.getFormat(), true ) ); } this.converter = new ConverterChain( new BeanToPropertyArrayConverter(propertyNames), new ArrayConverter(Object.class, String.class, propertyConverters) ); }
@SuppressWarnings({ "unchecked", "rawtypes" }) public BeanFixedWidthScript(FixedWidthColumnDescriptor[] descriptors) { int length = descriptors.length; String[] propertyNames = new String[length]; Converter[] propertyConverters = new Converter[length]; for (int i = 0; i < length; i++) { FixedWidthColumnDescriptor descriptor = descriptors[i]; propertyNames[i] = descriptor.getName(); propertyConverters[i] = new ConverterChain( new ToStringConverter(), new FormatFormatConverter(String.class, new PadFormat(descriptor.getWidth(), descriptor.getAlignment(), ' '), true ) ); } this.converter = new ConverterChain( new BeanToPropertyArrayConverter(propertyNames), new ArrayConverter(Object.class, String.class, propertyConverters) ); }