Search in sources :

Example 1 with UnaryFactory

use of org.simpleflatmapper.util.UnaryFactory in project SimpleFlatMapper by arnaudroger.

the class DiscriminatorMapperTest method setUp.

@Before
public void setUp() {
    Mapper<Object[], Person> studentMapper = new Mapper<Object[], Person>() {

        @Override
        public Person map(Object[] source) throws MappingException {
            return map(source, null);
        }

        @Override
        public Person map(Object[] source, MappingContext<? super Object[]> context) throws MappingException {
            StudentGS studentGS = new StudentGS();
            try {
                mapTo(source, studentGS, context);
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
            return studentGS;
        }

        @Override
        public void mapTo(Object[] source, Person target, MappingContext<? super Object[]> context) throws Exception {
            StudentGS studentGS = (StudentGS) target;
            studentGS.setName((String) source[1]);
            studentGS.setId((Integer) source[2]);
        }
    };
    Mapper<Object[], Person> professorMapper = new Mapper<Object[], Person>() {

        @Override
        public Person map(Object[] source) throws MappingException {
            return map(source, null);
        }

        @Override
        public Person map(Object[] source, MappingContext<? super Object[]> context) throws MappingException {
            ProfessorGS professorGS = new ProfessorGS();
            try {
                mapTo(source, professorGS, context);
            } catch (Exception e) {
                ErrorHelper.rethrow(e);
            }
            return professorGS;
        }

        @Override
        public void mapTo(Object[] source, Person target, MappingContext<? super Object[]> context) throws Exception {
            ProfessorGS professorGS = (ProfessorGS) target;
            professorGS.setName((String) source[1]);
            professorGS.setId((Integer) source[2]);
        }
    };
    List<DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>> mappers = new ArrayList<DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>>();
    mappers.add(new DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>(new Predicate<Object[]>() {

        @Override
        public boolean test(Object[] objects) {
            return "student".equals(objects[0]);
        }
    }, studentMapper, new MappingContextFactoryFromRows<Object[], Object[][], RuntimeException>() {

        @Override
        public MappingContext<? super Object[]> newMappingContext(Object[][] objects) throws RuntimeException {
            return MappingContext.INSTANCE;
        }
    }));
    mappers.add(new DiscriminatorMapper.PredicatedMapper<Object[], Object[][], Person, RuntimeException>(new Predicate<Object[]>() {

        @Override
        public boolean test(Object[] objects) {
            return "professor".equals(objects[0]);
        }
    }, professorMapper, new MappingContextFactoryFromRows<Object[], Object[][], RuntimeException>() {

        @Override
        public MappingContext<? super Object[]> newMappingContext(Object[][] objects) throws RuntimeException {
            return MappingContext.INSTANCE;
        }
    }));
    mapper = new DiscriminatorMapper<Object[], Object[][], Person, RuntimeException>(mappers, new UnaryFactory<Object[][], Enumarable<Object[]>>() {

        @Override
        public Enumarable<Object[]> newInstance(Object[][] objects) {
            return new ArrayEnumarable<Object[]>(objects);
        }
    }, new UncheckedConverter<Object[], String>() {

        @Override
        public String convert(Object[] in) {
            return Arrays.toString(in);
        }
    }, RethrowConsumerErrorHandler.INSTANCE);
}
Also used : ArrayList(java.util.ArrayList) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) MappingContextFactoryFromRows(org.simpleflatmapper.map.context.MappingContextFactoryFromRows) MappingException(org.simpleflatmapper.map.MappingException) Predicate(org.simpleflatmapper.util.Predicate) MappingContext(org.simpleflatmapper.map.MappingContext) ProfessorGS(org.simpleflatmapper.test.beans.ProfessorGS) DiscriminatorMapper(org.simpleflatmapper.map.mapper.DiscriminatorMapper) Mapper(org.simpleflatmapper.map.Mapper) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) UncheckedConverter(org.simpleflatmapper.converter.UncheckedConverter) Person(org.simpleflatmapper.test.beans.Person) ArrayEnumarable(org.simpleflatmapper.util.ArrayEnumarable) StudentGS(org.simpleflatmapper.test.beans.StudentGS) Before(org.junit.Before)

Example 2 with UnaryFactory

use of org.simpleflatmapper.util.UnaryFactory in project SimpleFlatMapper by arnaudroger.

the class AsmFactoryTest method testRegisterOrCreate.

@Test
public void testRegisterOrCreate() {
    AsmFactory asmFactory = new AsmFactory(getClass().getClassLoader());
    final MyService[] services = new MyService[] { new MyService(), new MyService() };
    UnaryFactory<AsmFactory, MyService> factory = new UnaryFactory<AsmFactory, MyService>() {

        int i = 0;

        @Override
        public MyService newInstance(AsmFactory asmFactory) {
            return services[i++];
        }
    };
    assertSame(services[0], asmFactory.registerOrCreate(MyService.class, factory));
    assertSame(services[0], asmFactory.registerOrCreate(MyService.class, factory));
}
Also used : AsmFactory(org.simpleflatmapper.reflect.asm.AsmFactory) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) Test(org.junit.Test)

Example 3 with UnaryFactory

use of org.simpleflatmapper.util.UnaryFactory in project SimpleFlatMapper by arnaudroger.

the class ResultSetMapperFactoryTest method testMapToDbObjectStatic.

@Test
public void testMapToDbObjectStatic() throws Exception {
    DBI dbi = new DBI(DbHelper.getHsqlDataSource());
    dbi.registerMapper(new SfmResultSetMapperFactory(new UnaryFactory<Class<?>, Mapper<ResultSet, ?>>() {

        @Override
        public Mapper<ResultSet, ?> newInstance(Class<?> aClass) {
            return new Mapper<ResultSet, DbObject>() {

                @Override
                public DbObject map(ResultSet source) throws MappingException {
                    return map(source, null);
                }

                @Override
                public DbObject map(ResultSet source, MappingContext<? super ResultSet> context) throws MappingException {
                    DbObject dbObject = new DbObject();
                    try {
                        mapTo(source, dbObject, context);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return dbObject;
                }

                @Override
                public void mapTo(ResultSet source, DbObject target, MappingContext<? super ResultSet> context) throws Exception {
                    target.setId(source.getInt("id"));
                    target.setCreationTime(source.getTimestamp("creation_time"));
                    target.setEmail(source.getString("email"));
                    target.setName(source.getString("name"));
                    String type_name = source.getString("type_name");
                    if (type_name != null) {
                        target.setTypeName(DbObject.Type.valueOf(type_name));
                    }
                    target.setTypeOrdinal(DbObject.Type.values()[source.getInt("type_ordinal")]);
                }
            };
        }
    }));
    Handle handle = dbi.open();
    try {
        DbObject dbObject = handle.createQuery(DbHelper.TEST_DB_OBJECT_QUERY).mapTo(DbObject.class).first();
        DbHelper.assertDbObjectMapping(dbObject);
        SfmBindTest.SfmBindExample attach = handle.attach(SfmBindTest.SfmBindExample.class);
        attach.insert(DbObject.newInstance());
        assertTrue(handle.createQuery("select * from TEST_DB_OBJECT").mapTo(DbObject.class).list().size() > 1);
    } finally {
        handle.close();
    }
}
Also used : DbObject(org.simpleflatmapper.test.beans.DbObject) SfmResultSetMapperFactory(org.simpleflatmapper.jdbi.SfmResultSetMapperFactory) DBI(org.skife.jdbi.v2.DBI) MappingException(org.simpleflatmapper.map.MappingException) Handle(org.skife.jdbi.v2.Handle) MappingContext(org.simpleflatmapper.map.MappingContext) Mapper(org.simpleflatmapper.map.Mapper) ResultSet(java.sql.ResultSet) UnaryFactory(org.simpleflatmapper.util.UnaryFactory) Test(org.junit.Test)

Example 4 with UnaryFactory

use of org.simpleflatmapper.util.UnaryFactory in project SimpleFlatMapper by arnaudroger.

the class ReflectionService method getPredifinedClassMetaFactory.

@SuppressWarnings("unchecked")
private static UnaryFactory<ReflectionService, ClassMeta<?>>[] getPredifinedClassMetaFactory() {
    final List<UnaryFactory<ReflectionService, ClassMeta<?>>> list = new ArrayList<UnaryFactory<ReflectionService, ClassMeta<?>>>();
    Consumer<UnaryFactory<ReflectionService, ClassMeta<?>>> consumer = new Consumer<UnaryFactory<ReflectionService, ClassMeta<?>>>() {

        @Override
        public void accept(UnaryFactory<ReflectionService, ClassMeta<?>> reflectionServiceClassMetaUnaryFactory) {
            list.add(reflectionServiceClassMetaUnaryFactory);
        }
    };
    new JavaLangClassMetaFactoryProducer().produce(consumer);
    ProducerServiceLoader.produceFromServiceLoader(ClassMetaFactoryProducer.class, consumer);
    return list.toArray(new UnaryFactory[0]);
}
Also used : JavaLangClassMetaFactoryProducer(org.simpleflatmapper.reflect.impl.JavaLangClassMetaFactoryProducer) Consumer(org.simpleflatmapper.util.Consumer) OptionalClassMeta(org.simpleflatmapper.reflect.meta.OptionalClassMeta) ArrayClassMeta(org.simpleflatmapper.reflect.meta.ArrayClassMeta) ObjectClassMeta(org.simpleflatmapper.reflect.meta.ObjectClassMeta) TupleClassMeta(org.simpleflatmapper.reflect.meta.TupleClassMeta) FastTupleClassMeta(org.simpleflatmapper.reflect.meta.FastTupleClassMeta) ClassMeta(org.simpleflatmapper.reflect.meta.ClassMeta) MapClassMeta(org.simpleflatmapper.reflect.meta.MapClassMeta) UnaryFactory(org.simpleflatmapper.util.UnaryFactory)

Aggregations

UnaryFactory (org.simpleflatmapper.util.UnaryFactory)4 Test (org.junit.Test)2 Mapper (org.simpleflatmapper.map.Mapper)2 MappingContext (org.simpleflatmapper.map.MappingContext)2 MappingException (org.simpleflatmapper.map.MappingException)2 ResultSet (java.sql.ResultSet)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1 UncheckedConverter (org.simpleflatmapper.converter.UncheckedConverter)1 SfmResultSetMapperFactory (org.simpleflatmapper.jdbi.SfmResultSetMapperFactory)1 MappingContextFactoryFromRows (org.simpleflatmapper.map.context.MappingContextFactoryFromRows)1 DiscriminatorMapper (org.simpleflatmapper.map.mapper.DiscriminatorMapper)1 AsmFactory (org.simpleflatmapper.reflect.asm.AsmFactory)1 JavaLangClassMetaFactoryProducer (org.simpleflatmapper.reflect.impl.JavaLangClassMetaFactoryProducer)1 ArrayClassMeta (org.simpleflatmapper.reflect.meta.ArrayClassMeta)1 ClassMeta (org.simpleflatmapper.reflect.meta.ClassMeta)1 FastTupleClassMeta (org.simpleflatmapper.reflect.meta.FastTupleClassMeta)1 MapClassMeta (org.simpleflatmapper.reflect.meta.MapClassMeta)1 ObjectClassMeta (org.simpleflatmapper.reflect.meta.ObjectClassMeta)1 OptionalClassMeta (org.simpleflatmapper.reflect.meta.OptionalClassMeta)1