diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..c708c36
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,4 @@
+/target
+/.settings
+/.classpath
+/.project
diff --git a/LICENSE b/LICENSE
index ad410e1..6b0b127 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,4 +1,5 @@
-Apache License
+
+ Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
@@ -178,7 +179,7 @@ Apache License
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
- boilerplate notice, with the fields enclosed by brackets "{}"
+ boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
@@ -186,7 +187,7 @@ Apache License
same "printed page" as the copyright notice for easier
identification within third-party archives.
- Copyright {yyyy} {name of copyright owner}
+ Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -198,4 +199,5 @@ Apache License
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
- limitations under the License.
\ No newline at end of file
+ limitations under the License.
+
diff --git a/pom.xml b/pom.xml
new file mode 100644
index 0000000..b8defa9
--- /dev/null
+++ b/pom.xml
@@ -0,0 +1,55 @@
+
+
+
+ 4.0.0
+
+
+ com.rometools
+ rome-parent
+ 1.5.0-SNAPSHOT
+
+
+ rome-utils
+ jar
+
+ rome-utils
+
+ Utility classes for ROME projects
+
+ http://rometools.github.io/rome-utils/
+
+
+ scm:git:git@github.com:rometools/rome-utils.git
+ scm:git:git@github.com:rometools/rome-utils.git
+ https://github.com/rometools/rome-utils
+
+
+
+
+ Patrick Gotthard
+ http://www.patrick-gotthard.de
+ +1
+
+
+
+
+
+ junit
+ junit
+ test
+
+
+ hamcrest-core
+ org.hamcrest
+
+
+
+
+ org.hamcrest
+ hamcrest-all
+ test
+
+
+
+
\ No newline at end of file
diff --git a/src/main/java/com/rometools/utils/Alternatives.java b/src/main/java/com/rometools/utils/Alternatives.java
new file mode 100644
index 0000000..cf8fba2
--- /dev/null
+++ b/src/main/java/com/rometools/utils/Alternatives.java
@@ -0,0 +1,23 @@
+package com.rometools.utils;
+
+public final class Alternatives {
+
+ private Alternatives() {
+ }
+
+ /**
+ * Returns the first object that is not null
+ *
+ * @param objects The objects to process
+ * @return The first value that is not null. null when there is no not-null value
+ */
+ public static T firstNotNull(final T... objects) {
+ for (final T object : objects) {
+ if (object != null) {
+ return object;
+ }
+ }
+ return null;
+ }
+
+}
diff --git a/src/main/java/com/rometools/utils/Dates.java b/src/main/java/com/rometools/utils/Dates.java
new file mode 100644
index 0000000..9060e30
--- /dev/null
+++ b/src/main/java/com/rometools/utils/Dates.java
@@ -0,0 +1,24 @@
+package com.rometools.utils;
+
+import java.util.Date;
+
+public final class Dates {
+
+ private Dates() {
+ }
+
+ /**
+ * Creates a copy on a Date.
+ *
+ * @param d The Date to copy, can be null
+ * @return null when the input Date was null, a copy of the Date otherwise
+ */
+ public static Date copy(final Date d) {
+ if (d == null) {
+ return null;
+ } else {
+ return new Date(d.getTime());
+ }
+ }
+
+}
diff --git a/src/main/java/com/rometools/utils/Lists.java b/src/main/java/com/rometools/utils/Lists.java
new file mode 100644
index 0000000..3cbdc10
--- /dev/null
+++ b/src/main/java/com/rometools/utils/Lists.java
@@ -0,0 +1,100 @@
+package com.rometools.utils;
+
+import java.util.ArrayList;
+import java.util.List;
+
+public final class Lists {
+
+ private Lists() {
+ }
+
+ /**
+ * Returns the list when it is not null. Returns a new list otherwise.
+ *
+ * @param list The list to process, can be null
+ * @return The input list when it is not null, a new list otherwise
+ */
+ public static List createWhenNull(final List list) {
+ if (list == null) {
+ return new ArrayList();
+ } else {
+ return list;
+ }
+ }
+
+ /**
+ * Creates a new List with the given item as the first entry.
+ *
+ * @param item The item to add to the new list
+ * @return List containing the given item
+ */
+ public static List create(final T item) {
+ final List list = new ArrayList();
+ list.add(item);
+ return list;
+ }
+
+ /**
+ * Extracts the first entry of the list when it is not null and contains values.
+ *
+ * @param list The list to extract the first entry from, can be null
+ * @return The first entry of the list when it is not null or empty, null otherwise
+ */
+ public static T firstEntry(final List list) {
+ if (list != null && !list.isEmpty()) {
+ return list.get(0);
+ } else {
+ return null;
+ }
+ }
+
+ /**
+ * Checks whether the list is null or empty.
+ *
+ * @param list The list to check
+ * @return true when the list is null or empty, false otherwise
+ */
+ public static boolean isEmpty(final List> list) {
+ return list == null || list.isEmpty();
+ }
+
+ /**
+ * Checks whether the list is not null and not empty.
+ *
+ * @param list The list to check
+ * @return true when the list is not null and not empty
+ */
+ public static boolean isNotEmpty(final List> list) {
+ return !isEmpty(list);
+ }
+
+ /**
+ * Checks whether the list has the given size. A null list is treated like a list without entries.
+ *
+ * @param list The list to check
+ * @param size The size to check
+ * @return true when the list has the given size or when size = 0 and the list is null, false otherwise
+ */
+ public static boolean sizeIs(final List> list, final int size) {
+ if (size == 0) {
+ return list == null || list.isEmpty();
+ } else {
+ return list != null && list.size() == size;
+ }
+ }
+
+ /**
+ * Returns null, when the given list is empty or null
+ *
+ * @param list The list to process
+ * @return null when the list is empty or null, the given list otherwise
+ */
+ public static List emptyToNull(final List list) {
+ if (isEmpty(list)) {
+ return null;
+ } else {
+ return list;
+ }
+ }
+
+}
diff --git a/src/main/java/com/rometools/utils/Strings.java b/src/main/java/com/rometools/utils/Strings.java
new file mode 100644
index 0000000..76f56e4
--- /dev/null
+++ b/src/main/java/com/rometools/utils/Strings.java
@@ -0,0 +1,78 @@
+package com.rometools.utils;
+
+import java.util.Locale;
+
+public final class Strings {
+
+ private Strings() {
+ }
+
+ /**
+ * Checks whether a String is null.
+ *
+ * @param s The String to check
+ * @return true when the String is null, false otherwise
+ */
+ public static boolean isNull(final String s) {
+ return s == null;
+ }
+
+ /**
+ * Checks whether a String is null or empty.
+ *
+ * @param s The String to check
+ * @return true when the String is null or empty, false otherwise
+ */
+ public static boolean isEmpty(final String s) {
+ return isNull(s) || s.isEmpty();
+ }
+
+ /**
+ * Checks whether a String is neither null nor empty.
+ *
+ * @param s The String to check
+ * @return true when the String is neither null nor empty, false otherwise
+ */
+ public static boolean isNotEmpty(final String s) {
+ return !isEmpty(s);
+ }
+
+ /**
+ * Checks whether a String is null, empty or blank.
+ *
+ * @param s The String to check
+ * @return true when the String is null, empty or blank, false otherwise
+ */
+ public static boolean isBlank(final String s) {
+ return isEmpty(s) || s.trim().isEmpty();
+ }
+
+ /**
+ * null-safe trimming of a String.
+ *
+ * @param s The String to trim, may be null
+ * @return null when the input String is null, the trimmed String otherwise
+ */
+ public static String trim(final String s) {
+ if (s == null) {
+ return null;
+ } else {
+ return s.trim();
+ }
+ }
+
+ /**
+ * null-safe lower-case conversion of a String.
+ *
+ * @param s The String to process, may be null
+ * @return null when the input String is null, the String in lower-case otherwise
+ */
+ public static String toLowerCase(final String s) {
+ if (s == null) {
+ return null;
+ } else {
+ return s.toLowerCase(Locale.ENGLISH);
+ }
+ }
+
+}
diff --git a/src/main/resources/.gitignore b/src/main/resources/.gitignore
new file mode 100644
index 0000000..e69de29
diff --git a/src/test/java/com/rometools/utils/AlternativesTest.java b/src/test/java/com/rometools/utils/AlternativesTest.java
new file mode 100644
index 0000000..ba23641
--- /dev/null
+++ b/src/test/java/com/rometools/utils/AlternativesTest.java
@@ -0,0 +1,23 @@
+package com.rometools.utils;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class AlternativesTest {
+
+ @Test
+ public void testFirstNotNull() {
+
+ final Integer nullInteger = null;
+ final Integer notNullInteger = 1;
+
+ assertThat(Alternatives.firstNotNull(notNullInteger, nullInteger), is(notNullInteger));
+ assertThat(Alternatives.firstNotNull(nullInteger, notNullInteger), is(notNullInteger));
+ assertThat(Alternatives.firstNotNull(nullInteger, nullInteger), is(nullValue()));
+
+ }
+
+}
diff --git a/src/test/java/com/rometools/utils/DatesTest.java b/src/test/java/com/rometools/utils/DatesTest.java
new file mode 100644
index 0000000..d38edcc
--- /dev/null
+++ b/src/test/java/com/rometools/utils/DatesTest.java
@@ -0,0 +1,26 @@
+package com.rometools.utils;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+import java.util.Date;
+
+import org.junit.Test;
+
+public class DatesTest {
+
+ @Test
+ public void testCopy() {
+
+ final Date date = new Date();
+ final Date nullDate = null;
+
+ assertThat(Dates.copy(date), is(notNullValue()));
+ assertThat(Dates.copy(date).getTime(), is(date.getTime()));
+ assertThat(Dates.copy(nullDate), is(nullValue()));
+
+ }
+
+}
diff --git a/src/test/java/com/rometools/utils/ListsTest.java b/src/test/java/com/rometools/utils/ListsTest.java
new file mode 100644
index 0000000..dc1b7a8
--- /dev/null
+++ b/src/test/java/com/rometools/utils/ListsTest.java
@@ -0,0 +1,105 @@
+package com.rometools.utils;
+
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.notNullValue;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.junit.Test;
+
+public class ListsTest {
+
+ @Test
+ public void testCreateWhenNull() {
+
+ final List list = new ArrayList();
+ final List nullList = null;
+
+ assertThat(Lists.createWhenNull(list), is(notNullValue()));
+ assertThat(Lists.createWhenNull(list), is(list));
+ assertThat(Lists.createWhenNull(nullList), is(notNullValue()));
+
+ }
+
+ @Test
+ public void testCreate() {
+
+ final List create = Lists.create(1);
+ assertThat(create, is(notNullValue()));
+ assertThat(create.size(), is(1));
+ assertThat(create, hasItem(1));
+
+ }
+
+ @Test
+ public void testFirstEntry() {
+
+ final List nullList = null;
+ final List listWithoutEntries = new ArrayList();
+ final List listWithOneEntry = Arrays.asList(1);
+ final List listWithTwoEntries = Arrays.asList(1, 2);
+
+ assertThat(Lists.firstEntry(nullList), is(nullValue()));
+ assertThat(Lists.firstEntry(listWithoutEntries), is(nullValue()));
+ assertThat(Lists.firstEntry(listWithOneEntry), is(1));
+ assertThat(Lists.firstEntry(listWithTwoEntries), is(1));
+
+ }
+
+ @Test
+ public void testIsEmpty() {
+
+ final List nullList = null;
+ final List listWithoutEntries = new ArrayList();
+ final List listWithOneEntry = Arrays.asList(1);
+
+ assertThat(Lists.isEmpty(nullList), is(true));
+ assertThat(Lists.isEmpty(listWithoutEntries), is(true));
+ assertThat(Lists.isEmpty(listWithOneEntry), is(false));
+
+ }
+
+ @Test
+ public void testIsNotEmpty() {
+
+ final List nullList = null;
+ final List listWithoutEntries = new ArrayList();
+ final List listWithOneEntry = Arrays.asList(1);
+
+ assertThat(Lists.isNotEmpty(nullList), is(false));
+ assertThat(Lists.isNotEmpty(listWithoutEntries), is(false));
+ assertThat(Lists.isNotEmpty(listWithOneEntry), is(true));
+ }
+
+ @Test
+ public void testSizeIs() {
+
+ final List nullList = null;
+ final List listWithoutEntries = new ArrayList();
+ final List listWithOneEntry = Arrays.asList(1);
+
+ assertThat(Lists.sizeIs(nullList, 0), is(true));
+ assertThat(Lists.sizeIs(listWithoutEntries, 0), is(true));
+ assertThat(Lists.sizeIs(listWithOneEntry, 1), is(true));
+
+ }
+
+ @Test
+ public void testEmptyToNull() {
+
+ final List nullList = null;
+ final List listWithoutEntries = new ArrayList();
+ final List listWithOneEntry = Arrays.asList(1);
+
+ assertThat(Lists.emptyToNull(nullList), is(nullValue()));
+ assertThat(Lists.emptyToNull(listWithoutEntries), is(nullValue()));
+ assertThat(Lists.emptyToNull(listWithOneEntry), is(notNullValue()));
+
+ }
+
+}
diff --git a/src/test/java/com/rometools/utils/StringsTest.java b/src/test/java/com/rometools/utils/StringsTest.java
new file mode 100644
index 0000000..97e82ac
--- /dev/null
+++ b/src/test/java/com/rometools/utils/StringsTest.java
@@ -0,0 +1,97 @@
+package com.rometools.utils;
+
+import static org.hamcrest.Matchers.is;
+import static org.hamcrest.Matchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+import org.junit.Test;
+
+public class StringsTest {
+
+ @Test
+ public void testIsNull() {
+
+ final String nullString = null;
+ final String emptyString = "";
+ final String blankString = " ";
+ final String string = "a";
+
+ assertThat(Strings.isNull(nullString), is(true));
+ assertThat(Strings.isNull(emptyString), is(false));
+ assertThat(Strings.isNull(blankString), is(false));
+ assertThat(Strings.isNull(string), is(false));
+
+ }
+
+ @Test
+ public void testIsEmpty() {
+
+ final String nullString = null;
+ final String emptyString = "";
+ final String blankString = " ";
+ final String string = "a";
+
+ assertThat(Strings.isEmpty(nullString), is(true));
+ assertThat(Strings.isEmpty(emptyString), is(true));
+ assertThat(Strings.isEmpty(blankString), is(false));
+ assertThat(Strings.isEmpty(string), is(false));
+
+ }
+
+ @Test
+ public void testIsNotEmpty() {
+
+ final String nullString = null;
+ final String emptyString = "";
+ final String blankString = " ";
+ final String string = "a";
+
+ assertThat(Strings.isNotEmpty(nullString), is(false));
+ assertThat(Strings.isNotEmpty(emptyString), is(false));
+ assertThat(Strings.isNotEmpty(blankString), is(true));
+ assertThat(Strings.isNotEmpty(string), is(true));
+ }
+
+ @Test
+ public void testIsBlank() {
+
+ final String nullString = null;
+ final String emptyString = "";
+ final String blankString = " ";
+ final String string = "a";
+
+ assertThat(Strings.isBlank(nullString), is(true));
+ assertThat(Strings.isBlank(emptyString), is(true));
+ assertThat(Strings.isBlank(blankString), is(true));
+ assertThat(Strings.isBlank(string), is(false));
+ }
+
+ @Test
+ public void testTrim() {
+
+ final String nullString = null;
+ final String emptyString = "";
+ final String blankString = " ";
+ final String string = " a ";
+
+ assertThat(Strings.trim(nullString), is(nullValue()));
+ assertThat(Strings.trim(emptyString), is(""));
+ assertThat(Strings.trim(blankString), is(""));
+ assertThat(Strings.trim(string), is("a"));
+ }
+
+ @Test
+ public void testToLowerCase() {
+
+ final String nullString = null;
+ final String emptyString = "";
+ final String blankString = " ";
+ final String string = "A";
+
+ assertThat(Strings.toLowerCase(nullString), is(nullValue()));
+ assertThat(Strings.toLowerCase(emptyString), is(""));
+ assertThat(Strings.toLowerCase(blankString), is(" "));
+ assertThat(Strings.toLowerCase(string), is("a"));
+ }
+
+}
diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore
new file mode 100644
index 0000000..e69de29