Skip to content

Instantly share code, notes, and snippets.

View BumpeiShimada's full-sized avatar
🏊‍♂️
meditating

curryisdrink BumpeiShimada

🏊‍♂️
meditating
View GitHub Profile
package main
import (
"fmt"
"time"
)
func main() {
now := time.Now()
@BumpeiShimada
BumpeiShimada / gist:df59386b6f62c54d105a28d71bb24d0a
Created July 9, 2019 18:53
A function enables text data line breaking
const lines = tweet.split("\\n").map(function(line) {
return <p className="line-wrap">{line}</p>;
});
public class LoginLogic {
@Autowired
private OriginalTimeManager timeManager; // This is an in-house class for managing data related to dates
/**
* @param graduateYear
* @return whether current date is after or equal to graduate year's 1st April
* @throws NullPointerException if the param graduateYear is null
*/
public class LoginLogic {
// ...
/**
* @return whether current date is after or equal to graduate year's 1st April
* NOTE FOR READERS: In Japan, students commonly graduates at the end of March
*/
boolean isAfterServiceGraduateDate() {
// Let's create a test before implementation!!
class LoginLogicUnitTest extends Specification {
private CandidateLoginLogic logic
def setup(){
logic = new CandidateLoginLogic()
// Mocking autowired methods
OriginalTimeManager timeManager = Mock()
logic.metaClass.setAttribute(logic, "timeManager", timeManager)
@Component
public class DateEmptyValidator implements BaseValidator {
@Override
public void validate(EventForm form, List<String> errors) {
validateStartDate(form, errors);
validateEndDate(form, errors);
}
@Override
public ValidateType getValidateType() {
@Component
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class ValidatorFactory {
// All concrete classes of an interface can be asserted like this in Spring.
// If you want to know more, see: https://dzone.com/articles/load-all-implementors
private List<BaseValidator> validators;
/**
* Filter out all unnecessary validation classes by checking event types
* and returns only needed ones.
@Service
@AllArgsConstructor(onConstructor = @__(@Autowired))
public class ValidationService {
private ValidatorFactory factory;
public void execute(boolean isExclusive, EventForm form, List<String> errors) {
List<BaseValidator> validators = factory.get(isExclusive);
validators.forEach(validator -> {
validator.validate(form, errors);
});
/**
* Types for categorizing validation classes
*/
enum ValidateType {
/**
* Applicable to all events
*/
ALL_EVENTS,
/**
* The base class for validating EventForm data
*/
public interface BaseValidator {
// Argument "errors" should be replaced with error handling class.
// In actual case, when "errors" are not empty a program detect it and understand that there are errors.
void validate(EventForm form, List<String> errors);
ValidateType getValidateType();