Move rome-utils files into a subdirectory

This commit is contained in:
mishako 2016-02-13 18:37:48 +01:00
parent 1f79112706
commit 56a9150d95
21 changed files with 886 additions and 886 deletions

View file

View file

@ -1,37 +1,37 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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;
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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;
}
}

View file

@ -1,38 +1,38 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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());
}
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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());
}
}
}

View file

@ -1,39 +1,39 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
public class Doubles {
private Doubles() {
}
/**
* Converts a String into an Double.
*
* @param s The String to convert, may be null
* @return The parsed Double or null when parsing is not possible
*/
public static Double parse(final String s) {
Double parsed = null;
try {
if (s != null) {
parsed = Double.parseDouble(s);
}
} catch (final NumberFormatException e) {
}
return parsed;
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
public class Doubles {
private Doubles() {
}
/**
* Converts a String into an Double.
*
* @param s The String to convert, may be null
* @return The parsed Double or null when parsing is not possible
*/
public static Double parse(final String s) {
Double parsed = null;
try {
if (s != null) {
parsed = Double.parseDouble(s);
}
} catch (final NumberFormatException e) {
}
return parsed;
}
}

View file

@ -1,36 +1,36 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
public final class Integers {
private Integers() {
}
/**
* Converts a String into an Integer.
*
* @param s The String to convert, may be null
* @return The parsed Integer or null when parsing is not possible
*/
public static Integer parse(final String s) {
try {
return Integer.parseInt(s);
} catch (final NumberFormatException e) {
return null;
}
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
public final class Integers {
private Integers() {
}
/**
* Converts a String into an Integer.
*
* @param s The String to convert, may be null
* @return The parsed Integer or null when parsing is not possible
*/
public static Integer parse(final String s) {
try {
return Integer.parseInt(s);
} catch (final NumberFormatException e) {
return null;
}
}
}

View file

@ -1,116 +1,116 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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;
}
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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;
}
}
}

View file

@ -1,39 +1,39 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
public final class Longs {
private Longs() {
}
/**
* Converts a String into a Long by first parsing it as Double and then casting it to Long.
*
* @param s The String to convert, may be null or in decimal format
* @return The parsed Long or null when parsing is not possible
*/
public static Long parseDecimal(final String s) {
Long parsed = null;
try {
if (s != null) {
parsed = (long) Double.parseDouble(s);
}
} catch (final NumberFormatException e) {
}
return parsed;
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
public final class Longs {
private Longs() {
}
/**
* Converts a String into a Long by first parsing it as Double and then casting it to Long.
*
* @param s The String to convert, may be null or in decimal format
* @return The parsed Long or null when parsing is not possible
*/
public static Long parseDecimal(final String s) {
Long parsed = null;
try {
if (s != null) {
parsed = (long) Double.parseDouble(s);
}
} catch (final NumberFormatException e) {
}
return parsed;
}
}

View file

@ -1,126 +1,126 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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();
}
/**
* Removes the whitespace at the beginning and end 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();
}
}
/**
* Removes the whitespace at the beginning and end of a String. When the String only contains
* whitespace, it returns null.
*
* @param s The String to trim, may be null
* @return null when the input String is null or does only contain whitespace, the trimmed
* String otherwise
*/
public static String trimToNull(final String s) {
final String trimmed = trim(s);
if (trimmed == null || trimmed.isEmpty()) {
return null;
} else {
return trimmed;
}
}
/**
* Removes the whitespace at the beginning and end of a String. When the String only contains
* whitespace, it returns null.
*
* @param s The String to trim, may be null
* @return null when the input String is null or does only contain whitespace, the trimmed
* String otherwise
*/
public static String trimToEmpty(final String s) {
final String trimmed = trim(s);
if (trimmed == null || trimmed.isEmpty()) {
return "";
} else {
return trimmed;
}
}
/**
* 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);
}
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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();
}
/**
* Removes the whitespace at the beginning and end 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();
}
}
/**
* Removes the whitespace at the beginning and end of a String. When the String only contains
* whitespace, it returns null.
*
* @param s The String to trim, may be null
* @return null when the input String is null or does only contain whitespace, the trimmed
* String otherwise
*/
public static String trimToNull(final String s) {
final String trimmed = trim(s);
if (trimmed == null || trimmed.isEmpty()) {
return null;
} else {
return trimmed;
}
}
/**
* Removes the whitespace at the beginning and end of a String. When the String only contains
* whitespace, it returns null.
*
* @param s The String to trim, may be null
* @return null when the input String is null or does only contain whitespace, the trimmed
* String otherwise
*/
public static String trimToEmpty(final String s) {
final String trimmed = trim(s);
if (trimmed == null || trimmed.isEmpty()) {
return "";
} else {
return trimmed;
}
}
/**
* 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);
}
}
}

View file

@ -1,37 +1,37 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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()));
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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()));
}
}

View file

@ -1,40 +1,40 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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()));
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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()));
}
}

View file

@ -1,40 +1,40 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import org.junit.Test;
public class DoublesTest {
@Test
public void testParse() {
final String nullString = null;
final String emptyString = null;
final String integerString = "1";
final String decimalString = "1.0";
assertThat(Doubles.parse(nullString), is(nullValue()));
assertThat(Doubles.parse(emptyString), is(nullValue()));
assertThat(Doubles.parse(integerString), is(1.0));
assertThat(Doubles.parse(decimalString), is(1.0));
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import org.junit.Test;
public class DoublesTest {
@Test
public void testParse() {
final String nullString = null;
final String emptyString = null;
final String integerString = "1";
final String decimalString = "1.0";
assertThat(Doubles.parse(nullString), is(nullValue()));
assertThat(Doubles.parse(emptyString), is(nullValue()));
assertThat(Doubles.parse(integerString), is(1.0));
assertThat(Doubles.parse(decimalString), is(1.0));
}
}

View file

@ -1,40 +1,40 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import org.junit.Test;
public class IntegersTest {
@Test
public void testParse() {
final String nullString = null;
final String emptyString = null;
final String integerString = "1";
final String decimalString = "1.0";
assertThat(Integers.parse(nullString), is(nullValue()));
assertThat(Integers.parse(emptyString), is(nullValue()));
assertThat(Integers.parse(integerString), is(1));
assertThat(Integers.parse(decimalString), is(nullValue()));
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import org.junit.Test;
public class IntegersTest {
@Test
public void testParse() {
final String nullString = null;
final String emptyString = null;
final String integerString = "1";
final String decimalString = "1.0";
assertThat(Integers.parse(nullString), is(nullValue()));
assertThat(Integers.parse(emptyString), is(nullValue()));
assertThat(Integers.parse(integerString), is(1));
assertThat(Integers.parse(decimalString), is(nullValue()));
}
}

View file

@ -1,119 +1,119 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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()));
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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()));
}
}

View file

@ -1,40 +1,40 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import org.junit.Test;
public class LongsTest {
@Test
public void testParseDecimal() {
final String nullString = null;
final String emptyString = "";
final String longString = String.valueOf(Long.MAX_VALUE);
final String decimalString = String.valueOf(Double.MAX_VALUE);
assertThat(Longs.parseDecimal(nullString), is(nullValue()));
assertThat(Longs.parseDecimal(emptyString), is(nullValue()));
assertThat(Longs.parseDecimal(longString), is(Long.MAX_VALUE));
assertThat(Longs.parseDecimal(decimalString), is((long) Double.MAX_VALUE));
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
package com.rometools.utils;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import org.junit.Test;
public class LongsTest {
@Test
public void testParseDecimal() {
final String nullString = null;
final String emptyString = "";
final String longString = String.valueOf(Long.MAX_VALUE);
final String decimalString = String.valueOf(Double.MAX_VALUE);
assertThat(Longs.parseDecimal(nullString), is(nullValue()));
assertThat(Longs.parseDecimal(emptyString), is(nullValue()));
assertThat(Longs.parseDecimal(longString), is(Long.MAX_VALUE));
assertThat(Longs.parseDecimal(decimalString), is((long) Double.MAX_VALUE));
}
}

View file

@ -1,139 +1,139 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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 testTrimToEmpty() {
final String nullString = null;
final String emptyString = "";
final String blankString = " ";
final String string = " a ";
assertThat(Strings.trimToEmpty(nullString), is(""));
assertThat(Strings.trimToEmpty(emptyString), is(""));
assertThat(Strings.trimToEmpty(blankString), is(""));
assertThat(Strings.trimToEmpty(string), is("a"));
}
@Test
public void testTrimToNull() {
final String nullString = null;
final String emptyString = "";
final String blankString = " ";
final String string = " a ";
assertThat(Strings.trimToNull(nullString), is(nullValue()));
assertThat(Strings.trimToNull(emptyString), is(nullValue()));
assertThat(Strings.trimToNull(blankString), is(nullValue()));
assertThat(Strings.trimToNull(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"));
}
}
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
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 testTrimToEmpty() {
final String nullString = null;
final String emptyString = "";
final String blankString = " ";
final String string = " a ";
assertThat(Strings.trimToEmpty(nullString), is(""));
assertThat(Strings.trimToEmpty(emptyString), is(""));
assertThat(Strings.trimToEmpty(blankString), is(""));
assertThat(Strings.trimToEmpty(string), is("a"));
}
@Test
public void testTrimToNull() {
final String nullString = null;
final String emptyString = "";
final String blankString = " ";
final String string = " a ";
assertThat(Strings.trimToNull(nullString), is(nullValue()));
assertThat(Strings.trimToNull(emptyString), is(nullValue()));
assertThat(Strings.trimToNull(blankString), is(nullValue()));
assertThat(Strings.trimToNull(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"));
}
}