Search in sources :

Example 1 with CsvColumnKey

use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.

the class CsvMapperCustomReaderTest method testCustomCsvReaderValueFactory.

@Test
public void testCustomCsvReaderValueFactory() throws IOException {
    CsvMapper<Tuple2<String, String>> csvMapper = CsvMapperFactory.newInstance().addColumnDefinition(new Predicate<CsvColumnKey>() {

        @Override
        public boolean test(CsvColumnKey csvColumnKey) {
            return true;
        }
    }, CsvColumnDefinition.customCellValueReaderFactoryDefinition(new CellValueReaderFactory() {

        @SuppressWarnings("unchecked")
        @Override
        public <P> CellValueReader<P> getReader(Type propertyType, final int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder builder) {
            return (CellValueReader<P>) new CellValueReader<String>() {

                @Override
                public String read(char[] chars, int offset, int length, ParsingContext parsingContext) {
                    return "g" + index;
                }
            };
        }
    })).newMapper(Tuples.typeDef(String.class, String.class));
    Tuple2<String, String> value = csvMapper.iterator(new StringReader("b0,b1\nc0,c1")).next();
    assertEquals("g0", value.first());
    assertEquals("g1", value.second());
}
Also used : ParsingContext(org.simpleflatmapper.csv.ParsingContext) ParsingContextFactoryBuilder(org.simpleflatmapper.csv.ParsingContextFactoryBuilder) CellValueReader(org.simpleflatmapper.csv.CellValueReader) Predicate(org.simpleflatmapper.util.Predicate) Type(java.lang.reflect.Type) Tuple2(org.simpleflatmapper.tuple.Tuple2) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) CellValueReaderFactory(org.simpleflatmapper.csv.CellValueReaderFactory) StringReader(java.io.StringReader) CsvColumnDefinition(org.simpleflatmapper.csv.CsvColumnDefinition) Test(org.junit.Test)

Example 2 with CsvColumnKey

use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.

the class FieldMapperToAppendableFactory method newFieldMapper.

@Override
@SuppressWarnings("unchecked")
public <S, P> FieldMapper<S, Appendable> newFieldMapper(PropertyMapping<S, P, CsvColumnKey, FieldMapperColumnDefinition<CsvColumnKey>> pm, MappingContextFactoryBuilder builder, MapperBuilderErrorHandler mappingErrorHandler) {
    if (pm == null)
        throw new NullPointerException("pm is null");
    Getter<? super S, ? extends P> getter;
    Getter<?, ?> customGetter = pm.getColumnDefinition().getCustomGetterFrom(pm.getPropertyMeta().getOwnerType());
    if (customGetter != null) {
        getter = (Getter<S, P>) customGetter;
    } else {
        getter = pm.getPropertyMeta().getGetter();
    }
    ColumnDefinition<CsvColumnKey, ?> columnDefinition = pm.getColumnDefinition();
    Type type = pm.getPropertyMeta().getPropertyType();
    if (TypeHelper.isPrimitive(type) && !columnDefinition.has(FormatProperty.class)) {
        if (getter instanceof BooleanGetter) {
            return new BooleanFieldMapper<S, Appendable>((BooleanGetter) getter, new BooleanAppendableSetter(cellWriter));
        } else if (getter instanceof ByteGetter) {
            return new ByteFieldMapper<S, Appendable>((ByteGetter) getter, new ByteAppendableSetter(cellWriter));
        } else if (getter instanceof CharacterGetter) {
            return new CharacterFieldMapper<S, Appendable>((CharacterGetter) getter, new CharacterAppendableSetter(cellWriter));
        } else if (getter instanceof ShortGetter) {
            return new ShortFieldMapper<S, Appendable>((ShortGetter) getter, new ShortAppendableSetter(cellWriter));
        } else if (getter instanceof IntGetter) {
            return new IntFieldMapper<S, Appendable>((IntGetter) getter, new IntegerAppendableSetter(cellWriter));
        } else if (getter instanceof LongGetter) {
            return new LongFieldMapper<S, Appendable>((LongGetter) getter, new LongAppendableSetter(cellWriter));
        } else if (getter instanceof FloatGetter) {
            return new FloatFieldMapper<S, Appendable>((FloatGetter) getter, new FloatAppendableSetter(cellWriter));
        } else if (getter instanceof DoubleGetter) {
            return new DoubleFieldMapper<S, Appendable>((DoubleGetter) getter, new DoubleAppendableSetter(cellWriter));
        }
    }
    Setter<Appendable, ? super P> setter = null;
    if (TypeHelper.isEnum(type) && columnDefinition.has(EnumOrdinalFormatProperty.class)) {
        setter = (Setter) new EnumOrdinalAppendableSetter(cellWriter);
    }
    Format format = null;
    if (columnDefinition.has(FormatProperty.class)) {
        format = columnDefinition.lookFor(FormatProperty.class).format();
    } else if (TypeHelper.areEquals(type, Date.class)) {
        String df = DEFAULT_DATE_FORMAT;
        DateFormatProperty dfp = columnDefinition.lookFor(DateFormatProperty.class);
        if (dfp != null) {
            df = dfp.get();
        }
        format = new SimpleDateFormat(df);
    }
    if (format != null) {
        final Format f = format;
        builder.addSupplier(pm.getColumnKey().getIndex(), new CloneFormatSupplier(f));
        return new FormatingAppender<S>(getter, new MappingContextFormatGetter<S>(pm.getColumnKey().getIndex()), cellWriter);
    }
    if (setter == null) {
        setter = getSetter(pm, cellWriter);
    }
    if (setter == null) {
        Converter<? super P, ? extends CharSequence> converter = converterService.findConverter(pm.getPropertyMeta().getPropertyType(), CharSequence.class, columnDefinition != null ? columnDefinition.properties() : new Object[0]);
        if (converter != null) {
            return new ConvertingAppender<S, P>(getter, converter, cellWriter);
        }
    }
    return new FieldMapperImpl<S, Appendable, P>(getter, setter);
}
Also used : ByteGetter(org.simpleflatmapper.reflect.primitive.ByteGetter) DoubleGetter(org.simpleflatmapper.reflect.primitive.DoubleGetter) LongGetter(org.simpleflatmapper.reflect.primitive.LongGetter) FieldMapperImpl(org.simpleflatmapper.map.fieldmapper.FieldMapperImpl) BooleanGetter(org.simpleflatmapper.reflect.primitive.BooleanGetter) Format(java.text.Format) SimpleDateFormat(java.text.SimpleDateFormat) CharacterGetter(org.simpleflatmapper.reflect.primitive.CharacterGetter) IntFieldMapper(org.simpleflatmapper.map.fieldmapper.IntFieldMapper) CharacterFieldMapper(org.simpleflatmapper.map.fieldmapper.CharacterFieldMapper) FloatGetter(org.simpleflatmapper.reflect.primitive.FloatGetter) BooleanFieldMapper(org.simpleflatmapper.map.fieldmapper.BooleanFieldMapper) ShortGetter(org.simpleflatmapper.reflect.primitive.ShortGetter) EnumOrdinalFormatProperty(org.simpleflatmapper.map.property.EnumOrdinalFormatProperty) Date(java.util.Date) DateFormatProperty(org.simpleflatmapper.map.property.DateFormatProperty) Type(java.lang.reflect.Type) FloatFieldMapper(org.simpleflatmapper.map.fieldmapper.FloatFieldMapper) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) SimpleDateFormat(java.text.SimpleDateFormat) IntGetter(org.simpleflatmapper.reflect.primitive.IntGetter)

Example 3 with CsvColumnKey

use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.

the class CsvColumnKeyTest method testGetParent.

@Test
public void testGetParent() throws Exception {
    assertNull(new CsvColumnKey("c", 0).getParent());
    assertNotNull(new CsvColumnKey("c", 0).alias("b").getParent());
}
Also used : CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) Test(org.junit.Test)

Example 4 with CsvColumnKey

use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.

the class CsvColumnKeyTest method testEquals.

@Test
public void testEquals() throws Exception {
    assertEquals(new CsvColumnKey("col", 2), new CsvColumnKey("col", 2));
    assertEquals(new CsvColumnKey("col", 2).alias("h"), new CsvColumnKey("col", 2).alias("h"));
    // parent not int equals
    assertEquals(new CsvColumnKey("col2", 2).alias("h"), new CsvColumnKey("col3", 2).alias("h"));
    assertNotEquals(new CsvColumnKey("col", 2), new CsvColumnKey("col1", 2));
    assertNotEquals(new CsvColumnKey("col", 2), new CsvColumnKey("col", 4));
}
Also used : CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) Test(org.junit.Test)

Example 5 with CsvColumnKey

use of org.simpleflatmapper.csv.CsvColumnKey in project SimpleFlatMapper by arnaudroger.

the class DynamicSheetMapper method getPoiMapper.

// IFJAVA8_END
private SheetMapper<T> getPoiMapper(int startRow, Sheet sheet) {
    Row row = sheet.getRow(startRow);
    List<CsvColumnKey> keys = new ArrayList<CsvColumnKey>(row.getLastCellNum() - row.getFirstCellNum());
    for (short i = row.getFirstCellNum(); i <= row.getLastCellNum(); i++) {
        Cell cell = row.getCell(i);
        if (cell != null && cell.getCellType() != Cell.CELL_TYPE_BLANK) {
            keys.add(new CsvColumnKey(cell.getStringCellValue(), i));
        }
    }
    return getPoiMapper(new MapperKey<CsvColumnKey>(keys.toArray(new CsvColumnKey[0])));
}
Also used : CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) ArrayList(java.util.ArrayList) Row(org.apache.poi.ss.usermodel.Row) Cell(org.apache.poi.ss.usermodel.Cell)

Aggregations

CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)16 Test (org.junit.Test)10 DateFormatProperty (org.simpleflatmapper.map.property.DateFormatProperty)4 Type (java.lang.reflect.Type)3 ParsingContext (org.simpleflatmapper.csv.ParsingContext)3 StringReader (java.io.StringReader)2 CellValueReader (org.simpleflatmapper.csv.CellValueReader)2 CellValueReaderFactory (org.simpleflatmapper.csv.CellValueReaderFactory)2 CsvColumnDefinition (org.simpleflatmapper.csv.CsvColumnDefinition)2 ParsingContextFactoryBuilder (org.simpleflatmapper.csv.ParsingContextFactoryBuilder)2 EnumOrdinalFormatProperty (org.simpleflatmapper.map.property.EnumOrdinalFormatProperty)2 FieldMapperColumnDefinition (org.simpleflatmapper.map.property.FieldMapperColumnDefinition)2 BooleanGetter (org.simpleflatmapper.reflect.primitive.BooleanGetter)2 ByteGetter (org.simpleflatmapper.reflect.primitive.ByteGetter)2 CharacterGetter (org.simpleflatmapper.reflect.primitive.CharacterGetter)2 DoubleGetter (org.simpleflatmapper.reflect.primitive.DoubleGetter)2 FloatGetter (org.simpleflatmapper.reflect.primitive.FloatGetter)2 IntGetter (org.simpleflatmapper.reflect.primitive.IntGetter)2 LongGetter (org.simpleflatmapper.reflect.primitive.LongGetter)2 ShortGetter (org.simpleflatmapper.reflect.primitive.ShortGetter)2