From 77240dc024831aa643e0c2c885e09e92c15fbf7a Mon Sep 17 00:00:00 2001 From: DianeDuan Date: Mon, 21 Nov 2016 20:41:33 +0800 Subject: [PATCH] delete extra comments and variable assignments, use guava precondition for check --- spring-core/pom.xml | 5 +++++ .../InitializationToolFactory.java | 19 +++++++++---------- .../factorybean/NonSingleToolFactory.java | 8 ++++---- .../factorybean/PostConstructToolFactory.java | 19 +++++++++---------- .../factorybean/SingleToolFactory.java | 8 ++++---- .../java/com/baeldung/factorybean/Tool.java | 6 +++--- .../com/baeldung/factorybean/ToolFactory.java | 8 ++++---- .../java/com/baeldung/factorybean/Worker.java | 4 ++-- .../factorybean-abstract-spring-ctx.xml | 4 ++-- .../resources/factorybean-init-spring-ctx.xml | 4 ++-- .../factorybean-postconstruct-spring-ctx.xml | 4 ++-- .../main/resources/factorybean-spring-ctx.xml | 4 ++-- .../FactoryBeanInitializeTest.java | 5 ++--- 13 files changed, 50 insertions(+), 48 deletions(-) diff --git a/spring-core/pom.xml b/spring-core/pom.xml index 798a717d01..e8ab8b00c4 100644 --- a/spring-core/pom.xml +++ b/spring-core/pom.xml @@ -50,6 +50,11 @@ 4.12 test + + com.google.guava + guava + 20.0 + diff --git a/spring-core/src/main/java/com/baeldung/factorybean/InitializationToolFactory.java b/spring-core/src/main/java/com/baeldung/factorybean/InitializationToolFactory.java index 925ba2d8e4..6d2fd2564e 100644 --- a/spring-core/src/main/java/com/baeldung/factorybean/InitializationToolFactory.java +++ b/spring-core/src/main/java/com/baeldung/factorybean/InitializationToolFactory.java @@ -1,22 +1,21 @@ package com.baeldung.factorybean; +import static com.google.common.base.Preconditions.checkArgument; + import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; +import org.springframework.util.StringUtils; public class InitializationToolFactory implements FactoryBean, InitializingBean { - private int factoryId;// standard setters and getters - private int toolId;// standard setters and getters - private String toolName;// standard setters and getters - private double toolPrice;// standard setters and getters + private int factoryId; + private int toolId; + private String toolName; + private double toolPrice; @Override public void afterPropertiesSet() throws Exception { - if (toolName == null || toolName.equals("")) { - throw new IllegalArgumentException("tool name cannot be empty"); - } - if (toolPrice < 0) { - throw new IllegalArgumentException("tool price should not be less than 0"); - } + checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty"); + checkArgument(toolPrice >= 0, "tool price should not be less than 0"); } @Override diff --git a/spring-core/src/main/java/com/baeldung/factorybean/NonSingleToolFactory.java b/spring-core/src/main/java/com/baeldung/factorybean/NonSingleToolFactory.java index 0cd80eab41..c818b775eb 100644 --- a/spring-core/src/main/java/com/baeldung/factorybean/NonSingleToolFactory.java +++ b/spring-core/src/main/java/com/baeldung/factorybean/NonSingleToolFactory.java @@ -3,10 +3,10 @@ package com.baeldung.factorybean; import org.springframework.beans.factory.config.AbstractFactoryBean; public class NonSingleToolFactory extends AbstractFactoryBean { - private int factoryId;// standard setters and getters - private int toolId;// standard setters and getters - private String toolName;// standard setters and getters - private double toolPrice;// standard setters and getters + private int factoryId; + private int toolId; + private String toolName; + private double toolPrice; public NonSingleToolFactory() { setSingleton(false); diff --git a/spring-core/src/main/java/com/baeldung/factorybean/PostConstructToolFactory.java b/spring-core/src/main/java/com/baeldung/factorybean/PostConstructToolFactory.java index 8298f6f4db..47db05d271 100644 --- a/spring-core/src/main/java/com/baeldung/factorybean/PostConstructToolFactory.java +++ b/spring-core/src/main/java/com/baeldung/factorybean/PostConstructToolFactory.java @@ -1,14 +1,17 @@ package com.baeldung.factorybean; +import static com.google.common.base.Preconditions.checkArgument; + import javax.annotation.PostConstruct; import org.springframework.beans.factory.FactoryBean; +import org.springframework.util.StringUtils; public class PostConstructToolFactory implements FactoryBean { - private int factoryId;// standard setters and getters - private int toolId;// standard setters and getters - private String toolName;// standard setters and getters - private double toolPrice;// standard setters and getters + private int factoryId; + private int toolId; + private String toolName; + private double toolPrice; @Override public Tool getObject() throws Exception { @@ -27,12 +30,8 @@ public class PostConstructToolFactory implements FactoryBean { @PostConstruct public void checkParams() { - if (toolName == null || toolName.equals("")) { - throw new IllegalArgumentException("tool name cannot be empty"); - } - if (toolPrice < 0) { - throw new IllegalArgumentException("tool price should not be less than 0"); - } + checkArgument(!StringUtils.isEmpty(toolName), "tool name cannot be empty"); + checkArgument(toolPrice >= 0, "tool price should not be less than 0"); } public int getFactoryId() { diff --git a/spring-core/src/main/java/com/baeldung/factorybean/SingleToolFactory.java b/spring-core/src/main/java/com/baeldung/factorybean/SingleToolFactory.java index 94d68ef113..bc0c2d79c0 100644 --- a/spring-core/src/main/java/com/baeldung/factorybean/SingleToolFactory.java +++ b/spring-core/src/main/java/com/baeldung/factorybean/SingleToolFactory.java @@ -4,10 +4,10 @@ import org.springframework.beans.factory.config.AbstractFactoryBean; //no need to set singleton property because default value is true public class SingleToolFactory extends AbstractFactoryBean { - private int factoryId;// standard setters and getters - private int toolId;// standard setters and getters - private String toolName;// standard setters and getters - private double toolPrice;// standard setters and getters + private int factoryId; + private int toolId; + private String toolName; + private double toolPrice; @Override public Class getObjectType() { diff --git a/spring-core/src/main/java/com/baeldung/factorybean/Tool.java b/spring-core/src/main/java/com/baeldung/factorybean/Tool.java index abdd074e9a..a7f7f7681e 100644 --- a/spring-core/src/main/java/com/baeldung/factorybean/Tool.java +++ b/spring-core/src/main/java/com/baeldung/factorybean/Tool.java @@ -1,9 +1,9 @@ package com.baeldung.factorybean; public class Tool { - private int id;// standard setters and getters - private String name;// standard setters and getters - private double price;// standard setters and getters + private int id; + private String name; + private double price; public Tool() { } diff --git a/spring-core/src/main/java/com/baeldung/factorybean/ToolFactory.java b/spring-core/src/main/java/com/baeldung/factorybean/ToolFactory.java index 9b2f7fa42e..ca8f82eadb 100644 --- a/spring-core/src/main/java/com/baeldung/factorybean/ToolFactory.java +++ b/spring-core/src/main/java/com/baeldung/factorybean/ToolFactory.java @@ -3,10 +3,10 @@ package com.baeldung.factorybean; import org.springframework.beans.factory.FactoryBean; public class ToolFactory implements FactoryBean { - private int factoryId;// standard setters and getters - private int toolId;// standard setters and getters - private String toolName;// standard setters and getters - private double toolPrice;// standard setters and getters + private int factoryId; + private int toolId; + private String toolName; + private double toolPrice; @Override public Tool getObject() throws Exception { diff --git a/spring-core/src/main/java/com/baeldung/factorybean/Worker.java b/spring-core/src/main/java/com/baeldung/factorybean/Worker.java index 070322a5f9..9a35c0656b 100644 --- a/spring-core/src/main/java/com/baeldung/factorybean/Worker.java +++ b/spring-core/src/main/java/com/baeldung/factorybean/Worker.java @@ -1,8 +1,8 @@ package com.baeldung.factorybean; public class Worker { - private String number;// standard setters and getters - private Tool tool;// standard setters and getters + private String number; + private Tool tool; public Worker() { } diff --git a/spring-core/src/main/resources/factorybean-abstract-spring-ctx.xml b/spring-core/src/main/resources/factorybean-abstract-spring-ctx.xml index 2f34e2e1cf..6bce114d9c 100644 --- a/spring-core/src/main/resources/factorybean-abstract-spring-ctx.xml +++ b/spring-core/src/main/resources/factorybean-abstract-spring-ctx.xml @@ -1,6 +1,6 @@ - diff --git a/spring-core/src/main/resources/factorybean-init-spring-ctx.xml b/spring-core/src/main/resources/factorybean-init-spring-ctx.xml index f5835c1e70..47722b6d3d 100644 --- a/spring-core/src/main/resources/factorybean-init-spring-ctx.xml +++ b/spring-core/src/main/resources/factorybean-init-spring-ctx.xml @@ -1,6 +1,6 @@ - diff --git a/spring-core/src/main/resources/factorybean-postconstruct-spring-ctx.xml b/spring-core/src/main/resources/factorybean-postconstruct-spring-ctx.xml index 81e02a0b35..94a4f407a8 100644 --- a/spring-core/src/main/resources/factorybean-postconstruct-spring-ctx.xml +++ b/spring-core/src/main/resources/factorybean-postconstruct-spring-ctx.xml @@ -1,7 +1,7 @@ - diff --git a/spring-core/src/main/resources/factorybean-spring-ctx.xml b/spring-core/src/main/resources/factorybean-spring-ctx.xml index 800e489ba0..ab0c646bb0 100644 --- a/spring-core/src/main/resources/factorybean-spring-ctx.xml +++ b/spring-core/src/main/resources/factorybean-spring-ctx.xml @@ -1,6 +1,6 @@ - diff --git a/spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanInitializeTest.java b/spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanInitializeTest.java index 851c15a3ec..848eea216a 100644 --- a/spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanInitializeTest.java +++ b/spring-core/src/test/java/com/baeldung/factorybean/FactoryBeanInitializeTest.java @@ -1,17 +1,16 @@ package com.baeldung.factorybean; import org.junit.Test; -import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class FactoryBeanInitializeTest { @Test(expected = Exception.class) public void testInitializationToolFactory() { - ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-init-spring-ctx.xml"); + new ClassPathXmlApplicationContext("classpath:factorybean-init-spring-ctx.xml"); } @Test(expected = Exception.class) public void testPostConstructToolFactory() { - ApplicationContext context = new ClassPathXmlApplicationContext("classpath:factorybean-postconstruct-spring-ctx.xml"); + new ClassPathXmlApplicationContext("classpath:factorybean-postconstruct-spring-ctx.xml"); } }