Control class for an entity For a Student object in the package entity, I have a control.studentpackage with classes that are relevant to the Student object.

The Student is a class with only private properties and getters/setters for every property. The setters do no validation on their own.

My project structure is as follows:

src/
  control/
    student/
      StudentControl.java
      StudentValidator.java
      ValidationError.java
  entity/
     Student.java


ValidationError is just a String wrapper with getters and setters.

The StudentValidator is a functional interface that extends Function&lt;Student, Optional&lt;ValidationError&gt;&gt;.

interface StudentValidator extends Function&lt;Student, Optional&lt;ValidationError&gt;&gt; {}


The StudentControl class is as follows:

package control.student;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import entity.Student;

public class StudentControl {

  private static final Pattern namePattern = Pattern.compile("[aA-zZ]+");

  /**
   * List of functions that validate a Student.
   */
  private static final List&lt;StudentValidator&gt; validatorFunctions = new ArrayList&lt;&gt;();
  static {
    validatorFunctions.add(validateNamesFunction());
  }

  /**
   * Validates a Student's fields.
   * 
   * @param student Student to validate
   * @return an Optional that may contain a List of ValidationError. Those ValidationError(s) will
   *         have error messages describing each error occurred while validating the student.
   */
  public static Optional&lt;List&lt;ValidationError&gt;&gt; validate(Student student) {
    List&lt;ValidationError&gt; errors = validatorFunctions.stream()
        .map(f -&gt; f.apply(student))
        .filter(Optional::isPresent)
        .map(Optional::get)
        .collect(Collectors.toList());

    return errors.isEmpty() ? Optional.empty() : Optional.of(errors);
  }

  /**
   * Returns a function that validates a Student's name.
   */
  public static StudentValidator validateNamesFunction() {
    return student -&gt; {
      return namePattern.matcher(student.getName()).matches()
          ? Optional.empty()
          : Optional.of(new ValidationError("Invalid name"));
    };
  }

  /**
   * Returns a function that validates a Student's birth date.
   */
  public static StudentValidator validateBirthDateFunction() {
    return student -&gt; {
      return student.getBirthDate().getTime() &gt; new Date().getTime()
          ? Optional.of(new ValidationError("Student was born in the future."))
          : Optional.empty();
    };
  }

  /**
   * Returns a function that validates a Student's join date.
   */
  public static StudentValidator validateJoinDateFunction() {
    return student -&gt; {
      return student.getJoinDate().getTime() &gt; new Date().getTime()
          ? Optional.of(new ValidationError("Student joined in the future."))
          : Optional.empty();
    };
  }
}


Is this code readable? Does it easily convey to the programmer what it is meant to do? Does it comply with the MVC pattern? How could I change my code to make it look, feel and run better?
Creating validators

I think it's perfectly readable. However, there's a lot of repetition.

To make creating validators simpler and more declarative, you could take it one step further and lift the test/return-Optional logic into a higher-order function:

public class StudentControl {

  public static final StudentValidator namesValidator = validator(
    student -&gt; namePattern.matcher(student.getName()).matches(),
    "Invalid name"
  );

  ...

  private static StudentValidator validator(Predicate&lt;Student&gt; pred, String error) {
    return student -&gt; pred.test(student)
      ? Optional.empty()
      : Optional.of(error);
  }
}


and then if you have other models besides Student, you might genericize the validators.

interface EntityValidator&lt;T&gt; extends Function&lt;T, Optional&lt;ValidationError&gt;&gt; {}

public abstract class EntityControl&lt;T&gt; {
  protected final List&lt;EntityValidator&lt;? super T&gt;&gt; validators = new ArrayList();

  protected EntityValidator&lt;T&gt; validator(Predicate&lt;T&gt; pred, String error) {
    return x -&gt; pred.test(x)
      ? Optional.empty()
      : Optional.of(error);
  }

  // Or, if you want to allow any subclass to create a validator for any type,
  // it could be static:
  protected static &lt;T&gt; EntityValidator&lt;T&gt; validator(...)

  public Optional&lt;List&lt;ValidationError&gt;&gt; validate(T entity) {
    ...
  }
}


This feels a bit cleaner to me as far as separation of concerns, but if you're only ever going to validate Students it's probably overkill.

Other

You might also consider having validate() return an Optional&lt;Set&lt;ValidationError&gt;&gt; instead of the List, if order isn't important. Since validation errors are just strings, you can't effectively do any further handling of them, so I assume they just get piped out to the user somehow?


  Does it comply with the MVC pattern?


I'm not an MVC expert, but: what is the view supposed to be? You haven't included anything that displays Students or errors, receives input, or otherwise does anything with Students, so it's hard to thoroughly assess this.