Initial commit
This commit is contained in:
parent
e014922849
commit
5f65787ca5
13 changed files with 541 additions and 4 deletions
4
.gitignore
vendored
Normal file
4
.gitignore
vendored
Normal file
|
@ -0,0 +1,4 @@
|
|||
/target
|
||||
/.settings
|
||||
/.classpath
|
||||
/.project
|
10
LICENSE
10
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.
|
||||
limitations under the License.
|
||||
|
||||
|
|
55
pom.xml
Normal file
55
pom.xml
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<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/maven-v4_0_0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.rometools</groupId>
|
||||
<artifactId>rome-parent</artifactId>
|
||||
<version>1.5.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<artifactId>rome-utils</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>rome-utils</name>
|
||||
|
||||
<description>Utility classes for ROME projects</description>
|
||||
|
||||
<url>http://rometools.github.io/rome-utils/</url>
|
||||
|
||||
<scm>
|
||||
<connection>scm:git:git@github.com:rometools/rome-utils.git</connection>
|
||||
<developerConnection>scm:git:git@github.com:rometools/rome-utils.git</developerConnection>
|
||||
<url>https://github.com/rometools/rome-utils</url>
|
||||
</scm>
|
||||
|
||||
<developers>
|
||||
<developer>
|
||||
<name>Patrick Gotthard</name>
|
||||
<url>http://www.patrick-gotthard.de</url>
|
||||
<timezone>+1</timezone>
|
||||
</developer>
|
||||
</developers>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>hamcrest-core</artifactId>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.hamcrest</groupId>
|
||||
<artifactId>hamcrest-all</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
23
src/main/java/com/rometools/utils/Alternatives.java
Normal file
23
src/main/java/com/rometools/utils/Alternatives.java
Normal file
|
@ -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> T firstNotNull(final T... objects) {
|
||||
for (final T object : objects) {
|
||||
if (object != null) {
|
||||
return object;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
24
src/main/java/com/rometools/utils/Dates.java
Normal file
24
src/main/java/com/rometools/utils/Dates.java
Normal file
|
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
100
src/main/java/com/rometools/utils/Lists.java
Normal file
100
src/main/java/com/rometools/utils/Lists.java
Normal file
|
@ -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 <T> List<T> createWhenNull(final List<T> list) {
|
||||
if (list == null) {
|
||||
return new ArrayList<T>();
|
||||
} 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 <T> List<T> create(final T item) {
|
||||
final List<T> list = new ArrayList<T>();
|
||||
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> T firstEntry(final List<T> 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 <T> List<T> emptyToNull(final List<T> list) {
|
||||
if (isEmpty(list)) {
|
||||
return null;
|
||||
} else {
|
||||
return list;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
78
src/main/java/com/rometools/utils/Strings.java
Normal file
78
src/main/java/com/rometools/utils/Strings.java
Normal file
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
0
src/main/resources/.gitignore
vendored
Normal file
0
src/main/resources/.gitignore
vendored
Normal file
23
src/test/java/com/rometools/utils/AlternativesTest.java
Normal file
23
src/test/java/com/rometools/utils/AlternativesTest.java
Normal file
|
@ -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()));
|
||||
|
||||
}
|
||||
|
||||
}
|
26
src/test/java/com/rometools/utils/DatesTest.java
Normal file
26
src/test/java/com/rometools/utils/DatesTest.java
Normal file
|
@ -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()));
|
||||
|
||||
}
|
||||
|
||||
}
|
105
src/test/java/com/rometools/utils/ListsTest.java
Normal file
105
src/test/java/com/rometools/utils/ListsTest.java
Normal file
|
@ -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<Integer> list = new ArrayList<Integer>();
|
||||
final List<Integer> 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<Integer> create = Lists.create(1);
|
||||
assertThat(create, is(notNullValue()));
|
||||
assertThat(create.size(), is(1));
|
||||
assertThat(create, hasItem(1));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFirstEntry() {
|
||||
|
||||
final List<Integer> nullList = null;
|
||||
final List<Integer> listWithoutEntries = new ArrayList<Integer>();
|
||||
final List<Integer> listWithOneEntry = Arrays.asList(1);
|
||||
final List<Integer> 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<Integer> nullList = null;
|
||||
final List<Integer> listWithoutEntries = new ArrayList<Integer>();
|
||||
final List<Integer> 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<Integer> nullList = null;
|
||||
final List<Integer> listWithoutEntries = new ArrayList<Integer>();
|
||||
final List<Integer> 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<Integer> nullList = null;
|
||||
final List<Integer> listWithoutEntries = new ArrayList<Integer>();
|
||||
final List<Integer> 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<Integer> nullList = null;
|
||||
final List<Integer> listWithoutEntries = new ArrayList<Integer>();
|
||||
final List<Integer> listWithOneEntry = Arrays.asList(1);
|
||||
|
||||
assertThat(Lists.emptyToNull(nullList), is(nullValue()));
|
||||
assertThat(Lists.emptyToNull(listWithoutEntries), is(nullValue()));
|
||||
assertThat(Lists.emptyToNull(listWithOneEntry), is(notNullValue()));
|
||||
|
||||
}
|
||||
|
||||
}
|
97
src/test/java/com/rometools/utils/StringsTest.java
Normal file
97
src/test/java/com/rometools/utils/StringsTest.java
Normal file
|
@ -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"));
|
||||
}
|
||||
|
||||
}
|
0
src/test/resources/.gitignore
vendored
Normal file
0
src/test/resources/.gitignore
vendored
Normal file
Loading…
Reference in a new issue