Search code examples
javalambdafunctional-interface

Java Lambda with function interface


I am new to java and working on lambda functions with the below requirements. Can someone help me with this. Create a public class ValidateUtility with the below methods :

public static Validate validateEmployeeName() – The lambda expression for the validateName method must return true if the name is valid and return false if the name is invalid.

In this case, the name is valid if it contains alphabets and space and it should contain minimum 5 characters and maximum 20 characters.

public static Validate validateProductName() – The lambda expression for the validateName method must return true if the name is valid and return false if the name is invalid.

In this case, the name is valid if the first character is an alphabet followed by 5 digits.

Given Functional interface

 public interface Validate{
    //write the abstract method 
    public boolean validateName(String name);
}

And empty main class.


Solution

  • One way to implement static methods that return lambda expressions (or anonymous methods) as validators would be as below. This implementation uses Regex but may be changed to any other implementations.

    public class ValidateUtility {
    
        @FunctionalInterface
        public interface Validate {
            public boolean validateName(String name);
        }
    
        /**
         * Validates a given string (employee name) using
         * 
         * @return true if the string contains alphabets [a-zA-Z] and (white) space(s),
         *         and has atleast 5 and atmost 20 characters, and false otherwise.
         */
        public static Validate validateEmployeeName() {
            return employeeName -> employeeName.matches("[a-zA-Z\s]{5,20}");
        }
    
        /**
         * Validates a given string (product name) using *
         * 
         * @return true if the string contains an alphabet [a-zA-Z] followed by 5 digits
         *         [0-9], and false otherwise.
         */
        public static Validate validateProductName() {
            return productName -> productName.matches("[a-zA-Z]{1}[\\d]{5}");
        }
    
        public static void main(String[] args) {
            boolean isValidEmployeeName = ValidateUtility.validateEmployeeName().validateName("Bla Bla");
            System.out.println(isValidEmployeeName);
    
            boolean isValidProductName = ValidateUtility.validateProductName().validateName("X12345");
            System.out.println(isValidProductName);
        } }
    

    However, it is preferable to use suitable functional interfaces that Java already provides, and in this case Predicate which can be implemented as following.

        final Predicate<String> employeeNameValidatorPredicate = 
            employeeName -> employeeName.matches("[a-zA-Z\s]{5,20}");
        
        final Predicate<String> productNameValidatorPredicate = 
            productName -> productName.matches("[a-zA-Z]{1}[\\d]{5}");
    
        boolean isValidEmployeeName = employeeNameValidatorPredicate.test("Bla Bla");
        System.out.println(isValidEmployeeName);
    
        boolean isValidProductName = productNameValidatorPredicate.test("X12345");
        System.out.println(isValidProductName);