refactoring : primitive obsession - replace primitive with object
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.example.refactoring._11_primitive_obsession._30_replace_primitive_with_object;
|
||||
|
||||
public class Order {
|
||||
|
||||
private final Priority priority;
|
||||
|
||||
public Order(String priorityValue) {
|
||||
this(new Priority(priorityValue));
|
||||
}
|
||||
public Order(Priority priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public Priority getPriority() {
|
||||
return priority;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.example.refactoring._11_primitive_obsession._30_replace_primitive_with_object;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
|
||||
public class OrderProcessor {
|
||||
|
||||
public long numberOfHighPriorityOrders(List<Order> orders) {
|
||||
return orders.stream()
|
||||
.filter(o -> o.getPriority().higherThan(new Priority("normal")))
|
||||
.count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.example.refactoring._11_primitive_obsession._30_replace_primitive_with_object;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class Priority {
|
||||
|
||||
private final String value;
|
||||
|
||||
private final List<String> type = List.of("low", "normal", "high", "rush");
|
||||
|
||||
public Priority(String value) {
|
||||
if (type.contains(value)) {
|
||||
this.value = value;
|
||||
} else {
|
||||
throw new IllegalArgumentException("illegal value for priority : " + value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return this.value;
|
||||
}
|
||||
|
||||
private int index() {
|
||||
return this.type.indexOf(this.value);
|
||||
}
|
||||
|
||||
public boolean higherThan(Priority other) {
|
||||
return this.index() > other.index();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.example.refactoring._11_primitive_obsession._30_replace_primitive_with_object._before;
|
||||
|
||||
public class Order {
|
||||
|
||||
private String priority;
|
||||
|
||||
public Order(String priority) {
|
||||
this.priority = priority;
|
||||
}
|
||||
|
||||
public String getPriority() {
|
||||
return priority;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.example.refactoring._11_primitive_obsession._30_replace_primitive_with_object._before;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class OrderProcessor {
|
||||
|
||||
public long numberOfHighPriorityOrders(List<Order> orders) {
|
||||
return orders.stream()
|
||||
.filter(o -> o.getPriority() == "high" || o.getPriority() == "rush")
|
||||
.count();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.example.refactoring._11_primitive_obsession._30_replace_primitive_with_object;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class OrderProcessorTest {
|
||||
|
||||
@Test
|
||||
void numberOfHighPriorityOrders() {
|
||||
OrderProcessor orderProcessor = new OrderProcessor();
|
||||
long highPriorityOrders = orderProcessor.numberOfHighPriorityOrders(
|
||||
List.of(new Order("low"),
|
||||
new Order("normal"),
|
||||
new Order("high"),
|
||||
new Order("rush")));
|
||||
assertEquals(2, highPriorityOrders);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user