diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a66a0 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +/target/ +/.settings/ +/.classpath +/.project +/build/ +/.idea/ +*.iml + diff --git a/README.md b/README.md new file mode 100644 index 0000000..0cb0ef1 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +## Object Utils ## + +A collection of useful object utilities + +Trim Properties + +```java +User trimmedUser = ObjectUtils.trimProperties(user); +``` + diff --git a/lombok.config b/lombok.config new file mode 100644 index 0000000..a23edb4 --- /dev/null +++ b/lombok.config @@ -0,0 +1,2 @@ +config.stopBubbling = true +lombok.addLombokGeneratedAnnotation = true \ No newline at end of file diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..deb4507 --- /dev/null +++ b/pom.xml @@ -0,0 +1,34 @@ + + 4.0.0 + + io.ljunggren + object-utils + 0.0.1-SNAPSHOT + jar + + object-utils + http://maven.apache.org + + + UTF-8 + 1.8 + 1.8 + + + + + org.projectlombok + lombok + 1.18.12 + provided + + + junit + junit + 4.13 + test + + + diff --git a/src/main/java/io/ljunggren/objectUtils/ObjectUtils.java b/src/main/java/io/ljunggren/objectUtils/ObjectUtils.java new file mode 100644 index 0000000..7f192d0 --- /dev/null +++ b/src/main/java/io/ljunggren/objectUtils/ObjectUtils.java @@ -0,0 +1,28 @@ +package io.ljunggren.objectUtils; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +public class ObjectUtils { + + @SuppressWarnings("unchecked") + public static T trimProperties(T object) throws IllegalArgumentException, IllegalAccessException { + if (object == null) { + return null; + } + if (object instanceof String) { + String string = ((String) object).trim(); + return (T) string; + } + Field[] fields = object.getClass().getDeclaredFields(); + for (Field field: fields) { + if (field != null && field.getType() == String.class && !Modifier.isFinal(field.getModifiers())) { + field.setAccessible(true); + String value = (String) field.get(object); + field.set(object, value == null ? value : value.trim()); + } + } + return object; + } + +} diff --git a/src/test/java/io/ljunggren/objectUtils/ObjectUtilsTest.java b/src/test/java/io/ljunggren/objectUtils/ObjectUtilsTest.java new file mode 100644 index 0000000..dacd676 --- /dev/null +++ b/src/test/java/io/ljunggren/objectUtils/ObjectUtilsTest.java @@ -0,0 +1,45 @@ +package io.ljunggren.objectUtils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +import org.junit.Test; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + + +public class ObjectUtilsTest { + + @Data + @NoArgsConstructor + @AllArgsConstructor + private static class User { + private String name; + private int age; + private boolean active; + } + + + @Test + public void trimPropertiesTest() throws IllegalArgumentException, IllegalAccessException { + User user = new User(" Alex ", 40, true); + user = ObjectUtils.trimProperties(user); + assertEquals("Alex", user.getName()); + } + + @Test + public void trimPropertiesPrimitiveTest() throws IllegalArgumentException, IllegalAccessException { + String name = " Alex "; + name = ObjectUtils.trimProperties(name); + assertEquals("Alex", name); + } + + @Test + public void trimPropertiesNullTest() throws IllegalArgumentException, IllegalAccessException { + Object object = ObjectUtils.trimProperties(null); + assertNull(object); + } + +}