Processing to issue an error message

Processing to issue an error message

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

Processing to issue an error message
I want to display an error message when registering in the database
When deploying to Heroku, OpenApp causes an error
How to make an image partially transparent in Processing
What to do if an ActionController :: UnknownFormat error occurs
[Rails] What to do if you can't get an error message with the errors method
Steps required to issue an asynchronous event for Spring Boot
From Ruby on Rails error message display to Japanese localization
Display an error screen during the download process to response
How to create an application
Easy to create Processing library
Common processing and error processing springmvc
[Ruby] Introduction to Ruby Error statement
[Swift] Processing to share screenshots
How to handle an instance
Summary of java error processing
Response to WSL2 docker throwing an error after updating Docker for Windows
[Java] Adding an element to the Collection causes a compile error
I tried to translate the error message when executing Eclipse (Java)
I got an error and couldn't push! !! [error: failed to push some refs to]
I want to control the default error message of Spring Boot