javadictionaryrandomrandomized-algorithm

How to randomize an object: Map<Author, List<Book>> with EasyRandom in Java?


I'm trying to randomize an object: Map<Author, List> with easyRandom for testing.

For the moment I haven't get my code to compile. So my code is something like:

MapRandomizer<Author, List<Book>> mapRandomizer = new MapRandomizer<>(Author.class, ArrayList<Book>.class);
        Map<Author, List<Book>> map = mapRandomizer.getRandomValue(); 

But I don't know how to put the list (ArrayList.class???) as a parameter of the mapRandomizer.

Does anyone knows how to get my code to compile and create a random object of type: Map<Author, List>?

Thank you,


Solution

  • You can't really create a random map without creating random entries. You'll need to be able to create each contained type (in your example Author and Book) randomly. Putting them in a list and map is just creating the proper containers then.

    So what you really probably have is

    class MapRandomizer<K, List<E>> {
     private ItemRandomizer<K> keyRandomizer;
     private ListRandomizer<E> listRandomizer;
    
     public Map<K, List<E>> createMap() {
      // random count, for each entry create random K and random List<E>
      // using keyRandomizer and listRandomizer
     }
    }
    

    with

    class ListRandomizer<E> {
     private ItemRandomizer<E> entryRandomizer;
    
     public List<E> createList() {
      // random count, fill list with that amount of entries 
      // using the entry randomizer
     }
    }
    

    and then you can create your concrete randomizer with

    MapRandomizer<Author, List<Book>> randomizer = new MapRandomizer<>(
     new AuthorRandomizer(), new ListRandomizer<Book>(new BookRandomizer())
    );
    

    In this, AuthorRandomizer and BookRandomizer will need to implement ItemRandomizer<Author> and ItemRAndomizer<Book>, respectively.

    EDIT: since you posted your code in the reply, you should be easily able to wrap generate(Autor.class) to implement ItemRandomizer<Author>.