All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.inspiresoftware.lib.dto.geda.examples.usecases.generics.RunWithGenerics Maven / Gradle / Ivy

There is a newer version: 3.2.0
Show newest version
/*
 * This code is distributed under The GNU Lesser General Public License (LGPLv3)
 * Please visit GNU site for LGPLv3 http://www.gnu.org/copyleft/lesser.html
 *
 * Copyright Denis Pavlov 2009
 * Web: http://www.genericdtoassembler.org
 * SVN: https://svn.code.sf.net/p/geda-genericdto/code/trunk/
 * SVN (mirror): http://geda-genericdto.googlecode.com/svn/trunk/
 */

package com.inspiresoftware.lib.dto.geda.examples.usecases.generics;

import com.inspiresoftware.lib.dto.geda.adapter.BeanFactory;
import com.inspiresoftware.lib.dto.geda.assembler.Assembler;
import com.inspiresoftware.lib.dto.geda.assembler.DTOAssembler;
import com.inspiresoftware.lib.dto.geda.exception.GeDAException;

import java.util.*;

import static org.junit.Assert.*;

/**
 * User: denispavlov
 * Date: 13-04-22
 * Time: 7:07 PM
 */
public class RunWithGenerics {

    /**
     * Example showing how to use GeDA with generics. The answer is that there is no difference
     * but you do need to at least distinguish between fields, collections and maps.
     */
    public void genericMapping() throws GeDAException {

        final DtoGenericClass>, Map>> dto = new DtoClass();
        final EntityClass entity = new EntityClass();

        final Assembler assembler = DTOAssembler.newAssembler(dto.getClass(), entity.getClass());

        final DtoGenericItemClass item = new DtoGenericItemClass();
        item.setMyProp("item");

        dto.setMyProp("prop");
        final List> coll = new ArrayList>();
        coll.add(item);
        dto.setMyColl(coll);
        final Map> map = new HashMap>();
        map.put("m1", item);
        dto.setMyMap(map);

        assembler.assembleEntity(dto, entity, null, new BeanFactory() {

            public Class getClazz(final String entityBeanKey) {
                return null;
            }

            public Object get(final String entityBeanKey) {
                return new EntityGenericItemClass();
            }

        });

        assertEquals("prop", entity.getMyProp());

        assertNotNull(entity.getMyColl());
        assertNotSame(dto.getMyColl(), entity.getMyColl());
        assertEquals(1, entity.getMyColl().size());
        assertEquals("item", entity.getMyColl().iterator().next().getMyProp());

        assertNotNull(entity.getMyMap());
        assertNotSame(dto.getMyMap(), entity.getMyMap());
        assertEquals(1, entity.getMyMap().size());
        assertEquals("item", entity.getMyMap().iterator().next().getMyProp());

        entity.setMyProp("e1");
        entity.getMyColl().iterator().next().setMyProp("ci1");
        entity.getMyMap().iterator().next().setMyProp("mi1");

        assembler.assembleDto(dto, entity, null, new BeanFactory() {

            public Class getClazz(final String entityBeanKey) {
                return null;
            }

            public Object get(final String entityBeanKey) {
                return new DtoGenericItemClass();
            }

        });

        assertEquals("e1", dto.getMyProp());

        assertNotNull(dto.getMyColl());
        assertNotSame(dto.getMyColl(), entity.getMyColl());
        assertEquals(1, dto.getMyColl().size());
        assertEquals("ci1", dto.getMyColl().iterator().next().getMyProp());

        assertNotNull(entity.getMyMap());
        assertNotSame(dto.getMyMap(), entity.getMyMap());
        assertEquals(1, dto.getMyMap().size());
        assertTrue(dto.getMyMap().containsKey("mi1"));
        assertEquals("mi1", dto.getMyMap().get("mi1").getMyProp());


    }

    public static void main(String[] args) {
        new RunWithGenerics().genericMapping();
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy