Java

Java is a set of computer software and specifications developed by Sun Microsystems, which was later acquired by the Oracle Corporation, that provides a system for developing application software and deploying it in a cross-platform computing environment. Java is used in a wide variety of computing platforms from embedded devices and mobile phones to enterprise servers and supercomputers.

Spring Logo

Spring Framework

The Spring Framework provides a comprehensive programming and configuration model for modern Java-based enterprise applications - on any kind of deployment platform. A key element of Spring is infrastructural support at the application level: Spring focuses on the "plumbing" of enterprise applications so that teams can focus on application-level business logic, without unnecessary ties to specific deployment environments.

Hibernate Logo

Hibernate Framework

Hibernate ORM is an object-relational mapping framework for the Java language. It provides a framework for mapping an object-oriented domain model to a relational database.

Sunday, August 23, 2015

Method References in Java8


Method References:
  • Method references refers to methods or constructors without invoking them.
  • We can use lambda expressions to create anonymous methods. 
  • Sometimes, however, a lambda expression does nothing but call an existing method
  • In those cases, it's often clearer to refer to the existing method by name.
  • Using Method references refer to the existing method by name, they are compact, easy-to-read lambda expressions for methods that already have a name.
  • In java, method references can be specified by using double colon (::) operator.
  • Method reference can be expressed using the lambda expression syntax (->) in order to make it simple :: operator can be used.
Syntax:
           <class_name or instance_name>::<method_name>
Example:
/**
 * @author Ranga Reddy
 * @version 1.0
 */
public class MethodReferenceExample {
    public static void main(String[] args) {
        
        // with out using method reference
        new Thread(
                () -> {System.out.println("Hello, Mr. Ranga"); }
        ) {
            
        }.run();;
        
        // with using method reference
        new Thread(MethodReferenceExample:: sayHello) {
            
        }.run();
        
    }
    
    public static void sayHello() {
        System.out.println("Hello, Mr. Ranga");
    }
}
Types of Method References:
There are four types of method references:

Method Reference Type
Syntax
Example
Reference to a static method
ClassName::staticMethodName
String::valueOf
Reference to a bound non-static method
ObjectName::instanceMethodName
s::toString
Reference to an instance method of an arbitrary object of a particular type
ClassName::instanceMethodName
Object::toString
Reference to a constructor
ClassName::new
String::new
Reference to a static method:
We can call static method references by using ContainingClass::staticMethodName
Example:
public class MethodReferenceExample {
    public static void main(String[] args) {        
        // Reference to a Static Method     
        new Thread(MethodReferenceExample::sayHello).start();
    }       
    public static void sayHello() {     
        System.out.println("Hello, Mr. Ranga");
    }
}
Reference to a bound non-static method:
We can call non-static method using ObjectName:: instanceMethodName
Example:

public class MethodReferenceExample {
    public static void main(String[] args) {
        // Reference to an Instance Method of a Particular Object
        String name = "My name is Ranga Reddy";
        printName(name::toString);      
    }
    
    private static void printName(Supplier<String> supplier) {
        System.out.println(supplier.get());
    }
}
Reference to an instance method of an arbitrary object of a particular type:
We can call non-static method using ClassName:: instanceMethodName

Example:
public class MethodReferenceExample {
    public static void main(String[] args) {
        // Reference to an Instance Method of an Arbitrary Object of a Particular Type
        String[] names = { "Ranga", "Reddy", "Vinod", "Raja", "Manu", "Teja", "Vasu" };     
        Arrays.sort(names, String::compareToIgnoreCase);
        System.out.println(Arrays.toString(names));     
    }
}
Reference to a bound non-static method:
We can call non-static method using ClassName:: new
Example:
@FunctionalInterface
interface EmployeeFactory {
    Employee getEmployee(long id, String name, Integer age);
}

class Employee {
    private long id;
    private String name;
    private int age;
        
    public Employee() {     
        this(1, "Ranga", 27);
    }
    
    public Employee(long id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "Employee [id=" + id + ", name=" + name + ", age=" + age + "]";
    }   
}

public class MethodReferenceExample {
    public static void main(String[] args) {                
        // Reference to constructor
        EmployeeFactory employeeFactory = Employee :: new;
        Employee employee = employeeFactory.getEmployee(1, "Ranga Reddy",27);
        System.out.println(employee);       
    }
}
When to use method references:
When a lambda expression is invoking already defined method, then we can replace it with reference to that method.
When we can't use method references:
We can't pass arguments to the method references. So that places we can't use method references.
References:
  1. https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html
  2. http://java8.in/java-8-method-references/
  3. http://www.informit.com/articles/article.aspx?p=2191424

Write a Java Program to convert String value into Integer value with out using parseInt() method.


In this post, we are going to learn how to convert string value into integer value with out using the parseInt() method.

package com.ranga;

/**
*
* This class is used to convert the String value into Integer.
* @author Ranga Reddy
* @version 1.0
*
*/

public class StringToInteger {

public static void main(String[] args) {
String string = "123456";
System.out.println("String value: "+ string);
int integerValue = convertStringToInteger(string);
System.out.println("Integer value: "+ integerValue);
}

/**
* Used to convert the String to Integer.
* @param str - the value of a string value
* @return the integer value.
*/

public static int convertStringToInteger( String str ) {
if(str == null)
throw new NullPointerException("Value is "+str);

int i = 0, intValue = 0;
boolean isNegativeValue = false;
int strLength = str.length();
// checking the first character is negative or not. if it is negative then i start with 1.
if( str.charAt(0) == '-' ){
isNegativeValue = true;
i = 1;
}

for(;i<strLength; i++) {
int value = str.charAt(i) - '0';
if( value > -1 && value < 10 ) {
intValue *= 10;
intValue += value;
} else {
throw new NumberFormatException("Invalid integer string. Value is "+str);
}
}

// if value is negative then adding the prefix is -
if( isNegativeValue ) {
intValue = -intValue;
}
return intValue;
}
}

Input 1: 
String string = null;
Output 1:
Exception in thread "main" java.lang.NumberFormatException: null value can't be convert into integer.
Explanation: If the string value is null then throwing the NumberFormatException.

Input 2:
String  string = "143";
Output 1:
143
Explanation: 
Initial values of 
int i = 0;
int intValue = 0;
boolean isNegativeValue = false;
int strLength = str.length() = 3;

Then checking the string 0th position value will be -ve or not because number can be positive or negative.
if(str.charAt(0) == '-') {

}

If it is true then number start position will be 1 on words. so now i value will be. And also make isNegativeValue value will be true. In the above case 0th position is not negative value. so i value start with 0 only.
then get the each position value and multiply with 10 and add into the end of integer value.

i =0
value = str.charAt(i) - '0' = str.charAt(0) - '0' = 49 - 48 = 1
intValue = 0 * 10 = 10
intValue = intValue +  value = 0 + 1 = 1

Note: str.charAt(1) returns the ASCII value of 1. ASCII value of 1 is 49 and then substracting the ASCII value of 0 is 48

i = 1
value = str.charAt(1) = 52 - 48 = 4
intValue = 1 * 10 = 10
intValue = 10 + 4 = 14

i = 2
value = str.charAt(2) = 51 - 48 = 3
intValue = 14 * 10 = 140
intValue = 140 + 3 = 143

Note: value should be always between 0 to 9 only. If it not then throwing the NumberFormatException








Thursday, August 20, 2015

RESTful API using Spring Boot and Cassandra

In this post, we are going to learn how to implement RESTful web services using Spring Boot and Cassandra db.

Project Structure:

Tools & Technologies:
  • Java 8
  • Maven 3.0.3
  • Spring Boot 1.2.5.RELEASE
  • Cassandra Database 2.1.8
  • Spring data cassandra 1.2.2.RELEASE
Cassandra Keyspace and Table Creation:
cqlsh> create keyspace "ranga" with replication = {'class': 'SimpleStrategy', 'replication_factor': '1'};
cqlsh:system_traces> use ranga;
cqlsh:ranga> CREATE TABLE employee(id bigint PRIMARY KEY, name text, age int, salary float);
cqlsh:system_traces> INSERT INTO employee(id, name, age, salary) VALUES(1, 'Ranga', 27, 30000);
cqlsh:ranga> select * from employee;

id | age | name | salary
----+-----+-------+--------
1 | 27 | Ranga | 30000
Maven Configuration
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<groupId>com.ranga</groupId>
<artifactId>SpringBootRestAPICassandraExample</artifactId>
<version>1.0.0-SNAPSHOT</version>

<!-- Inherit defaults from Spring Boot -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>

<dependencies>
<!-- Get the dependencies of a web application -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- Spring Data Cassandra -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-cassandra</artifactId>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

</project>

Configuring the Cassandra DB properties
src/main/resources/cassandra.properties
cassandra.contactpoints=localhost,127.0.0.1
cassandra.port=9042
cassandra.keyspace=ranga
Utility class for Cassandra db operations:
CassandraUtil.java
package com.ranga.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.data.cassandra.config.CassandraClusterFactoryBean;
import org.springframework.data.cassandra.config.CassandraSessionFactoryBean;
import org.springframework.data.cassandra.config.SchemaAction;
import org.springframework.data.cassandra.convert.CassandraConverter;
import org.springframework.data.cassandra.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;
import org.springframework.data.cassandra.mapping.BasicCassandraMappingContext;
import org.springframework.data.cassandra.mapping.CassandraMappingContext;

/**
* Utility class for getting the CassandraOperations object.
* @author Ranga Reddy
* @version 1.0
*/


@Configuration
@PropertySource(value = { "classpath:cassandra.properties" })
public class CassandraUtil {

/**
* Constant String for Keyspace
*/

private static final String KEYSPACE = "cassandra.keyspace";
/**
* Constant String for ContactPoints
*/

private static final String CONTACTPOINTS = "cassandra.contactpoints";
/**
* Constant String for Port
*/

private static final String PORT = "cassandra.port";

@Autowired
private Environment environment;

public CassandraUtil() {
System.out.println("CassandraUtil()");
}

private String getKeyspaceName() {
return environment.getProperty(KEYSPACE);
}

private String getContactPoints() {
return environment
.getProperty(CONTACTPOINTS);
}

private int getPortNumber() {
return Integer.parseInt(environment
.getProperty(PORT));
}

@Bean
public CassandraClusterFactoryBean cluster() {
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setContactPoints(getContactPoints());
cluster.setPort(getPortNumber());
return cluster;
}

@Bean
public CassandraMappingContext mappingContext() {
return new BasicCassandraMappingContext();
}

@Bean
public CassandraConverter converter() {
return new MappingCassandraConverter(mappingContext());
}

@Bean
public CassandraSessionFactoryBean session() throws Exception {
CassandraSessionFactoryBean cassandraSessionFactoryBean = new CassandraSessionFactoryBean();
cassandraSessionFactoryBean.setCluster(cluster().getObject());
cassandraSessionFactoryBean.setKeyspaceName(getKeyspaceName());
cassandraSessionFactoryBean.setConverter(converter());
cassandraSessionFactoryBean.setSchemaAction(SchemaAction.NONE);
return cassandraSessionFactoryBean;
}

@Bean
public CassandraOperations cassandraTemplate() throws Exception {
return new CassandraTemplate(session().getObject());
}
}
MyCassandraTemplate.java
package com.ranga.util;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.stereotype.Repository;

/**
* Utility class for handling all CRUD Operations.
* @author Ranga Reddy
* @version 1.0
*/

@Repository
public class MyCassandraTemplate {

@Autowired
private CassandraOperations cassandraTemplate;

/** MyCassandraTemplate Default constructor*/
public MyCassandraTemplate() {
System.out.println("MyCassandraTemplate()");
}

/**
* Creating the entity.
* @param entity
* @return {@link Object}
*/

public <T> T create(T entity) {
return cassandraTemplate.insert(entity);
}

/**
* Creating the list of entities.
* @param entity
*/

public <T> void createList(List<T> entities) {
cassandraTemplate.insert(entities);
}

/**
* Updating the entity.
* @param entity
* @param claz
* @return T
*/

public <T> T update(T entity) {
return (T) cassandraTemplate.update(entity);
}

/**
* Updating the list of entities.
* @param entity
* @param claz
* @return T
*/

public <T> void updateList(List<T> entities) {
cassandraTemplate.update(entities);
}

/**
* Updating the entity.
* @param entity
* @param claz
* @return T
*/

public <T> T update(T entity, Class<T> claz) {
return (T) cassandraTemplate.update(entity);
}

/**
* Get the Entity using Id.
* @param id
* @param claz
* @return T
*/

public <T> T findById(Object id, Class<T> claz) {
return cassandraTemplate.selectOneById(claz, id);
}

/**
* Delete the Entity using Id.
* @param id
* @param claz
*/

public <T> void deleteById(Object id, Class<T> claz) {
cassandraTemplate.deleteById(claz, id);
}

/**
* Delete the Entity using object.
* @param entity
*/

public void delete(Object entity) {
cassandraTemplate.delete(entity);
}

/**
* Deleting the list of entities
* @param entities
*/

public <T> void delete(List<T> entities) {
cassandraTemplate.delete(entities);
}

/**
* Deleting the all entities.
* @param claz
*/

public <T> void deleteAll(Class<T> claz) {
cassandraTemplate.deleteAll(claz);
}

/**
* Getting the all entities.
* @param claz
* @return List of entities
*/

public <T> List<T> findAll(Class<T> claz) {
return (List<T>) cassandraTemplate.selectAll(claz);
}

/**
* Getting the all entity values using specific id's data.
* @param ids
* @param claz
* @return
*/

public <T> List<T> findAll(List<Object> ids, Class<T> claz) {
return cassandraTemplate.selectBySimpleIds(claz, ids);
}

/**
* Getting the count of records.
* @param claz
* @return the count value.
*/

public <T> void truncate(Class<T> claz) {
cassandraTemplate.truncate(claz.getName());
}

/**
* Getting the count of records.
* @param claz
* @return the count value.
*/

public <T> long getCount(Class<T> claz) {
return cassandraTemplate.count(claz);
}


/**
* Checking the object exists or not.
* @param id
* @param claz
* @return true if the object exists in the database otherwise it will return false.
*/

public <T> boolean exists(Object id, Class<T> claz) {
return cassandraTemplate.exists(claz, id);
}

}
Employee entity:
Employee.java
package com.ranga.entity;

import org.springframework.data.cassandra.mapping.Column;
import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;

/**
* Employee entity class.
*
* @author Ranga Reddy
* @version 1.0
*/

@Table("employee")
public class Employee {

@PrimaryKey("id")
private long id;

@Column("name")
private String name;

@Column
private int age;

@Column(value ="salary")
private float salary;

/**
* Default Constructor
*/

public Employee() {
super();
}

/**
* Parameterized Constructor
* @param id
* @param name
* @param age
* @param salary
*/

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

/**
* @return the id
*/

public long getId() {
return id;
}

/**
* @param id the id to set
*/

public void setId(long id) {
this.id = id;
}

/**
* @return the name
*/

public String getName() {
return name;
}

/**
* @param name the name to set
*/

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

/**
* @return the age
*/

public int getAge() {
return age;
}

/**
* @param age the age to set
*/

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

/**
* @return the salary
*/

public float getSalary() {
return salary;
}

/**
* @param salary the salary to set
*/

public void setSalary(float salary) {
this.salary = salary;
}

/* (non-Javadoc)
* @see java.lang.Object#toString()
*/

@Override
public String toString() {
return "Employee [id=" + id + ", name=" + name + ", age=" + age
+ ", salary=" + salary + "]";
}
}
Employee DAO & Impl classes;
EmployeeDAO.java
package com.ranga.dao;

import java.util.List;

import com.ranga.entity.Employee;

/**
* DAO interface for Employee to perform CRUD operation.
* @author Ranga Reddy
* @version 1.0
*/

public interface EmployeeDAO {
/**
* Used to Create the Employee Information
* @param employee
* @return {@link Employee}
*/

public Employee createEmployee(Employee employee);

/**
* Getting the Employee Information using Id
* @param id
* @return {@link Employee}
*/

public Employee getEmployee(int id);

/**
* Used to Update the Employee Information
* @param employee
* @return {@link Employee}
*/


public Employee updateEmployee(Employee employee);

/**
* Deleting the Employee Information using Id
* @param id
*/

public void deleteEmployee(int id);

/**
* Getting the All Employees information
* @return
*/

public List<Employee> getAllEmployees();
}
EmployeeDAOImpl.java
/**
*
*/

package com.ranga.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import com.ranga.dao.EmployeeDAO;
import com.ranga.entity.Employee;
import com.ranga.util.MyCassandraTemplate;

/**
* DAOImpl class for Employee to perform CRUD operation.
* @author Ranga Reddy
* @version 1.0
*/

@Repository
public class EmployeeDAOImpl implements EmployeeDAO {

@Autowired
private MyCassandraTemplate myCassandraTemplate;

@Override
public Employee createEmployee(Employee employee) {
return myCassandraTemplate.create(employee);
}

@Override
public Employee getEmployee(int id) {
return myCassandraTemplate.findById(id, Employee.class);
}

@Override
public Employee updateEmployee(Employee employee) {
return myCassandraTemplate.update(employee, Employee.class);
}

@Override
public void deleteEmployee(int id) {
myCassandraTemplate.deleteById(id, Employee.class);
}

@Override
public List<Employee> getAllEmployees() {
return myCassandraTemplate.findAll(Employee.class);
}
}
Employee Service & Impl classes
EmployeeService.java
package com.ranga.service;

import java.util.List;

import com.ranga.entity.Employee;

/**
* Service interface for Employee to perform CRUD operation.
* @author Ranga Reddy
* @version 1.0
*/

public interface EmployeeService {
/**
* Used to Create the Employee Information
* @param employee
* @return {@link Employee}
*/

public Employee createEmployee(Employee employee);

/**
* Getting the Employee Information using Id
* @param id
* @return {@link Employee}
*/

public Employee getEmployee(int id);

/**
* Used to Update the Employee Information
* @param employee
* @return {@link Employee}
*/


public Employee updateEmployee(Employee employee);

/**
* Deleting the Employee Information using Id
* @param id
*/

public void deleteEmployee(int id);

/**
* Getting the All Employees information
* @return
*/

public List<Employee> getAllEmployees();
}
EmployeeServiceImpl.java
package com.ranga.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ranga.dao.EmployeeDAO;
import com.ranga.entity.Employee;
import com.ranga.service.EmployeeService;

/**
* Service Impl class for Employee to perform CRUD operation.
* @author Ranga Reddy
* @version 1.0
*/

@Service
public class EmployeeServiceImpl implements EmployeeService {

@Autowired
private EmployeeDAO employeeDAO;

/**
* Default Constructor
*/

public EmployeeServiceImpl() {
super();
}

@Override
public Employee createEmployee(Employee employee) {
return employeeDAO.createEmployee(employee);
}

@Override
public Employee getEmployee(int id) {
return employeeDAO.getEmployee(id);
}

@Override
public Employee updateEmployee(Employee employee) {
return employeeDAO.updateEmployee(employee);
}

@Override
public void deleteEmployee(int id) {
employeeDAO.deleteEmployee(id);
}

@Override
public List<Employee> getAllEmployees() {
return employeeDAO.getAllEmployees();
}

}
Employee Rest Controller class:
EmployeeController.java
package com.ranga.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.ranga.entity.Employee;
import com.ranga.service.EmployeeService;

/**
* @author Ranga Reddy
* @version 1.0
* @since Aug 20, 2015
*/

@RestController
public class EmployeeController {

@Autowired
private EmployeeService employeeService;

public EmployeeController() {
System.out.println("EmployeeController()");
}

@RequestMapping(value = "/employee", method = RequestMethod.POST)
Employee create(@RequestBody Employee employee) {
return employeeService.createEmployee(employee);
}

@RequestMapping(value = "/employee/{id}", method = RequestMethod.DELETE)
void delete(@PathVariable("id") int id) {
employeeService.deleteEmployee(id);
}

@RequestMapping(value="/employee", method = RequestMethod.GET)
List<Employee> findAll() {
return employeeService.getAllEmployees();
}

@RequestMapping(value = "/employee/{id}", method = RequestMethod.GET)
Employee findById(@PathVariable("id") int id) {
return employeeService.getEmployee(id);
}

@RequestMapping(value = "/employee", method = RequestMethod.PUT)
Employee update(@RequestBody Employee employee) {
return employeeService.updateEmployee(employee);
}
}
Spring Boot Application class:
Application.java
package com.ranga.main;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

/**
* Main application to run boot application.
*
* @author Ranga Reddy
* @version 1.0
*/

@SpringBootApplication
@ComponentScan(basePackages="com.ranga")
public class Application {
public static void main(String[] args) throws Exception {
SpringApplication.run(Application.class, args);
}
}
Run the Application.java file.
For testing purpose, i used SOAP UI

Reading All:

Creating:

Reading Specific:

Updating:

Deleting: