I came up with something useful, so I used it as a memorandum.
ErrorMessage.java
package listTest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;
public class ErrorMessage {
public static void main(String[] args) {
//Suppose a list of models is entered
List<Model> models = new ArrayList<>();
//Get all types of detected error messages
List<String> eMsgCodes = models.stream()
.map(ErrorMessage::validationLogic)
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toList());
//Output message code by converting it from a configuration file
eMsgCodes.forEach(ErrorMessage::viewErrorMessage);
}
/**
*Check the input value for each element.
* <p>
*Unique judgment that does not pop with annotation etc..
*
* @param model The model to be checked
* @return List of error words
*/
private static List<String> validationLogic(Model model) {
List<String> errorMessages = new LinkedList<>();
//Error with non-id id
if (0 != model.getId()) {
errorMessages.add("MessageCode.001");
}
//Error whose name is not 〇〇
if (!"???".equals(model.getName())) {
errorMessages.add("MessageCode.002");
}
//Error that the address is not 〇〇
if (!"???".equals(model.getAddress())) {
errorMessages.add("MessageCode.003");
}
return errorMessages;
}
/**
*Convert message code to display message and output.
* <p>
*Display the corresponding wording obtained from the configuration file on the screen.
*
* @param msgCode Message code
* @see config file
*/
private static void viewErrorMessage(String msgCode) {
System.out.println(msgCode);
}
}
class Model {
private int id;
private String name;
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Mainly here
here.java
//Get all types of detected error messages
List<String> eMsgCodes = models.stream()
.map(ErrorMessage::validationLogic)
.flatMap(Collection::stream)
.distinct()
.collect(Collectors.toList());
If you know that you will get multiple error messages after checking In terms of logic, the argument is model and the return value is List. It's all processed by streaming it and eliminating fog.
It feels pretty good
Recommended Posts