diff --git a/src/main/java/hello/controller/TopicController.java b/src/main/java/hello/controller/TopicController.java index c2e8273..c4e322c 100644 --- a/src/main/java/hello/controller/TopicController.java +++ b/src/main/java/hello/controller/TopicController.java @@ -3,6 +3,7 @@ import hello.model.Topic; import hello.service.TopicService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -29,8 +30,10 @@ public List getAllTopics() { * @return */ @RequestMapping("/topic/{id}") - public Topic getTopicWithID(@PathVariable String id) { - return topicService.getTopicWithId(id); + public ResponseEntity getTopicWithID(@PathVariable String id) { + return topicService.getTopicWithId(id) + .map(ResponseEntity::ok) + .orElseGet(() -> ResponseEntity.notFound().build()); } /** diff --git a/src/main/java/hello/declaration/CustomPredicate.java b/src/main/java/hello/declaration/CustomPredicate.java index 46f0c44..af828ac 100644 --- a/src/main/java/hello/declaration/CustomPredicate.java +++ b/src/main/java/hello/declaration/CustomPredicate.java @@ -4,6 +4,7 @@ * Use as a Functional Interface * @param */ +@FunctionalInterface public interface CustomPredicate { boolean test(T t); } diff --git a/src/main/java/hello/model/Customer.java b/src/main/java/hello/model/Customer.java index a6df80e..640db05 100644 --- a/src/main/java/hello/model/Customer.java +++ b/src/main/java/hello/model/Customer.java @@ -1,45 +1,4 @@ package hello.model; -public class Customer { - private long id; - private String firstName, lastName; - - @Override - public String toString() { - return "Customer{" + - "id=" + id + - ", firstName='" + firstName + '\'' + - ", lastName='" + lastName + '\'' + - '}'; - } - - public Customer(long id, String firstName, String lastName) { - this.id = id; - this.firstName = firstName; - this.lastName = lastName; - } - - public long getId() { - return id; - } - - public void setId(long id) { - this.id = id; - } - - public String getFirstName() { - return firstName; - } - - public void setFirstName(String firstName) { - this.firstName = firstName; - } - - public String getLastName() { - return lastName; - } - - public void setLastName(String lastName) { - this.lastName = lastName; - } +public record Customer(long id, String firstName, String lastName) { } diff --git a/src/main/java/hello/model/Greeting.java b/src/main/java/hello/model/Greeting.java index f2e7772..135b2fb 100644 --- a/src/main/java/hello/model/Greeting.java +++ b/src/main/java/hello/model/Greeting.java @@ -1,21 +1,4 @@ package hello.model; -public class Greeting { - private final long id; - private final String content; - - public Greeting(long id, String content) { - this.id = id; - this.content = content; - } - - - public long getId() { - - return id; - } - - public String getContent() { - return content; - } +public record Greeting(long id, String content) { } diff --git a/src/main/java/hello/model/Quote.java b/src/main/java/hello/model/Quote.java index 2178a6d..b548a67 100644 --- a/src/main/java/hello/model/Quote.java +++ b/src/main/java/hello/model/Quote.java @@ -1,34 +1,4 @@ package hello.model; -public class Quote { - private String type; - private Value value; - - @Override - public String toString() { - return "Quote{" + - "type='" + type + '\'' + - ", value=" + value + - '}'; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - public Value getValue() { - return value; - } - - public void setValue(Value value) { - this.value = value; - } - - public Quote() { - - } +public record Quote(String type, Value value) { } diff --git a/src/main/java/hello/model/Topic.java b/src/main/java/hello/model/Topic.java index 483a4a7..59740dc 100644 --- a/src/main/java/hello/model/Topic.java +++ b/src/main/java/hello/model/Topic.java @@ -1,43 +1,4 @@ package hello.model; - -public class Topic { - private String id; - private String subjectName; - private String subjectDescription; - - public Topic() { - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getSubjectName() { - return subjectName; - } - - public void setSubjectName(String subjectName) { - this.subjectName = subjectName; - } - - public String getSubjectDescription() { - return subjectDescription; - } - - public void setSubjectDescription(String subjectDescription) { - this.subjectDescription = subjectDescription; - } - - public Topic(String id, String subjectName, String subjectDescription) { - super(); - this.id = id; - this.subjectName = subjectName; - this.subjectDescription = subjectDescription; - } - +public record Topic(String id, String subjectName, String subjectDescription) { } diff --git a/src/main/java/hello/model/Value.java b/src/main/java/hello/model/Value.java index 272be70..b795170 100644 --- a/src/main/java/hello/model/Value.java +++ b/src/main/java/hello/model/Value.java @@ -1,33 +1,4 @@ package hello.model; -public class Value { - private Long id; - private String quote; - - public Value() { - } - - @Override - public String toString() { - return "Value{" + - "id=" + id + - ", quote='" + quote + '\'' + - '}'; - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getQuote() { - return quote; - } - - public void setQuote(String quote) { - this.quote = quote; - } +public record Value(Long id, String quote) { } diff --git a/src/main/java/hello/service/TopicService.java b/src/main/java/hello/service/TopicService.java index feffb92..d0a5706 100644 --- a/src/main/java/hello/service/TopicService.java +++ b/src/main/java/hello/service/TopicService.java @@ -35,8 +35,8 @@ public List getAllTopics() { * @param id * @return */ - public Topic getTopicWithId(String id) { - return topics.stream().filter(topic -> topic.getId().equals(id)).findFirst().get(); + public Optional getTopicWithId(String id) { + return topics.stream().filter(topic -> topic.id().equals(id)).findFirst(); } public void addTopic(Topic topic) { @@ -50,7 +50,7 @@ public void addTopic(Topic topic) { * @param topic */ public void updateTopic(String id, Topic topic) { - OptionalInt indexOfElement = IntStream.range(0, topics.size()).filter(index -> id.equals(topics.get(index).getId())).findFirst(); + OptionalInt indexOfElement = IntStream.range(0, topics.size()).filter(index -> id.equals(topics.get(index).id())).findFirst(); if (indexOfElement.isPresent()) topics.set(indexOfElement.getAsInt(), topic); } @@ -60,7 +60,7 @@ public void updateTopic(String id, Topic topic) { * @param id */ public void deleteTopic(String id) { - topics.removeIf(topic -> topic.getId().equals(id)); + topics.removeIf(topic -> topic.id().equals(id)); } /** @@ -70,7 +70,7 @@ public void deleteTopic(String id) { * @return */ public List filterMinimumLengthForId(Integer minLength) { - return printTopicsWithPredicate(topics, topic -> topic.getId().length() > minLength); + return printTopicsWithPredicate(topics, topic -> topic.id().length() > minLength); } @@ -96,7 +96,7 @@ private static List printTopicsWithPredicate(List topicList, Custo * @return */ public List sortTopicsWithID() { - topics.sort(Comparator.comparing(Topic::getId)); + topics.sort(Comparator.comparing(Topic::id)); return topics; } @@ -107,7 +107,7 @@ public List sortTopicsWithID() { * @return */ public String returnAllTopicIDWithStringSlicing() { - List topicIds = topics.stream().map(topic -> topic.getId()).collect(Collectors.toList()); + List topicIds = topics.stream().map(Topic::id).toList(); return String.join(":", topicIds); } @@ -147,13 +147,11 @@ public String splitAllIdWithColonSelectIDWithJavaKeywordThenSortThenJoin(String */ public String findIdHavingCharacter() { Pattern pattern = Pattern.compile(".*g.*"); - Object[] topicIdObjectList = topics.stream().map(topic -> topic.getId()).collect(Collectors.toList()).toArray(); - - String[] topicIdList = Arrays.stream(topicIdObjectList).toArray(String[]::new); + String[] topicIdList = topics.stream().map(Topic::id).toArray(String[]::new); return Stream.of(topicIdList) .filter(pattern.asPredicate()) - .collect(Collectors.toList()) + .toList() .toString(); }