Thursday, 22 June 2017

Java 8 new features

The main feature of Java 8, Lambda expressions, also called behaviour parameterization, it allows transferring code into methods. Another new feature, Method-reference gives a succinct and descriptive code.  


Lambda expression 

Mathematically, an expression with parameter variables has been called a Lambda expression.  
for instance: f(x,y)=x+2y

In programming, Lambda function stands for a function defined without a name (identifier). Before Java 8, in order to pass a function, we need to declare a class that implements an interface. By this way, a developer may pass a function wrapped in an object. It is expensive in some sense. 

So Java 8 introduces a lambda expression for cheaply passing a block of code around. Java compiler is becoming smarter and smarter. It may infer parameter types from a functional interface (one method interface). Don't be scared of this new term.  Actually, we have used it for a quite long time, for instance, Comparator or Comparable interfaces.  

As a known functional interface, by using the lambda expression, passing a function around can be greatly simplified. 

The following example, the comparing function can be directly passed as an argument parameter. I think it exactly follows the math definition, f(x,y)=x+2y

package Lambda;

import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

/**
 *
 * @author YNZ
 */
public class UsingLambadaExpression {

    public static void main(String[] args) {
        List users = new ArrayList<>();
        users.add(new User("Mike"));
        users.add(new User("Jeppe"));
        users.add(new User("Yichun"));
        Collections.sort(users, (o1, o2) -> o1.getName().compareTo(o2.getName()));
        System.out.println("" + users);
    }

}

class User {

    private String name;

    public User(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public String toString() {
        return this.name;
    }
}
Note: You can add annotation or final modifier to the lambda parameters. Note: It is not legal for lambda to return a value in a branch. Note: Functional interface contains a single abstract method.

Lambda Expression 3 ingredients:
(1) a block of code; (2) parameters; (3) values for the free variables, which are not defined in the block and transferred by the parameters.

a block of code and values of free values is a closure. Actually, it is implemented as a transferring by an object has a single method, so the free values have been initialized within that instance.

Method Reference 

 is used to simply certain Lambda expression, for instance:
(x->System.out.println(x))
We know there is already existed a method that may exactly operate the parameter as expected. In this case Lambda expression can be greatly simplified by using method reference.
 System.out::println 
Constructor reference, it is just like method reference, but using something like Class::new. It invokes its constructor. It equivalent to the lambda expression,i.e.
 x-> new constructor(x) 
Constructor reference can be used with the array type, i.e. int[]::new, i.e.
 x-> new int[x] 

Friday, 16 June 2017

JEE Bean Validation

Starting from JEE 6, Java starts to offer bean validation annotations, which may constrain property values.

JEE bean validation provides built-in constrain annotations, and a way to build custom-constrains too.

Bean validation annotations define how to validate bean properties. However, the validation need to be triggered from externally.

Or using annotation @Valid before the request or response body.

If a javax.validation.ValidationException or any subclass of ValidationException except ConstraintValidationException is thrown, the JAX-RS runtime will respond to the client request with a 500 (Internal Server Error) HTTP status code.

If a ConstraintValidationException is thrown, the JAX-RS runtime will respond to the client with one of the following HTTP status codes:

500 (Internal Server Error) if the exception was thrown while validating a method return type

400 (Bad Request) in all other cases



@Valid may throw
ConstraintValidationException 


import java.util.Set;
import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author YNZ
 */
public class LoginInfoIT {

    private final Logger logger;

    private static ValidatorFactory validatorFactory;
    private static Validator validator;

    public LoginInfoIT() {
        logger = LoggerFactory.getLogger(this.getClass());
    }

    @BeforeClass
    public static void setUpClass() {
        validatorFactory = Validation.buildDefaultValidatorFactory();
        validator = validatorFactory.getValidator();
    }

    @Test
    public void testEmailConstraintValidation() {
        logger.info("Test Email Pattern Validation! ");
        LoginInfo instance = new LoginInfo("zyc@gmail", "Obhh2017");
        Set> violations = validator.validate(instance);
        assertEquals(1, violations.size());
    }

import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Pattern;
import lombok.Data;

/**
 *
 * @author YNZ
 */
@Embeddable
@Data
public class LoginInfo implements Serializable {

    @Pattern(regexp = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+"
            + "(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$",
            message = " invalid email address! ")
    @NotNull
    @Column(name = "LOGIN_NAME", unique = true)
    protected String email;




Reference:
JEE 7 Bean Validation

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...