From 140de5b0b22d0c79a9c9779234cf3b6811cd905d Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Mon, 22 May 2017 17:35:18 +0530 Subject: [PATCH 01/10] Spring Data Injection Demo Spring Data Injection Demo using annotations --- SpringDataInjectionDemo/pom.xml | 44 ++++++++++++++++++ .../SpringDataInjectionDemoApplication.java | 12 +++++ .../didemo/controller/OrderController.java | 45 +++++++++++++++++++ .../java/com/baeldung/didemo/model/Order.java | 26 +++++++++++ .../service/CustomerServiceConstructorDI.java | 25 +++++++++++ ...omerServiceConstructorWithoutSpringDI.java | 20 +++++++++ .../service/CustomerServiceFieldDI.java | 19 ++++++++ .../service/CustomerServiceSetterDI.java | 24 ++++++++++ .../baeldung/didemo/service/OrderService.java | 30 +++++++++++++ .../src/main/resources/application.properties | 0 10 files changed, 245 insertions(+) create mode 100644 SpringDataInjectionDemo/pom.xml create mode 100644 SpringDataInjectionDemo/src/main/java/com/baeldung/SpringDataInjectionDemoApplication.java create mode 100644 SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java create mode 100644 SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/model/Order.java create mode 100644 SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorDI.java create mode 100644 SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorWithoutSpringDI.java create mode 100644 SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceFieldDI.java create mode 100644 SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceSetterDI.java create mode 100644 SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/OrderService.java create mode 100644 SpringDataInjectionDemo/src/main/resources/application.properties diff --git a/SpringDataInjectionDemo/pom.xml b/SpringDataInjectionDemo/pom.xml new file mode 100644 index 0000000000..5b70549079 --- /dev/null +++ b/SpringDataInjectionDemo/pom.xml @@ -0,0 +1,44 @@ + + + 4.0.0 + + baeldung + springdatainjectiondemo + 0.0.1-SNAPSHOT + jar + + SpringDataInjectionDemo + Spring Data Injection Demp + + + org.springframework.boot + spring-boot-starter-parent + 1.5.3.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/SpringDataInjectionDemoApplication.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/SpringDataInjectionDemoApplication.java new file mode 100644 index 0000000000..5a166bf9f9 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/SpringDataInjectionDemoApplication.java @@ -0,0 +1,12 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringDataInjectionDemoApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringDataInjectionDemoApplication.class, args); + } +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java new file mode 100644 index 0000000000..f66c1e0928 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java @@ -0,0 +1,45 @@ +package com.baeldung.didemo.controller; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.didemo.model.Order; +import com.baeldung.didemo.service.CustomerServiceConstructorDI; +import com.baeldung.didemo.service.CustomerServiceConstructorWithoutSpringDI; +import com.baeldung.didemo.service.CustomerServiceFieldDI; +import com.baeldung.didemo.service.CustomerServiceSetterDI; +import com.baeldung.didemo.service.OrderService; + +@RestController +@RequestMapping(value = "/orders") +public class OrderController { + + @Autowired + CustomerServiceConstructorDI constructorDI; + + @Autowired + CustomerServiceFieldDI fieldDI; + + @Autowired + CustomerServiceSetterDI setterDI; + + @RequestMapping(method = RequestMethod.GET) + public List getOrdersFieldDI(@RequestParam(required = false) String dIMethod) { + if ("setter".equals(dIMethod)) { + return setterDI.getCustomerOrders(1l); + } else if ("constructor".equals(dIMethod)) { + return constructorDI.getCustomerOrders(1l); + } else if ("field".equals(dIMethod)) { + return fieldDI.getCustomerOrders(1l); + } else { + OrderService orderSvc = new OrderService(); + CustomerServiceConstructorWithoutSpringDI customerSvc = new CustomerServiceConstructorWithoutSpringDI(orderSvc); + return customerSvc.getCustomerOrders(1l); + } + } +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/model/Order.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/model/Order.java new file mode 100644 index 0000000000..9fd3710879 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/model/Order.java @@ -0,0 +1,26 @@ +package com.baeldung.didemo.model; + +public class Order { + + private Integer id; + private String item; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getItem() { + return item; + } + + public void setItem(String item) { + this.item = item; + } + + // other order properties.. + +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorDI.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorDI.java new file mode 100644 index 0000000000..daf551b299 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorDI.java @@ -0,0 +1,25 @@ +package com.baeldung.didemo.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.didemo.model.Order; + +@Service +public class CustomerServiceConstructorDI { + + OrderService orderService; + + @Autowired + public CustomerServiceConstructorDI(OrderService orderService) { + super(); + this.orderService = orderService; + } + + public List getCustomerOrders(Long customerId) { + return orderService.getOrdersForCustomer(customerId); + } + +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorWithoutSpringDI.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorWithoutSpringDI.java new file mode 100644 index 0000000000..d61b9f86cc --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceConstructorWithoutSpringDI.java @@ -0,0 +1,20 @@ +package com.baeldung.didemo.service; + +import java.util.List; + +import com.baeldung.didemo.model.Order; + +public class CustomerServiceConstructorWithoutSpringDI { + + OrderService orderService; + + public CustomerServiceConstructorWithoutSpringDI(OrderService orderService) { + super(); + this.orderService = orderService; + } + + public List getCustomerOrders(Long customerId) { + return orderService.getOrdersForCustomer(customerId); + } + +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceFieldDI.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceFieldDI.java new file mode 100644 index 0000000000..acb22ad529 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceFieldDI.java @@ -0,0 +1,19 @@ +package com.baeldung.didemo.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.didemo.model.Order; + +@Service +public class CustomerServiceFieldDI { + + @Autowired + OrderService orderService; + + public List getCustomerOrders(Long customerId) { + return orderService.getOrdersForCustomer(customerId); + } +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceSetterDI.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceSetterDI.java new file mode 100644 index 0000000000..736a43aba4 --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/CustomerServiceSetterDI.java @@ -0,0 +1,24 @@ +package com.baeldung.didemo.service; + +import java.util.List; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.didemo.model.Order; + +@Service +public class CustomerServiceSetterDI { + + OrderService orderService; + + public List getCustomerOrders(Long customerId) { + return orderService.getOrdersForCustomer(customerId); + } + + @Autowired + public void setOrderService(OrderService orderService) { + this.orderService = orderService; + } + +} diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/OrderService.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/OrderService.java new file mode 100644 index 0000000000..4dc8fd6f5d --- /dev/null +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/service/OrderService.java @@ -0,0 +1,30 @@ +package com.baeldung.didemo.service; + +import java.util.ArrayList; +import java.util.List; + +import org.springframework.stereotype.Service; + +import com.baeldung.didemo.model.Order; + +@Service +public class OrderService { + + public List getOrdersForCustomer(Long id) { + List orders = new ArrayList(); + + Order order1 = new Order(); + order1.setId(1); + order1.setItem("Pizza"); + + Order order2 = new Order(); + order2.setId(1); + order2.setItem("Garlic Bread"); + + orders.add(order1); + orders.add(order2); + + return orders; + } + +} diff --git a/SpringDataInjectionDemo/src/main/resources/application.properties b/SpringDataInjectionDemo/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 From aeb33fe72dc068478fc8333504ad3cb39052dd5a Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Wed, 24 May 2017 16:30:32 +0530 Subject: [PATCH 02/10] Adding test cases Adding test cases --- .gitignore | 4 ++ SpringDataInjectionDemo/.gitignore | 24 +++++++ ...ringDataInjectionDemoApplicationTests.java | 65 +++++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 SpringDataInjectionDemo/.gitignore create mode 100644 SpringDataInjectionDemo/src/test/java/com/baeldung/didemo/SpringDataInjectionDemoApplicationTests.java diff --git a/.gitignore b/.gitignore index 60c38ed8f5..fb88a82371 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,7 @@ spring-openid/src/main/resources/application.properties spring-security-openid/src/main/resources/application.properties spring-all/*.log + +*.jar + +SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties diff --git a/SpringDataInjectionDemo/.gitignore b/SpringDataInjectionDemo/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/SpringDataInjectionDemo/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/SpringDataInjectionDemo/src/test/java/com/baeldung/didemo/SpringDataInjectionDemoApplicationTests.java b/SpringDataInjectionDemo/src/test/java/com/baeldung/didemo/SpringDataInjectionDemoApplicationTests.java new file mode 100644 index 0000000000..23012936c3 --- /dev/null +++ b/SpringDataInjectionDemo/src/test/java/com/baeldung/didemo/SpringDataInjectionDemoApplicationTests.java @@ -0,0 +1,65 @@ +package com.baeldung.didemo; + +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.didemo.model.Order; +import com.baeldung.didemo.service.CustomerServiceConstructorDI; +import com.baeldung.didemo.service.CustomerServiceFieldDI; +import com.baeldung.didemo.service.CustomerServiceSetterDI; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringDataInjectionDemoApplicationTests { + + @Autowired + CustomerServiceConstructorDI constructorDI; + + @Autowired + CustomerServiceFieldDI fieldDI; + + @Autowired + CustomerServiceSetterDI setterDI; + + @Test + public void testConstructorDI() { + List orders = constructorDI.getCustomerOrders(1l); + Assert.assertNotNull(orders); + Assert.assertTrue(orders.size() == 2); + } + + @Test + public void testFieldDI() { + List orders = fieldDI.getCustomerOrders(1l); + Assert.assertNotNull(orders); + Assert.assertTrue(orders.size() == 2); + } + + @Test + public void testSetterDI() { + List orders = setterDI.getCustomerOrders(1l); + Assert.assertNotNull(orders); + Assert.assertTrue(orders.size() == 2); + } + + @Test + public void testCombined() { + List ordersSetter = setterDI.getCustomerOrders(1l); + List ordersConstructor = constructorDI.getCustomerOrders(1l); + List ordersField = fieldDI.getCustomerOrders(1l); + + Assert.assertNotNull(ordersSetter); + Assert.assertNotNull(ordersConstructor); + Assert.assertNotNull(ordersField); + Assert.assertTrue(ordersSetter.size() == 2 && ordersConstructor.size() == ordersSetter.size() && ordersField.size() == ordersSetter.size()); + } + + + +} From 4771dc4a3e850b9ba9b1a83d6f89f4346e82076f Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Wed, 24 May 2017 16:48:12 +0530 Subject: [PATCH 03/10] Follow BDD Naming Convention Follow BDD Naming Convention in test cases --- SpringDataInjectionDemo/pom.xml | 5 +++++ .../com/baeldung/didemo/controller/OrderController.java | 1 + .../didemo/SpringDataInjectionDemoApplicationTests.java | 8 ++++---- 3 files changed, 10 insertions(+), 4 deletions(-) diff --git a/SpringDataInjectionDemo/pom.xml b/SpringDataInjectionDemo/pom.xml index 5b70549079..afb5b985ba 100644 --- a/SpringDataInjectionDemo/pom.xml +++ b/SpringDataInjectionDemo/pom.xml @@ -29,6 +29,11 @@ org.springframework.boot spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + diff --git a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java index f66c1e0928..75a325a6e4 100644 --- a/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java +++ b/SpringDataInjectionDemo/src/main/java/com/baeldung/didemo/controller/OrderController.java @@ -3,6 +3,7 @@ package com.baeldung.didemo.controller; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; diff --git a/SpringDataInjectionDemo/src/test/java/com/baeldung/didemo/SpringDataInjectionDemoApplicationTests.java b/SpringDataInjectionDemo/src/test/java/com/baeldung/didemo/SpringDataInjectionDemoApplicationTests.java index 23012936c3..c8c0859d25 100644 --- a/SpringDataInjectionDemo/src/test/java/com/baeldung/didemo/SpringDataInjectionDemoApplicationTests.java +++ b/SpringDataInjectionDemo/src/test/java/com/baeldung/didemo/SpringDataInjectionDemoApplicationTests.java @@ -28,28 +28,28 @@ public class SpringDataInjectionDemoApplicationTests { CustomerServiceSetterDI setterDI; @Test - public void testConstructorDI() { + public void givenConstructorDI_whenNumberOfOrdersIsTwo_thenCorrect() { List orders = constructorDI.getCustomerOrders(1l); Assert.assertNotNull(orders); Assert.assertTrue(orders.size() == 2); } @Test - public void testFieldDI() { + public void givenFieldDI_whenNumberOfOrdersIsTwo_thenCorrect() { List orders = fieldDI.getCustomerOrders(1l); Assert.assertNotNull(orders); Assert.assertTrue(orders.size() == 2); } @Test - public void testSetterDI() { + public void givenSetterDI_whenNumberOfOrdersIsTwo_thenCorrect() { List orders = setterDI.getCustomerOrders(1l); Assert.assertNotNull(orders); Assert.assertTrue(orders.size() == 2); } @Test - public void testCombined() { + public void givenAllThreeTypesOfDI_whenNumberOfOrdersIsEqualInAll_thenCorrect() { List ordersSetter = setterDI.getCustomerOrders(1l); List ordersConstructor = constructorDI.getCustomerOrders(1l); List ordersField = fieldDI.getCustomerOrders(1l); From 7f80810f5349349b473ec39d56f50875cf8cb9d8 Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Mon, 5 Jun 2017 11:53:02 +0530 Subject: [PATCH 04/10] Adding new Project : Spring check if a property is null Using Java Introspector to check if a field is null in an object --- .gitignore | 4 ++ spring-check-if-a-property-is-null/.gitignore | 24 +++++++ spring-check-if-a-property-is-null/pom.xml | 50 +++++++++++++++ ...ringCheckIfAPropertyIsNullApplication.java | 21 +++++++ .../baeldung/reflection/model/Customer.java | 63 +++++++++++++++++++ .../com/baeldung/reflection/util/Utils.java | 34 ++++++++++ .../src/main/resources/application.properties | 0 ...heckIfAPropertyIsNullApplicationTests.java | 31 +++++++++ 8 files changed, 227 insertions(+) create mode 100644 spring-check-if-a-property-is-null/.gitignore create mode 100644 spring-check-if-a-property-is-null/pom.xml create mode 100644 spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java create mode 100644 spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java create mode 100644 spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java create mode 100644 spring-check-if-a-property-is-null/src/main/resources/application.properties create mode 100644 spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java diff --git a/.gitignore b/.gitignore index fb88a82371..1890e8bd0e 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,7 @@ spring-all/*.log *.jar SpringDataInjectionDemo/.mvn/wrapper/maven-wrapper.properties + +spring-call-getters-using-reflection/.mvn/wrapper/maven-wrapper.properties + +spring-check-if-a-property-is-null/.mvn/wrapper/maven-wrapper.properties diff --git a/spring-check-if-a-property-is-null/.gitignore b/spring-check-if-a-property-is-null/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-check-if-a-property-is-null/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/spring-check-if-a-property-is-null/pom.xml b/spring-check-if-a-property-is-null/pom.xml new file mode 100644 index 0000000000..d92e2a8e9c --- /dev/null +++ b/spring-check-if-a-property-is-null/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + baeldung + spring-check-null + 0.0.1-SNAPSHOT + jar + + spring-check-if-a-property-is-null + Calling getters using Introspector + + + org.springframework.boot + spring-boot-starter-parent + 1.5.3.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java new file mode 100644 index 0000000000..24348a714e --- /dev/null +++ b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java @@ -0,0 +1,21 @@ +package com.baeldung; + +import java.util.List; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.baeldung.reflection.model.Customer; +import com.baeldung.reflection.util.Utils; + +@SpringBootApplication +public class SpringCheckIfAPropertyIsNullApplication { + + public static void main(String[] args) throws Exception { + + Customer customer = new Customer(1, "Himanshu", null, null); + List nullProps = Utils.getNullPropertiesList(customer); + System.out.println(nullProps); + } + + +} diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java new file mode 100644 index 0000000000..d0c6c31dce --- /dev/null +++ b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java @@ -0,0 +1,63 @@ +package com.baeldung.reflection.model; + +/** + * + * @author himanshumantri + * + */ +public class Customer { + + private Integer id; + private String name; + private String emailId; + private Long phoneNumber; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmailId() { + return emailId; + } + + public void setEmailId(String emailId) { + this.emailId = emailId; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Customer [id=").append(id).append(", name=").append(name).append(", emailId=").append(emailId).append(", phoneNumber=") + .append(phoneNumber).append("]"); + return builder.toString(); + } + + public Customer(Integer id, String name, String emailId, Long phoneNumber) { + super(); + this.id = id; + this.name = name; + this.emailId = emailId; + this.phoneNumber = phoneNumber; + } + + public Long getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(Long phoneNumber) { + this.phoneNumber = phoneNumber; + } + +} diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java new file mode 100644 index 0000000000..665717db09 --- /dev/null +++ b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java @@ -0,0 +1,34 @@ +package com.baeldung.reflection.util; + +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.baeldung.reflection.model.Customer; + +public class Utils { + + public static List getNullPropertiesList(Customer customer) throws Exception { + PropertyDescriptor[] propDescArr = Introspector.getBeanInfo(Customer.class, Object.class).getPropertyDescriptors(); + List propDescList = Arrays.asList(propDescArr); + + List nullProps = new ArrayList(); + + propDescList.stream().forEach(p -> { + Method getterMethod = p.getReadMethod(); + try { + if (getterMethod != null && getterMethod.invoke(customer) == null) { + // If the value if null for that field + nullProps.add(p.getName()); + } + } catch (Exception e) { + // Handle the exception + e.printStackTrace(); + } + }); + return nullProps; + } +} diff --git a/spring-check-if-a-property-is-null/src/main/resources/application.properties b/spring-check-if-a-property-is-null/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java b/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java new file mode 100644 index 0000000000..edd009e719 --- /dev/null +++ b/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java @@ -0,0 +1,31 @@ +package com.baeldung; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.reflection.model.Customer; +import com.baeldung.reflection.util.Utils; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringCheckIfAPropertyIsNullApplicationTests { + + @Test + public void givenCustomer_whenAFieldIsNull_thenFieldNameInResult() throws Exception { + Customer customer = new Customer(1, "Himanshu", null, null); + + List result = Utils.getNullPropertiesList(customer); + List expectedFieldNames = Arrays.asList("emailId","phoneNumber"); + + Assert.assertTrue(result.size() == expectedFieldNames.size()); + Assert.assertTrue(result.containsAll(expectedFieldNames)); + + } + +} From bc5e87c7ef6f108f4d5d94ce5b09820b7ef74b4d Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Mon, 5 Jun 2017 18:20:58 +0530 Subject: [PATCH 05/10] Adding new project : Spring check if a property is null Using Java Instrospector --- spring-check-if-a-property-is-null/.gitignore | 24 +++++++ spring-check-if-a-property-is-null/pom.xml | 50 +++++++++++++++ ...ringCheckIfAPropertyIsNullApplication.java | 21 +++++++ .../baeldung/reflection/model/Customer.java | 63 +++++++++++++++++++ .../com/baeldung/reflection/util/Utils.java | 34 ++++++++++ .../src/main/resources/application.properties | 0 ...heckIfAPropertyIsNullApplicationTests.java | 31 +++++++++ 7 files changed, 223 insertions(+) create mode 100644 spring-check-if-a-property-is-null/.gitignore create mode 100644 spring-check-if-a-property-is-null/pom.xml create mode 100644 spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java create mode 100644 spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java create mode 100644 spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java create mode 100644 spring-check-if-a-property-is-null/src/main/resources/application.properties create mode 100644 spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java diff --git a/spring-check-if-a-property-is-null/.gitignore b/spring-check-if-a-property-is-null/.gitignore new file mode 100644 index 0000000000..2af7cefb0a --- /dev/null +++ b/spring-check-if-a-property-is-null/.gitignore @@ -0,0 +1,24 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +nbproject/private/ +build/ +nbbuild/ +dist/ +nbdist/ +.nb-gradle/ \ No newline at end of file diff --git a/spring-check-if-a-property-is-null/pom.xml b/spring-check-if-a-property-is-null/pom.xml new file mode 100644 index 0000000000..d92e2a8e9c --- /dev/null +++ b/spring-check-if-a-property-is-null/pom.xml @@ -0,0 +1,50 @@ + + + 4.0.0 + + baeldung + spring-check-null + 0.0.1-SNAPSHOT + jar + + spring-check-if-a-property-is-null + Calling getters using Introspector + + + org.springframework.boot + spring-boot-starter-parent + 1.5.3.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + + diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java new file mode 100644 index 0000000000..24348a714e --- /dev/null +++ b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java @@ -0,0 +1,21 @@ +package com.baeldung; + +import java.util.List; + +import org.springframework.boot.autoconfigure.SpringBootApplication; + +import com.baeldung.reflection.model.Customer; +import com.baeldung.reflection.util.Utils; + +@SpringBootApplication +public class SpringCheckIfAPropertyIsNullApplication { + + public static void main(String[] args) throws Exception { + + Customer customer = new Customer(1, "Himanshu", null, null); + List nullProps = Utils.getNullPropertiesList(customer); + System.out.println(nullProps); + } + + +} diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java new file mode 100644 index 0000000000..d0c6c31dce --- /dev/null +++ b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java @@ -0,0 +1,63 @@ +package com.baeldung.reflection.model; + +/** + * + * @author himanshumantri + * + */ +public class Customer { + + private Integer id; + private String name; + private String emailId; + private Long phoneNumber; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getEmailId() { + return emailId; + } + + public void setEmailId(String emailId) { + this.emailId = emailId; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Customer [id=").append(id).append(", name=").append(name).append(", emailId=").append(emailId).append(", phoneNumber=") + .append(phoneNumber).append("]"); + return builder.toString(); + } + + public Customer(Integer id, String name, String emailId, Long phoneNumber) { + super(); + this.id = id; + this.name = name; + this.emailId = emailId; + this.phoneNumber = phoneNumber; + } + + public Long getPhoneNumber() { + return phoneNumber; + } + + public void setPhoneNumber(Long phoneNumber) { + this.phoneNumber = phoneNumber; + } + +} diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java new file mode 100644 index 0000000000..665717db09 --- /dev/null +++ b/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java @@ -0,0 +1,34 @@ +package com.baeldung.reflection.util; + +import java.beans.Introspector; +import java.beans.PropertyDescriptor; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +import com.baeldung.reflection.model.Customer; + +public class Utils { + + public static List getNullPropertiesList(Customer customer) throws Exception { + PropertyDescriptor[] propDescArr = Introspector.getBeanInfo(Customer.class, Object.class).getPropertyDescriptors(); + List propDescList = Arrays.asList(propDescArr); + + List nullProps = new ArrayList(); + + propDescList.stream().forEach(p -> { + Method getterMethod = p.getReadMethod(); + try { + if (getterMethod != null && getterMethod.invoke(customer) == null) { + // If the value if null for that field + nullProps.add(p.getName()); + } + } catch (Exception e) { + // Handle the exception + e.printStackTrace(); + } + }); + return nullProps; + } +} diff --git a/spring-check-if-a-property-is-null/src/main/resources/application.properties b/spring-check-if-a-property-is-null/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java b/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java new file mode 100644 index 0000000000..edd009e719 --- /dev/null +++ b/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java @@ -0,0 +1,31 @@ +package com.baeldung; + +import java.util.Arrays; +import java.util.List; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringRunner; + +import com.baeldung.reflection.model.Customer; +import com.baeldung.reflection.util.Utils; + +@RunWith(SpringRunner.class) +@SpringBootTest +public class SpringCheckIfAPropertyIsNullApplicationTests { + + @Test + public void givenCustomer_whenAFieldIsNull_thenFieldNameInResult() throws Exception { + Customer customer = new Customer(1, "Himanshu", null, null); + + List result = Utils.getNullPropertiesList(customer); + List expectedFieldNames = Arrays.asList("emailId","phoneNumber"); + + Assert.assertTrue(result.size() == expectedFieldNames.size()); + Assert.assertTrue(result.containsAll(expectedFieldNames)); + + } + +} From 7cf3afbd79a20485b70696871b2f711ed05e3b44 Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Mon, 5 Jun 2017 21:53:31 +0530 Subject: [PATCH 06/10] Renaming the project Renaming the project --- .../.gitignore | 0 .../pom.xml | 6 +++--- .../main/java/com/baeldung/CallAllGettersApplication.java | 5 +---- .../main/java/com/baeldung/reflection/model/Customer.java | 0 .../src/main/java/com/baeldung/reflection/util/Utils.java | 0 .../src/main/resources/application.properties | 0 .../java/com/baeldung/CallAllGettersApplicationTests.java | 7 +------ 7 files changed, 5 insertions(+), 13 deletions(-) rename {spring-check-if-a-property-is-null => call-all-getters}/.gitignore (100%) rename {spring-check-if-a-property-is-null => call-all-getters}/pom.xml (89%) rename spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java => call-all-getters/src/main/java/com/baeldung/CallAllGettersApplication.java (72%) rename {spring-check-if-a-property-is-null => call-all-getters}/src/main/java/com/baeldung/reflection/model/Customer.java (100%) rename {spring-check-if-a-property-is-null => call-all-getters}/src/main/java/com/baeldung/reflection/util/Utils.java (100%) rename {spring-check-if-a-property-is-null => call-all-getters}/src/main/resources/application.properties (100%) rename spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java => call-all-getters/src/test/java/com/baeldung/CallAllGettersApplicationTests.java (72%) diff --git a/spring-check-if-a-property-is-null/.gitignore b/call-all-getters/.gitignore similarity index 100% rename from spring-check-if-a-property-is-null/.gitignore rename to call-all-getters/.gitignore diff --git a/spring-check-if-a-property-is-null/pom.xml b/call-all-getters/pom.xml similarity index 89% rename from spring-check-if-a-property-is-null/pom.xml rename to call-all-getters/pom.xml index d92e2a8e9c..ed4735a861 100644 --- a/spring-check-if-a-property-is-null/pom.xml +++ b/call-all-getters/pom.xml @@ -4,12 +4,12 @@ 4.0.0 baeldung - spring-check-null + call-all-getters 0.0.1-SNAPSHOT jar - spring-check-if-a-property-is-null - Calling getters using Introspector + call-all-getters + Calling all getters using Introspector org.springframework.boot diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java b/call-all-getters/src/main/java/com/baeldung/CallAllGettersApplication.java similarity index 72% rename from spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java rename to call-all-getters/src/main/java/com/baeldung/CallAllGettersApplication.java index 24348a714e..5c10ed5b55 100644 --- a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/SpringCheckIfAPropertyIsNullApplication.java +++ b/call-all-getters/src/main/java/com/baeldung/CallAllGettersApplication.java @@ -2,13 +2,10 @@ package com.baeldung; import java.util.List; -import org.springframework.boot.autoconfigure.SpringBootApplication; - import com.baeldung.reflection.model.Customer; import com.baeldung.reflection.util.Utils; -@SpringBootApplication -public class SpringCheckIfAPropertyIsNullApplication { +public class CallAllGettersApplication { public static void main(String[] args) throws Exception { diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java b/call-all-getters/src/main/java/com/baeldung/reflection/model/Customer.java similarity index 100% rename from spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/model/Customer.java rename to call-all-getters/src/main/java/com/baeldung/reflection/model/Customer.java diff --git a/spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java b/call-all-getters/src/main/java/com/baeldung/reflection/util/Utils.java similarity index 100% rename from spring-check-if-a-property-is-null/src/main/java/com/baeldung/reflection/util/Utils.java rename to call-all-getters/src/main/java/com/baeldung/reflection/util/Utils.java diff --git a/spring-check-if-a-property-is-null/src/main/resources/application.properties b/call-all-getters/src/main/resources/application.properties similarity index 100% rename from spring-check-if-a-property-is-null/src/main/resources/application.properties rename to call-all-getters/src/main/resources/application.properties diff --git a/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java b/call-all-getters/src/test/java/com/baeldung/CallAllGettersApplicationTests.java similarity index 72% rename from spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java rename to call-all-getters/src/test/java/com/baeldung/CallAllGettersApplicationTests.java index edd009e719..cca49b4359 100644 --- a/spring-check-if-a-property-is-null/src/test/java/com/baeldung/SpringCheckIfAPropertyIsNullApplicationTests.java +++ b/call-all-getters/src/test/java/com/baeldung/CallAllGettersApplicationTests.java @@ -5,16 +5,11 @@ import java.util.List; import org.junit.Assert; import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.test.context.junit4.SpringRunner; import com.baeldung.reflection.model.Customer; import com.baeldung.reflection.util.Utils; -@RunWith(SpringRunner.class) -@SpringBootTest -public class SpringCheckIfAPropertyIsNullApplicationTests { +public class CallAllGettersApplicationTests { @Test public void givenCustomer_whenAFieldIsNull_thenFieldNameInResult() throws Exception { From 5b9a1255814150972ac28ed1dc2d94516010a892 Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Tue, 6 Jun 2017 15:20:48 +0530 Subject: [PATCH 07/10] Move null check logic to separate method Move null check logic to separate method --- .../com/baeldung/reflection/util/Utils.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/call-all-getters/src/main/java/com/baeldung/reflection/util/Utils.java b/call-all-getters/src/main/java/com/baeldung/reflection/util/Utils.java index 665717db09..c9542bbdf2 100644 --- a/call-all-getters/src/main/java/com/baeldung/reflection/util/Utils.java +++ b/call-all-getters/src/main/java/com/baeldung/reflection/util/Utils.java @@ -3,9 +3,10 @@ package com.baeldung.reflection.util; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; -import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import java.util.function.Predicate; +import java.util.stream.Collectors; import com.baeldung.reflection.model.Customer; @@ -15,20 +16,28 @@ public class Utils { PropertyDescriptor[] propDescArr = Introspector.getBeanInfo(Customer.class, Object.class).getPropertyDescriptors(); List propDescList = Arrays.asList(propDescArr); - List nullProps = new ArrayList(); - - propDescList.stream().forEach(p -> { - Method getterMethod = p.getReadMethod(); - try { - if (getterMethod != null && getterMethod.invoke(customer) == null) { - // If the value if null for that field - nullProps.add(p.getName()); - } - } catch (Exception e) { - // Handle the exception - e.printStackTrace(); - } - }); + List nullProps = propDescList.stream() + .filter(nulls(customer)) + .map(PropertyDescriptor::getName) + .collect(Collectors.toList()); return nullProps; } + + private static Predicate nulls(Customer customer) { + Predicate isNull = new Predicate() { + @Override + public boolean test(PropertyDescriptor pd) { + Method getterMethod = pd.getReadMethod(); + boolean result = false; + try { + result = (getterMethod != null && getterMethod.invoke(customer) == null); + } catch (Exception e) { + // Handle the exception + e.printStackTrace(); + } + return result; + } + }; + return isNull; + } } From e5dc539a4ffde5dc3b9ec45fac10664e0e7a2100 Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Tue, 20 Jun 2017 23:40:18 +0530 Subject: [PATCH 08/10] Adding collection utils guide Adding Apache Commons 4 Collection Utils guide --- collectionutils/pom.xml | 33 +++++ .../collectionutilsguide/model/Address.java | 47 ++++++ .../collectionutilsguide/model/Customer.java | 118 +++++++++++++++ .../CollectionUtilsGuideTest.java | 135 ++++++++++++++++++ 4 files changed, 333 insertions(+) create mode 100644 collectionutils/pom.xml create mode 100644 collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java create mode 100644 collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java create mode 100644 collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java diff --git a/collectionutils/pom.xml b/collectionutils/pom.xml new file mode 100644 index 0000000000..6d05375efe --- /dev/null +++ b/collectionutils/pom.xml @@ -0,0 +1,33 @@ + + 4.0.0 + baeldung + collectionutils + 0.0.1-SNAPSHOT + collectionutils-guide + A guide to Apache Commons CollectionUtils + + + UTF-8 + UTF-8 + 1.8 + + + + + + org.apache.commons + commons-collections4 + 4.0 + + + + + junit + junit + 4.12 + test + + + + + \ No newline at end of file diff --git a/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java new file mode 100644 index 0000000000..da700ccd0f --- /dev/null +++ b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java @@ -0,0 +1,47 @@ +package com.baeldung.collectionutilsguide.model; + +public class Address { + + String locality; + String city; + String zip; + + public String getLocality() { + return locality; + } + + public void setLocality(String locality) { + this.locality = locality; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getZip() { + return zip; + } + + public void setZip(String zip) { + this.zip = zip; + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Address [locality=").append(locality).append(", city=").append(city).append(", zip=").append(zip).append("]"); + return builder.toString(); + } + + public Address(String locality, String city, String zip) { + super(); + this.locality = locality; + this.city = city; + this.zip = zip; + } + +} diff --git a/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java new file mode 100644 index 0000000000..2c7aa9aa12 --- /dev/null +++ b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java @@ -0,0 +1,118 @@ +package com.baeldung.collectionutilsguide.model; + +public class Customer implements Comparable { + + Integer id; + String name; + Long phone; + String locality; + String city; + String zip; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getPhone() { + return phone; + } + + public void setPhone(Long phone) { + this.phone = phone; + } + + public String getLocality() { + return locality; + } + + public void setLocality(String locality) { + this.locality = locality; + } + + public String getCity() { + return city; + } + + public void setCity(String city) { + this.city = city; + } + + public String getZip() { + return zip; + } + + public void setZip(String zip) { + this.zip = zip; + } + + public Customer(Integer id, String name, Long phone, String locality, String city, String zip) { + super(); + this.id = id; + this.name = name; + this.phone = phone; + this.locality = locality; + this.city = city; + this.zip = zip; + } + + public Customer(Integer id, String name, Long phone) { + super(); + this.id = id; + this.name = name; + this.phone = phone; + } + + public Customer(String name) { + super(); + this.name = name; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((name == null) ? 0 : name.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + Customer other = (Customer) obj; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } + + public int compareTo(Customer o) { + return this.name.compareTo(o.getName()); + } + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("Customer [id=").append(id).append(", name=").append(name).append(", phone=").append(phone).append("]"); + return builder.toString(); + } + +} diff --git a/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java b/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java new file mode 100644 index 0000000000..0913a56c9d --- /dev/null +++ b/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java @@ -0,0 +1,135 @@ +package collectionutils; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.LinkedList; +import java.util.List; + +import org.apache.commons.collections4.Closure; +import org.apache.commons.collections4.CollectionUtils; +import org.apache.commons.collections4.Predicate; +import org.apache.commons.collections4.Transformer; +import org.junit.Test; + +import com.baeldung.collectionutilsguide.model.Address; +import com.baeldung.collectionutilsguide.model.Customer; + +public class CollectionUtilsGuideTest { + + Customer customer1 = new Customer(1, "Daniel", 123456l, "locality1", "city1", "1234"); + Customer customer2 = new Customer(2, "Fredrik", 234567l, "locality2", "city2", "2345"); + Customer customer3 = new Customer(3, "Kyle", 345678l, "locality3", "city3", "3456"); + Customer customer4 = new Customer(4, "Bob", 456789l, "locality4", "city4", "4567"); + Customer customer5 = new Customer(5, "Cat", 567890l, "locality5", "city5", "5678"); + Customer customer6 = new Customer(6, "John", 678901l, "locality6", "city6", "6789"); + + List list1 = Arrays.asList(customer1, customer2, customer3); + List list2 = Arrays.asList(customer4, customer5, customer6); + List list3 = Arrays.asList(customer1, customer2); + + List linkedList1 = new LinkedList(list1); + + @Test + public void givenList_WhenAddIgnoreNull_thenNoNullAdded() { + List list = Arrays.asList("x", "y", "z"); + CollectionUtils.addIgnoreNull(list, null); + assertFalse(list.contains(null)); + } + + @Test + public void givenTwoSortedLists_WhenCollated_thenSorted() { + List sortedList = CollectionUtils.collate(list1, list2); + assertTrue(sortedList.get(0).getName().equals("Bob")); + assertTrue(sortedList.get(2).getName().equals("Daniel")); + } + + @Test + public void givenListOfCustomers_whenTransformed_thenListOfAddress() { + Collection
addressCol = CollectionUtils.collect(list1, new Transformer() { + public Address transform(Customer customer) { + return new Address(customer.getLocality(), customer.getCity(), customer.getZip()); + } + }); + + List
addressList = new ArrayList
(addressCol); + assertTrue(addressList.size() == 3); + assertTrue(addressList.get(0).getLocality().equals("locality1")); + } + + @Test + public void givenCustomerList_WhenCountMatches_thenCorrect() { + int result = CollectionUtils.countMatches(list1, new Predicate() { + public boolean evaluate(Customer customer) { + return Arrays.asList("Daniel","Kyle").contains(customer.getName()); + } + }); + assertTrue(result == 2); + } + + @Test + public void givenCustomerList_WhenFiltered_thenCorrectSize() { + + boolean isModified = CollectionUtils.filter(linkedList1, new Predicate() { + public boolean evaluate(Customer customer) { + return Arrays.asList("Daniel","Kyle").contains(customer.getName()); + } + }); + + //filterInverse does the opposite. It removes the element from the list if the Predicate returns true + //select and selectRejected work the same way except that they do not remove elements from the given collection and return a new collection + + assertTrue(isModified && linkedList1.size() == 2); + } + + @Test + public void givenCustomerList_WhenForAllDoSetNameNull_thenNameNull() { + CollectionUtils.forAllDo(list1, new Closure() { + public void execute(Customer customer) { + customer.setName(null); + } + }); + + // forAllButLast does the same except for the last element in the collection + assertTrue(list1.get(0).getName() == null); + } + + @Test + public void givenEmptyList_WhenCheckedIsEmpty_thenTrue() { + List emptyList = new ArrayList(); + List nullList = null; + + //Very handy at times where we want to check if a collection is not null and not empty too. + //isNotEmpty does the opposite. Handy because using ! operator on isEmpty makes it missable while reading + assertTrue(CollectionUtils.isEmpty(emptyList)); + assertTrue(CollectionUtils.isEmpty(nullList)); + } + + @Test + public void givenCustomerListAndASubcollection_WhenChecked_thenTrue() { + assertTrue(CollectionUtils.isSubCollection(list3, list1)); + } + + @Test + public void givenTwoLists_WhenIntersected_thenCheckSize() { + Collection intersection = CollectionUtils.intersection(list1, list3); + assertTrue(intersection.size() == 2); + } + + @Test + public void givenTwoLists_WhenSubtracted_thenCheckElementNotPresentInA() { + Collection result = CollectionUtils.subtract(list1, list3); + assertFalse(result.contains(customer1)); + } + + @Test + public void givenTwoLists_WhenUnioned_thenCheckElementPresentInResult() { + Collection union = CollectionUtils.union(list1, list2); + assertTrue(union.contains(customer1)); + assertTrue(union.contains(customer4)); + } + +} From cfb7e8889dc0cbf98ba5d332ee63a93db9d719da Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Thu, 29 Jun 2017 13:05:50 +0530 Subject: [PATCH 09/10] Changing test names and updating maven dependency --- collectionutils/pom.xml | 2 +- .../java/collectionutils/CollectionUtilsGuideTest.java | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/collectionutils/pom.xml b/collectionutils/pom.xml index 6d05375efe..810f53847d 100644 --- a/collectionutils/pom.xml +++ b/collectionutils/pom.xml @@ -17,7 +17,7 @@ org.apache.commons commons-collections4 - 4.0 + 4.1 diff --git a/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java b/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java index 0913a56c9d..af925778b8 100644 --- a/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java +++ b/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java @@ -35,9 +35,8 @@ public class CollectionUtilsGuideTest { @Test public void givenList_WhenAddIgnoreNull_thenNoNullAdded() { - List list = Arrays.asList("x", "y", "z"); - CollectionUtils.addIgnoreNull(list, null); - assertFalse(list.contains(null)); + CollectionUtils.addIgnoreNull(list1, null); + assertFalse(list1.contains(null)); } @Test @@ -98,14 +97,15 @@ public class CollectionUtilsGuideTest { } @Test - public void givenEmptyList_WhenCheckedIsEmpty_thenTrue() { + public void givenNonEmptyList_WhenCheckedIsNotEmpty_thenTrue() { List emptyList = new ArrayList(); List nullList = null; //Very handy at times where we want to check if a collection is not null and not empty too. //isNotEmpty does the opposite. Handy because using ! operator on isEmpty makes it missable while reading - assertTrue(CollectionUtils.isEmpty(emptyList)); + assertTrue(CollectionUtils.isNotEmpty(list1)); assertTrue(CollectionUtils.isEmpty(nullList)); + assertTrue(CollectionUtils.isEmpty(emptyList)); } @Test From 8a8e650c8ff1c910a5eed2bcff4b1f802dcb3bd4 Mon Sep 17 00:00:00 2001 From: Himanshu Mantri Date: Wed, 5 Jul 2017 16:57:11 +0530 Subject: [PATCH 10/10] Moving Address into Customer --- .../collectionutilsguide/model/Address.java | 14 +---- .../collectionutilsguide/model/Customer.java | 53 +++---------------- collectionutils/src/pom.xml | 33 ++++++++++++ .../CollectionUtilsGuideTest.java | 14 ++--- 4 files changed, 50 insertions(+), 64 deletions(-) create mode 100644 collectionutils/src/pom.xml diff --git a/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java index da700ccd0f..87c03cde14 100644 --- a/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java +++ b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Address.java @@ -4,7 +4,6 @@ public class Address { String locality; String city; - String zip; public String getLocality() { return locality; @@ -22,26 +21,17 @@ public class Address { this.city = city; } - public String getZip() { - return zip; - } - - public void setZip(String zip) { - this.zip = zip; - } - @Override public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("Address [locality=").append(locality).append(", city=").append(city).append(", zip=").append(zip).append("]"); + builder.append("Address [locality=").append(locality).append(", city=").append(city).append("]"); return builder.toString(); } - public Address(String locality, String city, String zip) { + public Address(String locality, String city) { super(); this.locality = locality; this.city = city; - this.zip = zip; } } diff --git a/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java index 2c7aa9aa12..990ac62857 100644 --- a/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java +++ b/collectionutils/src/main/java/com/baeldung/collectionutilsguide/model/Customer.java @@ -4,10 +4,7 @@ public class Customer implements Comparable { Integer id; String name; - Long phone; - String locality; - String city; - String zip; + Address address; public Integer getId() { return id; @@ -25,53 +22,19 @@ public class Customer implements Comparable { this.name = name; } - public Long getPhone() { - return phone; + public Address getAddress() { + return address; } - public void setPhone(Long phone) { - this.phone = phone; + public void setAddress(Address address) { + this.address = address; } - public String getLocality() { - return locality; - } - - public void setLocality(String locality) { - this.locality = locality; - } - - public String getCity() { - return city; - } - - public void setCity(String city) { - this.city = city; - } - - public String getZip() { - return zip; - } - - public void setZip(String zip) { - this.zip = zip; - } - - public Customer(Integer id, String name, Long phone, String locality, String city, String zip) { + public Customer(Integer id, String name, String locality, String city) { super(); this.id = id; this.name = name; - this.phone = phone; - this.locality = locality; - this.city = city; - this.zip = zip; - } - - public Customer(Integer id, String name, Long phone) { - super(); - this.id = id; - this.name = name; - this.phone = phone; + this.address = new Address(locality, city); } public Customer(String name) { @@ -111,7 +74,7 @@ public class Customer implements Comparable { @Override public String toString() { StringBuilder builder = new StringBuilder(); - builder.append("Customer [id=").append(id).append(", name=").append(name).append(", phone=").append(phone).append("]"); + builder.append("Customer [id=").append(id).append(", name=").append(name).append("]"); return builder.toString(); } diff --git a/collectionutils/src/pom.xml b/collectionutils/src/pom.xml new file mode 100644 index 0000000000..810f53847d --- /dev/null +++ b/collectionutils/src/pom.xml @@ -0,0 +1,33 @@ + + 4.0.0 + baeldung + collectionutils + 0.0.1-SNAPSHOT + collectionutils-guide + A guide to Apache Commons CollectionUtils + + + UTF-8 + UTF-8 + 1.8 + + + + + + org.apache.commons + commons-collections4 + 4.1 + + + + + junit + junit + 4.12 + test + + + + + \ No newline at end of file diff --git a/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java b/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java index af925778b8..a003e38524 100644 --- a/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java +++ b/collectionutils/src/test/java/collectionutils/CollectionUtilsGuideTest.java @@ -20,12 +20,12 @@ import com.baeldung.collectionutilsguide.model.Customer; public class CollectionUtilsGuideTest { - Customer customer1 = new Customer(1, "Daniel", 123456l, "locality1", "city1", "1234"); - Customer customer2 = new Customer(2, "Fredrik", 234567l, "locality2", "city2", "2345"); - Customer customer3 = new Customer(3, "Kyle", 345678l, "locality3", "city3", "3456"); - Customer customer4 = new Customer(4, "Bob", 456789l, "locality4", "city4", "4567"); - Customer customer5 = new Customer(5, "Cat", 567890l, "locality5", "city5", "5678"); - Customer customer6 = new Customer(6, "John", 678901l, "locality6", "city6", "6789"); + Customer customer1 = new Customer(1, "Daniel", "locality1", "city1"); + Customer customer2 = new Customer(2, "Fredrik", "locality2", "city2"); + Customer customer3 = new Customer(3, "Kyle", "locality3", "city3"); + Customer customer4 = new Customer(4, "Bob", "locality4", "city4"); + Customer customer5 = new Customer(5, "Cat", "locality5", "city5"); + Customer customer6 = new Customer(6, "John", "locality6", "city6"); List list1 = Arrays.asList(customer1, customer2, customer3); List list2 = Arrays.asList(customer4, customer5, customer6); @@ -50,7 +50,7 @@ public class CollectionUtilsGuideTest { public void givenListOfCustomers_whenTransformed_thenListOfAddress() { Collection
addressCol = CollectionUtils.collect(list1, new Transformer() { public Address transform(Customer customer) { - return new Address(customer.getLocality(), customer.getCity(), customer.getZip()); + return customer.getAddress(); } });