@Override public BeanField<T> findField(int col) { // If we have a mapping for changing the order of the columns on // writing, be sure to use it. if (columnIndexForWriting != null) { return col < columnIndexForWriting.length ? fieldMap.get(columnIndexForWriting[col]) : null; } return fieldMap.get(col); }
/** * Sets the {@link java.util.Comparator} to be used to sort columns when * writing beans to a CSV file. * Behavior of this method when used on a mapping strategy intended for * reading data from a CSV source is not defined. * * @param writeOrder The {@link java.util.Comparator} to use. May be * {@code null}, in which case the natural ordering is used. * @since 4.3 */ public void setColumnOrderOnWrite(Comparator<Integer> writeOrder) { this.writeOrder = writeOrder; if (fieldMap != null) { fieldMap.setColumnOrderOnWrite(this.writeOrder); } } }
@Override protected void loadFieldMap() throws CsvBadConverterException { boolean required; fieldMap = new FieldMapByPosition<>(errorLocale); fieldMap.setColumnOrderOnWrite(writeOrder); required = annotation.required(); bean.setRequired(required); fieldMap.put(annotation.position(), bean); fieldMap.put(annotation.position(), new BeanFieldSplit<T>( field, required, errorLocale, converter, splitOn, writeDelimiter, collectionType, capture, format)); fieldMap.putComplex(annotation.position(), new BeanFieldJoinIntegerIndex<T>( field, required, errorLocale, converter, mapType, capture, format)); CsvConverter converter = determineConverter(field, field.getType(), fieldLocale, null); fieldMap.put(annotation.position(), new BeanFieldSingleValue<T>( field, required, errorLocale, converter, capture, format));
/** * There is no header per se for this mapping strategy, but this method * checks the first line to determine how many fields are present and * adjusts its field map accordingly. */ // The rest of the Javadoc is inherited @Override public void captureHeader(CSVReader reader) throws IOException { // Validation if (type == null) { throw new IllegalStateException(ResourceBundle .getBundle(ICSVParser.DEFAULT_BUNDLE_NAME, errorLocale) .getString("type.unset")); } String[] firstLine = reader.peek(); fieldMap.setMaxIndex(firstLine.length - 1); if (!columnsExplicitlySet) { headerIndex.clear(); for (FieldMapByPositionEntry entry : fieldMap) { Field f = entry.getField().getField(); if (f.getAnnotation(CsvCustomBindByPosition.class) != null || f.getAnnotation(CsvBindAndSplitByPosition.class) != null || f.getAnnotation(CsvBindAndJoinByPosition.class) != null || f.getAnnotation(CsvBindByPosition.class) != null) { headerIndex.put(entry.getPosition(), f.getName().toUpperCase().trim()); } } } }