Search in sources :

Example 1 with ParsingContext

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

the class CsvMapperCustomReaderTest method testCustomReaderPrimitivesWithBoxReader.

@Test
public void testCustomReaderPrimitivesWithBoxReader() throws IOException {
    CsvMapper<DbPrimitiveObject> intMapper = csvMapperFactory.newBuilder(DbPrimitiveObject.class).addMapping("p_boolean", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Boolean>() {

        @Override
        public Boolean read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return true;
        }
    })).addMapping("p_byte", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Byte>() {

        @Override
        public Byte read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 35;
        }
    })).addMapping("p_character", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Character>() {

        @Override
        public Character read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 37;
        }
    })).addMapping("p_short", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Short>() {

        @Override
        public Short read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 39;
        }
    })).addMapping("p_int", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Integer>() {

        @Override
        public Integer read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0x76666;
        }
    })).addMapping("p_long", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Long>() {

        @Override
        public Long read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0xf76666l;
        }
    })).addMapping("p_float", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Float>() {

        @Override
        public Float read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.14f;
        }
    })).addMapping("p_double", CsvColumnDefinition.customReaderDefinition(new CellValueReader<Double>() {

        @Override
        public Double read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.1526;
        }
    })).mapper();
    DbPrimitiveObject object = intMapper.iterator(new StringReader("false,12,12,12,12,12,12,12")).next();
    assertEquals(true, object.ispBoolean());
    assertEquals(35, object.getpByte());
    assertEquals(37, object.getpCharacter());
    assertEquals(39, object.getpShort());
    assertEquals(0x76666, object.getpInt());
    assertEquals(0xf76666l, object.getpLong());
    assertEquals(3.14f, object.getpFloat(), 0.0001);
    assertEquals(3.1526, object.getpDouble(), 0.000001);
}
Also used : ParsingContext(org.simpleflatmapper.csv.ParsingContext) CellValueReader(org.simpleflatmapper.csv.CellValueReader) StringReader(java.io.StringReader) DbPrimitiveObject(org.simpleflatmapper.test.beans.DbPrimitiveObject) Test(org.junit.Test)

Example 2 with ParsingContext

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

the class CsvMapperCustomReaderTest method testCustomReaderPrimitivesWithDirectReader.

@Test
public void testCustomReaderPrimitivesWithDirectReader() throws IOException {
    CsvMapper<DbPrimitiveObject> intMapper = csvMapperFactory.newBuilder(DbPrimitiveObject.class).addMapping("p_boolean", CsvColumnDefinition.customReaderDefinition(new BooleanCellValueReader() {

        @Override
        public Boolean read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return true;
        }

        @Override
        public boolean readBoolean(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return true;
        }
    })).addMapping("p_byte", CsvColumnDefinition.customReaderDefinition(new ByteCellValueReader() {

        @Override
        public Byte read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 35;
        }

        @Override
        public byte readByte(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 35;
        }
    })).addMapping("p_character", CsvColumnDefinition.customReaderDefinition(new CharCellValueReader() {

        @Override
        public Character read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 37;
        }

        @Override
        public char readChar(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 37;
        }
    })).addMapping("p_short", CsvColumnDefinition.customReaderDefinition(new ShortCellValueReader() {

        @Override
        public Short read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 39;
        }

        @Override
        public short readShort(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 39;
        }
    })).addMapping("p_int", CsvColumnDefinition.customReaderDefinition(new IntegerCellValueReader() {

        @Override
        public Integer read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0x76666;
        }

        @Override
        public int readInt(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0x76666;
        }
    })).addMapping("p_long", CsvColumnDefinition.customReaderDefinition(new LongCellValueReader() {

        @Override
        public long readLong(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0xf76666l;
        }

        @Override
        public Long read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 0xf76666l;
        }
    })).addMapping("p_float", CsvColumnDefinition.customReaderDefinition(new FloatCellValueReader() {

        @Override
        public Float read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.14f;
        }

        @Override
        public float readFloat(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.14f;
        }
    })).addMapping("p_double", CsvColumnDefinition.customReaderDefinition(new DoubleCellValueReader() {

        @Override
        public Double read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.1526;
        }

        @Override
        public double readDouble(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return 3.1526;
        }
    })).mapper();
    DbPrimitiveObject object = intMapper.iterator(new StringReader("false,12,12,12,12,12,12,12")).next();
    assertEquals(true, object.ispBoolean());
    assertEquals(35, object.getpByte());
    assertEquals(37, object.getpCharacter());
    assertEquals(39, object.getpShort());
    assertEquals(0x76666, object.getpInt());
    assertEquals(0xf76666l, object.getpLong());
    assertEquals(3.14f, object.getpFloat(), 0.0001);
    assertEquals(3.1526, object.getpDouble(), 0.000001);
}
Also used : ParsingContext(org.simpleflatmapper.csv.ParsingContext) StringReader(java.io.StringReader) DbPrimitiveObject(org.simpleflatmapper.test.beans.DbPrimitiveObject) Test(org.junit.Test)

Example 3 with ParsingContext

use of org.simpleflatmapper.csv.ParsingContext 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 4 with ParsingContext

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

the class CsvParserTest method testDSLMapWithCustomDefinitionOnStaticMapper.

@Test
public void testDSLMapWithCustomDefinitionOnStaticMapper() throws Exception {
    Iterator<Tuple2<String, String>> iterator = CsvParser.mapTo(String.class, String.class).addMapping("0").addMapping("1", CsvColumnDefinition.customReaderDefinition(new CellValueReader<String>() {

        @Override
        public String read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return "c1";
        }
    })).iterator(new StringReader("0,1\nv0,v1"));
    Tuple2<String, String> tuple = iterator.next();
    assertEquals("v0", tuple.first());
    assertEquals("c1", tuple.second());
}
Also used : ParsingContext(org.simpleflatmapper.csv.ParsingContext) Tuple2(org.simpleflatmapper.tuple.Tuple2) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 5 with ParsingContext

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

the class CsvParserTest method testDSLMapWithCustomDefinition.

// IFJAVA8_END
@Test
public void testDSLMapWithCustomDefinition() throws Exception {
    Iterator<Tuple2<String, String>> iterator = CsvParser.mapTo(String.class, String.class).columnDefinition("1", CsvColumnDefinition.customReaderDefinition(new CellValueReader<String>() {

        @Override
        public String read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return "c1";
        }
    })).iterator(new StringReader("0,1\nv0,v1"));
    Tuple2<String, String> tuple = iterator.next();
    assertEquals("v0", tuple.first());
    assertEquals("c1", tuple.second());
}
Also used : ParsingContext(org.simpleflatmapper.csv.ParsingContext) Tuple2(org.simpleflatmapper.tuple.Tuple2) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)9 ParsingContext (org.simpleflatmapper.csv.ParsingContext)9 StringReader (java.io.StringReader)7 CellValueReader (org.simpleflatmapper.csv.CellValueReader)5 CsvColumnDefinition (org.simpleflatmapper.csv.CsvColumnDefinition)4 Type (java.lang.reflect.Type)3 CellValueReaderFactory (org.simpleflatmapper.csv.CellValueReaderFactory)3 ParsingContextFactoryBuilder (org.simpleflatmapper.csv.ParsingContextFactoryBuilder)3 Tuple2 (org.simpleflatmapper.tuple.Tuple2)3 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)2 DbPrimitiveObject (org.simpleflatmapper.test.beans.DbPrimitiveObject)2 HashMap (java.util.HashMap)1 TimeZone (java.util.TimeZone)1 CsvMapperBuilder (org.simpleflatmapper.csv.CsvMapperBuilder)1 DelayedCellSetterImpl (org.simpleflatmapper.csv.impl.cellreader.DelayedCellSetterImpl)1 Getter (org.simpleflatmapper.reflect.Getter)1 Parameter (org.simpleflatmapper.reflect.Parameter)1 ExecutableInstantiatorDefinition (org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition)1 PropertyMeta (org.simpleflatmapper.reflect.meta.PropertyMeta)1 Db1DeepObject (org.simpleflatmapper.test.beans.Db1DeepObject)1