List-and-set

From cdd
Jump to: navigation, search

One of the main goals of CDD is to facilitate work in different contexts, and collections are no exception from this goal.

CDD has full support for working with lists, sets and maps and the goal is to make the code as clean and flexible as possible.

Working with lists

Let's say we have the following classes in package com.myproject:

public class PersonState {
    public final int ssn;
    public final String name;
}
 
public class PersonCreator {
    private final PersonMutableState state;
 
    public Person asPerson() {
        return new Person(state.asImmutable());
    }
}
 
public class Person extends PersonBehaviour {
    public Person(PersonState state) {
        super(state);
    }
}


Generate and execute:

package com.myproject;
 
import com.google.common.collect.ImmutableList;
 
import static com.myproject.PersonCreator.createPerson;
import static com.myproject.PersonCreator.createPersonList;
 
public class Main {
 
    public static void main(String[] args) {
        ImmutableList<Person> persons = createPersonList(
                createPerson().ssn(1111).name("Bert"),
                createPerson().ssn(2222).name("Carl")
        ).asPersonList();
 
        System.out.println("Persons: " + persons);
    }
}

output:

[Person{ssn=1111, name="Bert"}, Person{ssn=2222, name="Carl"}]


The list can also be wrapped in other ways:

package com.myproject;
 
import static com.myproject.PersonCreator.*;
 
public class Main {
 
    public static void main(String[] args) {
        PersonListBuilder listBuilder = createPersonList(
                createPerson().ssn(1111).name("Bert"),
                createPerson().ssn(2222).name("Carl")
        );
 
        System.out.println("StateList (ImmutableList<PersonState>): " + listBuilder.asStateList());
        System.out.println("MutableStateList (List<PersonMutableState>): " + listBuilder.asMutableStateList());
        System.out.println("PersonList (ImmutableList<Person>): " + listBuilder.asPersonList());
        System.out.println("PersonMutableList (List<Person>): " + listBuilder.asPersonMutableList());
    }
}

output:

StateList (ImmutableList<PersonState>): [{ssn=1111, name="Bert"}, {ssn=2222, name="Carl"}]
MutableStateList (List<PersonMutableState>): [{ssn=1111, name="Bert"}, {ssn=2222, name="Carl"}]
PersonList (ImmutableList<Person>): [Person{ssn=1111, name="Bert"}, Person{ssn=2222, name="Carl"}]
PersonMutableList (List<Person>): [Person{ssn=1111, name="Bert"}, Person{ssn=2222, name="Carl"}]


Working with sets

Sets look similar:

PersonSetBuilder setBuilder = createPersonSet(
        createPerson().ssn(1111).name("Bert"),
        createPerson().ssn(2222).name("Carl")
);
 
System.out.println("StateSet (ImmutableSet<PersonState>): " + setBuilder.asStateSet());
System.out.println("MutableStateSet (Set<PersonMutableState>): " + setBuilder.asMutableStateSet());
System.out.println("PersonSet (ImmutableSet<Person>): " + setBuilder.asPersonSet());
System.out.println("PersonMutableSet (Set<Person>): " + setBuilder.asPersonMutableSet());

output:

StateSet (ImmutableSet<PersonState>): [{ssn=1111, name="Bert"}, {ssn=2222, name="Carl"}]
MutableStateSet (Set<PersonMutableState>): [{ssn=2222, name="Carl"}, {ssn=1111, name="Bert"}]
PersonSet (ImmutableSet<Person>): [Person{ssn=1111, name="Bert"}, Person{ssn=2222, name="Carl"}]
PersonMutableSet (Set<Person>): [Person{ssn=2222, name="Carl"}, Person{ssn=1111, name="Bert"}]

Collections of collections

The class CollectionCreator can be used to create list of lists, map of sets and so forth.

An example is the methods stringAsMutableState and convertToMapAndBackAgain in PersonTest (with the state defined here:

PersonBuilder person = defaultPerson()
        .withChildren(createPersonSet(createPerson().name("Anders").hairColor(BLACK).children().defaults()))
        .withAddress(buildAddress().withId(2).withStreetName("Third street").withCity("Stockholm"))
        .withOldAddress(buildAddress().withId(3))
        .withOldAddresses(createAddressSet(defaultAddress(4)))
        .withGroupedAddresses(createAddressMap(createAddressEntry("abc", defaultAddress(5))))
        .withListOfSetOfState(createList(createSet(defaultAddress(6).asMutableState())))
        .withListOfSetOfMapOfIntegers(createList(createSet(mapOfIntegers())));


< Back          Next >