Skip to content

Commit

Permalink
refactor : Number Class 정적팩토리로 표현, 메모리에 저장
Browse files Browse the repository at this point in the history
  • Loading branch information
naheenosaur committed Feb 24, 2020
1 parent d65b03c commit 0a8fbea
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 50 deletions.
52 changes: 52 additions & 0 deletions src/main/java/calculator/Number.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package calculator;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Number {
private static final Pattern NUMERIC_PATTERN = Pattern.compile("[+]?\\d+");
private static final Map<Integer, Number> positiveNumbers = new HashMap<>();
private int value;

private Number() {
this.value = 0;
}

private Number(int num) {
this.value = num;
}

public static Number sumOf(List<String> strings) {
Number number = new Number();
for (String string : strings) {
number.add(ofPositive(string).value);
}
return number;
}

private static Number ofPositive(String stringNumber) {
int num = parsePositiveInt(stringNumber);
Number number = positiveNumbers.getOrDefault(num, new Number(num));
positiveNumbers.put(num, number);
return number;
}

private static int parsePositiveInt(String string) {
Matcher isNumeric = NUMERIC_PATTERN.matcher(string);
if (isNumeric.matches()) {
return Integer.parseInt(string);
}
throw new IllegalArgumentException();
}

public int getValue() {
return this.value;
}

private void add(int number) {
this.value += number;
}
}
41 changes: 0 additions & 41 deletions src/main/java/calculator/PositiveNumber.java

This file was deleted.

5 changes: 3 additions & 2 deletions src/main/java/calculator/StringCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.apache.logging.log4j.util.Strings;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Expand All @@ -18,8 +19,8 @@ public int calculate(String input) {
return 0;
}
List<String> inputs = splitInputs(input);
PositiveNumber positiveNumber = new PositiveNumber(inputs);
return positiveNumber.getValue();
Number number = Number.sumOf(Collections.unmodifiableList(inputs));
return number.getValue();
}

private List<String> splitInputs(String input) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
import org.junit.jupiter.params.provider.MethodSource;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.params.provider.Arguments.arguments;

class PositiveNumberTest {
class NumberTest {
private static final Stream<Arguments> throwWithNonNumericExceptionArguments() {
return Stream.of(
arguments(Arrays.asList("-1", "2", "3")),
Expand All @@ -24,6 +25,8 @@ private static final Stream<Arguments> throwWithNonNumericExceptionArguments() {
@MethodSource("throwWithNonNumericExceptionArguments")
@DisplayName("숫자 이외의 값 또는 음수는 RuntimeException을 발생한다.")
void throwWithNonNumericException(List<String> inputs) {
assertThrows(IllegalArgumentException.class, () -> new PositiveNumber(inputs));
assertThrows(IllegalArgumentException.class,
() -> Number.sumOf(Collections.unmodifiableList(inputs))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@

import com.fasterxml.jackson.databind.ObjectMapper;
import kitchenpos.bo.MenuGroupBo;
import kitchenpos.model.MenuGroupTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
Expand Down

0 comments on commit 0a8fbea

Please sign in to comment.