Removing the primitive obsession with status as int

We have decided to also tackle the optional item in our to-do list.

The to-do list:

  • Dependency injection in BooksEndpoint for books
  • Change status for statuses
  • Remove the primitive obsession with status (optional)
The smell: Primitive obsession involves using primitive data types to represent domain ideas. For example, we use a string to represent a message, an integer to represent an amount of money, or a struct/dictionary/hash to represent a specific object. 

The source is http://c2.com/cgi/wiki?PrimitiveObsession.

As this is a refactor step (that is, we are not introducing any new behavior into the system), we don't need any new specification. We will proceed and try to always stay green, or leave it for as little time as possible.

We have converted  States from a Java class with constants:

public class States { 
    public static final int BOUGHT = 1; 
    public static final int RENTED = 2; 
    public static final int AVAILABLE = 3; 
    public static final int CENSORED = 4; 
} 

And turned it into an enum:

enum States { 
    BOUGHT (1), 
    RENTED (2), 
    AVAILABLE (3), 
    CENSORED (4); 
 
    private final int value; 
 
    private States(int value) { 
        this.value = value; 
    } 
 
    public int getValue() { 
        return value; 
    } 
 
    public static States fromValue(int value) { 
        for (States states : values()) { 
            if(states.getValue() == value) { 
                return states; 
            } 
        } 
        throw new IllegalArgumentException( 
          "Value '" + value 
    + "' could not be found in States"); 
    } 
} 

Adapt the tests as follows:

public class BooksEndpointInteractionTest { 
    @Test 
    public void add_one_book() throws IOException { 
        addBook(TDD_IN_JAVA); 
        verify(booksRepository).add( 
            new Book(TITLE_BOOK_1, AUTHOR_BOOK_1, 
              States.BOUGHT)); 
    } 
    [...] 
public class BooksTest { 
 
    @Test 
    public void should_search_for_any_past_state() { 
        Book book1 = new Book("title", "author", 
States.AVAILABLE); book1.censor(); Books books = new Books(); books.add(book1); assertThat(books.filterByState( String.valueOf( States.AVAILABLE.getValue())) .isEmpty(), is(false)); } [...]

Adapt the production code. The code snippet is as follows:

@XmlRootElement 
public class Books { 
      public Books filterByState(String state) { 
        State expected = 
          States.fromValue(Integer.valueOf(state)); 
        return new Books( 
            new ConcurrentLinkedQueue<>( 
                books.stream() 
                  .filter(x -> x.anyState() 
                    .contains(expected)) 
                  .collect(toList()))); 
    } 
    [...] 

Also the following:

@XmlRootElement 
public class Book { 
 
    private final String title; 
    private final String author; 
    @XmlTransient 
    private ArrayList<States> status; 
    private int id; 
 
    public Book 
      (String title, String author, States status) { 
        this.title = title; 
        this.author = author; 
        this.status = new ArrayList<>(); 
        this.status.add(status); 
    } 
 
    public States getStatus() { 
        return status.get(status.size() - 1); 
    } 
 
    @XmlElement(name = "status") 
    public int getStatusAsInteger(){ 
        return getStatus().getValue(); 
    } 
 
    public List<States> anyState() { 
        return status; 
    } 
    [...] 

In this case, the serialization has been done using the annotation:

@XmlElement(name = "status") 

This converts the result of the method into the field named status.

Also, the status field, now ArrayList<States>, is marked with @XmlTransient so it is not serialized to JSON.

We execute all the tests and they are green, so we can now cross off the optional element in our to-do list.

Tasks performed:

  • Dependency injection in BooksEndpoint for books
  • Change status for statuses
  • Remove the primitive obsession with status (optional)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.72.74