Showing posts with label empty. Show all posts
Showing posts with label empty. Show all posts

Tuesday, 6 April 2021

Bean Validation: NotEmpty: Element must not be null or empty

@NotEmpty annotation can be applied on any CharSequence, Collection, Map and array, it makes sure that element must not be null and empty.

 

Example

@NotEmpty

private String name;

 

Where can I apply this annotation?

a.   METHOD

b.   FIELD

c.    ANNOTATION_TYPE

d.   CONSTRUCTOR

e.   PARAMETER

f.     TYPE_USE

 

Employee.java

package com.sample.app.model;

import javax.validation.constraints.NotEmpty;

public class Employee {

	private int id;

	@NotEmpty
	private String name;

	private int age;

	public Employee(int id, String name, int age) {
		this.id = id;
		this.name = name;
		this.age = age;
	}

	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 int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

}

 

Test.java

package com.sample.app;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import com.sample.app.model.Employee;

public class Test {
	private static ValidatorFactory validatorFactory = Validation.buildDefaultValidatorFactory();
	private static Validator validator = validatorFactory.getValidator();

	private static void validateBean(Employee emp) {
		System.out.println("************************************");
		Set<ConstraintViolation<Employee>> validationErrors = validator.validate(emp);

		if (validationErrors.size() == 0) {
			System.out.println("No validation errors....");
		}

		for (ConstraintViolation<Employee> violation : validationErrors) {
			System.out.println(violation.getPropertyPath() + "," + violation.getMessage());
		}
		System.out.println("");
	}

	public static void main(String args[]) {

		Employee emp1 = new Employee(1, null, 28);
		System.out.println("Validation errors on bean emp1");
		validateBean(emp1);

		Employee emp2 = new Employee(2, "", 59);
		System.out.println("Validation errors on bean emp2");
		validateBean(emp2);
		
		Employee emp3 = new Employee(3, " ", 9);
		System.out.println("Validation errors on bean emp3");
		validateBean(emp3);
		
		Employee emp4 = new Employee(4, "Sailaja", 24);
		System.out.println("Validation errors on bean emp4");
		validateBean(emp4);
	}
}

 

Output

Validation errors on bean emp1
************************************
name,must not be empty

Validation errors on bean emp2
************************************
name,must not be empty

Validation errors on bean emp3
************************************
No validation errors....

Validation errors on bean emp4
************************************
No validation errors....

 


  

Previous                                                    Next                                                    Home

Monday, 29 March 2021

Php: Emptiness

In Php "", "0", 0, 0.0, null, false, array() are considerd as empty.

 

How can I check the emptiness of a variable?

You can check the emptiness of a variable using empty() function.

 

emptiness_demo.php

#!/usr/bin/php

<?php
    $var1 = "";
    $var2 = '';
    $var3 = 0;
    $var4 = 0.0;
    $var5 = '0';
    $var6 = '0.0';
    $var7 = null;
    $var8 = false;
    $var9 = array();
    $var10 = [];
    $var11 = 1;

    echo (empty($var1)?'var1 is empty': "\$var1 : $var1")."\n";
    echo (empty($var2)?'var2 is empty': "\$var2 : $var2")."\n";
    echo (empty($var3)?'var3 is empty': "\$var3 : $var3")."\n";
    echo (empty($var4)?'var4 is empty': "\$var4 : $var4")."\n";
    echo (empty($var5)?'var5 is empty': "\$var5 : $var5")."\n";
    echo (empty($var6)?'var6 is empty': "\$var6 : $var6")."\n";
    echo (empty($var7)?'var7 is empty': "\$var7 : $var7")."\n";
    echo (empty($var8)?'var8 is empty': "\$var8 : $var8")."\n";
    echo (empty($var9)?'var9 is empty': "\$var9 : $var9")."\n";
    echo (empty($var10)?'var10 is empty': "\$var10 : $var10")."\n";
    echo (empty($var11)?'var11 is empty': "\$var11 : $var11")."\n";
?>

 

Output

$./emptiness_demo.php 

var1 is empty
var2 is empty
var3 is empty
var4 is empty
var5 is empty
$var6 : 0.0
var7 is empty
var8 is empty
var9 is empty
var10 is empty
$var11 : 1

 

 

 

  

Previous                                                    Next                                                    Home

Tuesday, 21 April 2020

How to check whether a map is null or empty?

Following snippet return true if the map is null or empty.

public static boolean isMapNullOrEmpty(Map<?, ?> map) {
         return map == null || map.isEmpty();
}

App.java
package com.sample.app;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class App {

	public static boolean isMapNullOrEmpty(Map<?, ?> map) {
		return map == null || map.isEmpty();
	}

	public static void main(String args[]) {
		System.out.println("Is map empty : " + isMapNullOrEmpty(null));
		System.out.println("Is map empty : " + isMapNullOrEmpty(Collections.EMPTY_MAP));

		Map<Integer, String> map = new HashMap<>();
		map.put(1, "Krishna");

		System.out.println("Is map empty : " + isMapNullOrEmpty(map));
	}

}

Output
Is map empty : true
Is map empty : true
Is map empty : false

You may like

Monday, 20 April 2020

How to check whether a collection is null or empty?

Below snippet return true if the collection is null or empty.

public static boolean isCollectionNullOrEmpty(Collection<?> collection) {
         return collection == null || collection.isEmpty();
}

App.java
package com.sample.app;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;

public class App {

	public static boolean isCollectionNullOrEmpty(Collection<?> collection) {
		return collection == null || collection.isEmpty();
	}

	public static void main(String args[]) {
		System.out.println("Is list empty : " + isCollectionNullOrEmpty(null));
		System.out.println("Is list empty : " + isCollectionNullOrEmpty(Collections.EMPTY_LIST));
		System.out.println("Is list empty : " + isCollectionNullOrEmpty(Arrays.asList(2, 3, 5)));
	}

}

Output
Is list empty : true
Is list empty : true
Is list empty : false


You may like