Search in sources :

Example 1 with CellValueReader

use of org.simpleflatmapper.csv.CellValueReader 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 CellValueReader

use of org.simpleflatmapper.csv.CellValueReader 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 3 with CellValueReader

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

the class CsvMapperCustomReaderFactoryTest method testCustomReaderFactory.

@Test
public void testCustomReaderFactory() throws IOException {
    CsvMapper<DbObject> mapper = CsvMapperFactory.newInstance().failOnAsm(true).cellValueReaderFactory(new CellValueReaderFactory() {

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

                @SuppressWarnings("unchecked")
                @Override
                public P read(char[] chars, int offset, int length, ParsingContext parsingContext) {
                    return (P) "Hello!";
                }
            };
        }
    }).newBuilder(DbObject.class).addMapping("name").mapper();
    DbObject bop = mapper.iterator(new StringReader("bop")).next();
    assertEquals("Hello!", bop.getName());
}
Also used : Type(java.lang.reflect.Type) ParsingContext(org.simpleflatmapper.csv.ParsingContext) ParsingContextFactoryBuilder(org.simpleflatmapper.csv.ParsingContextFactoryBuilder) DbObject(org.simpleflatmapper.test.beans.DbObject) CellValueReader(org.simpleflatmapper.csv.CellValueReader) CellValueReaderFactory(org.simpleflatmapper.csv.CellValueReaderFactory) StringReader(java.io.StringReader) CsvColumnDefinition(org.simpleflatmapper.csv.CsvColumnDefinition) Test(org.junit.Test)

Example 4 with CellValueReader

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

the class CsvMapperCellHandlerImpAsmInstantiatorTest method testInstantiateStringWithCharArray.

@SuppressWarnings("unchecked")
@Test
public void testInstantiateStringWithCharArray() throws Exception {
    HashMap<Parameter, Getter<? super CsvMapperCellHandlerImpl<String>, ?>> injections = new HashMap<Parameter, Getter<? super CsvMapperCellHandlerImpl<String>, ?>>();
    Parameter parameter = new Parameter(0, "arg0", char[].class);
    DelayedGetter delayedGetter = new DelayedGetter(0);
    injections.put(parameter, delayedGetter);
    Class<CsvMapperCellHandlerImpl<String>> source = (Class) CsvMapperCellHandlerImpl.class;
    Instantiator<CsvMapperCellHandlerImpl<String>, String> instantiator = factory.createInstantiator(source, new ExecutableInstantiatorDefinition(String.class.getConstructor(char[].class), parameter), injections, true);
    DelayedCellSetterImpl delayedCellSetter = new DelayedCellSetterImpl(null, new CellValueReader() {

        @Override
        public char[] read(char[] chars, int offset, int length, ParsingContext parsingContext) {
            return new char[] { 'h' };
        }
    });
    delayedCellSetter.set(null, 0, 0, null);
    CsvMapperCellHandlerImpl targetSettersImpl = new CsvMapperCellHandlerImpl(instantiator, new DelayedCellSetter[] { delayedCellSetter }, new CellSetter[] {}, null, null, null);
    Assert.assertNotNull(instantiator.newInstance(targetSettersImpl));
}
Also used : ExecutableInstantiatorDefinition(org.simpleflatmapper.reflect.instantiator.ExecutableInstantiatorDefinition) ParsingContext(org.simpleflatmapper.csv.ParsingContext) HashMap(java.util.HashMap) DelayedCellSetterImpl(org.simpleflatmapper.csv.impl.cellreader.DelayedCellSetterImpl) Getter(org.simpleflatmapper.reflect.Getter) CellValueReader(org.simpleflatmapper.csv.CellValueReader) Parameter(org.simpleflatmapper.reflect.Parameter) Test(org.junit.Test)

Example 5 with CellValueReader

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

the class CsvColumnDefinitionTest method testComposition.

@Test
public void testComposition() {
    TimeZone tz = TimeZone.getTimeZone("Europe/Brussels");
    CellValueReaderFactory cellValueReaderFactory = new CellValueReaderFactory() {

        @Override
        public <P> CellValueReader<P> getReader(Type propertyType, int index, CsvColumnDefinition columnDefinition, ParsingContextFactoryBuilder contextFactoryBuilder) {
            return null;
        }

        @Override
        public String toString() {
            return "CellValueReaderFactory";
        }
    };
    final Predicate<PropertyMeta<?, ?>> appliesTo = ConstantPredicate.falsePredicate();
    CsvColumnDefinition compose = CsvColumnDefinition.identity().addDateFormat("yyyyMM").addRename("blop").addCustomReader(new CellValueReader<Integer>() {

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

        @Override
        public String toString() {
            return "CellValueReader";
        }
    }).addCustomCellValueReaderFactory(cellValueReaderFactory).addTimeZone(tz).addKey(appliesTo);
    assertEquals("blop", compose.rename(new CsvColumnKey("bar", -1)).getName());
    assertEquals("blop", CsvColumnDefinition.renameDefinition("blop").rename(new CsvColumnKey("bar", -1)).getName());
    assertArrayEquals(new String[] { "yyyyMM" }, compose.dateFormats());
    assertEquals(new Integer(3), compose.getCustomReader().read(null, 0, 0, null));
    assertEquals(cellValueReaderFactory, compose.getCustomCellValueReaderFactory());
    assertEquals(tz, compose.getTimeZone());
    assertEquals(tz, CsvColumnDefinition.timeZoneDefinition(tz).getTimeZone());
    assertTrue(compose.hasCustomSourceFrom(Object.class));
    assertFalse(compose.ignore());
    assertEquals(Integer.class, compose.getCustomSourceReturnTypeFrom(Object.class));
    assertTrue(CsvColumnDefinition.identity().addIgnore().ignore());
    assertTrue(compose.isKey());
    assertTrue(CsvColumnDefinition.key(appliesTo).isKey());
    assertSame(appliesTo, compose.keyAppliesTo());
    final String toString = compose.addIgnore().toString();
    System.out.println("toString = " + toString);
}
Also used : ParsingContext(org.simpleflatmapper.csv.ParsingContext) ParsingContextFactoryBuilder(org.simpleflatmapper.csv.ParsingContextFactoryBuilder) CellValueReader(org.simpleflatmapper.csv.CellValueReader) TimeZone(java.util.TimeZone) Type(java.lang.reflect.Type) CellValueReaderFactory(org.simpleflatmapper.csv.CellValueReaderFactory) CsvColumnKey(org.simpleflatmapper.csv.CsvColumnKey) PropertyMeta(org.simpleflatmapper.reflect.meta.PropertyMeta) CsvColumnDefinition(org.simpleflatmapper.csv.CsvColumnDefinition) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)5 CellValueReader (org.simpleflatmapper.csv.CellValueReader)5 ParsingContext (org.simpleflatmapper.csv.ParsingContext)5 StringReader (java.io.StringReader)3 Type (java.lang.reflect.Type)3 CellValueReaderFactory (org.simpleflatmapper.csv.CellValueReaderFactory)3 CsvColumnDefinition (org.simpleflatmapper.csv.CsvColumnDefinition)3 ParsingContextFactoryBuilder (org.simpleflatmapper.csv.ParsingContextFactoryBuilder)3 CsvColumnKey (org.simpleflatmapper.csv.CsvColumnKey)2 HashMap (java.util.HashMap)1 TimeZone (java.util.TimeZone)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 DbObject (org.simpleflatmapper.test.beans.DbObject)1 DbPrimitiveObject (org.simpleflatmapper.test.beans.DbPrimitiveObject)1 Tuple2 (org.simpleflatmapper.tuple.Tuple2)1 Predicate (org.simpleflatmapper.util.Predicate)1