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 new file mode 100644 index 0000000..6b0b127 --- /dev/null +++ b/LICENSE @@ -0,0 +1,203 @@ + + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + 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 "[]" + 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 + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + 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. + 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. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..18a85fb --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +rome-utils +========== + +Utility classes for ROME projects diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..2945b69 --- /dev/null +++ b/pom.xml @@ -0,0 +1,66 @@ + + + 4.0.0 + + + com.rometools + rome-parent + 1.6.0-SNAPSHOT + + + rome-utils + 1.6.0-SNAPSHOT + jar + + rome-utils + + Utility classes for ROME projects + + http://rometools.github.io/rome-utils/ + + + scm:git:ssh://github.com/rometools/rome-utils.git + scm:git:ssh://git@github.com/rometools/rome-utils.git + https://github.com/rometools/rome-utils + + + + + Patrick Gotthard + http://www.patrick-gotthard.de + +1 + + + + + + sonatype-nexus-snapshots + https://oss.sonatype.org/content/repositories/snapshots + + false + + + true + + + + + + + org.slf4j + slf4j-api + + + junit + junit + test + + + org.hamcrest + hamcrest-library + 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..8baea38 --- /dev/null +++ b/src/main/java/com/rometools/utils/Alternatives.java @@ -0,0 +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 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..c1149f7 --- /dev/null +++ b/src/main/java/com/rometools/utils/Dates.java @@ -0,0 +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()); + } + } + +} diff --git a/src/main/java/com/rometools/utils/Doubles.java b/src/main/java/com/rometools/utils/Doubles.java new file mode 100644 index 0000000..7f4f8fd --- /dev/null +++ b/src/main/java/com/rometools/utils/Doubles.java @@ -0,0 +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; + } + +} diff --git a/src/main/java/com/rometools/utils/IO.java b/src/main/java/com/rometools/utils/IO.java new file mode 100644 index 0000000..7c7c57f --- /dev/null +++ b/src/main/java/com/rometools/utils/IO.java @@ -0,0 +1,63 @@ +/* + * 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.io.Closeable; +import java.io.IOException; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Utility class to handle I/O operations. + * + * @author Patrick Gotthard + * + */ +public final class IO { + + private static final Logger LOG = LoggerFactory.getLogger(IO.class); + + private IO() { + } + + /** + * Closes a {@link Closeable} object without suppressing a possible {@link IOException}. + * + * @param closeable The {@link Closeable} to close + * @throws IOException when the {@link Closeable} can't be closed + */ + public static void close(final Closeable closeable) throws IOException { + if (closeable != null) { + closeable.close(); + } + } + + /** + * Closes a {@link Closeable} object and suppresses a possible {@link IOException}. + * + * @param closeable The {@link Closeable} to close + */ + public static void closeQuietly(final Closeable closeable) { + if (closeable != null) { + try { + closeable.close(); + } catch (final IOException e) { + LOG.warn("Unable to close resource", e); + } + } + } + +} diff --git a/src/main/java/com/rometools/utils/Integers.java b/src/main/java/com/rometools/utils/Integers.java new file mode 100644 index 0000000..8694fdf --- /dev/null +++ b/src/main/java/com/rometools/utils/Integers.java @@ -0,0 +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; + } + } + +} 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..5beff55 --- /dev/null +++ b/src/main/java/com/rometools/utils/Lists.java @@ -0,0 +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 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/Longs.java b/src/main/java/com/rometools/utils/Longs.java new file mode 100644 index 0000000..023e1cf --- /dev/null +++ b/src/main/java/com/rometools/utils/Longs.java @@ -0,0 +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; + } + +} 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..03ab2fd --- /dev/null +++ b/src/main/java/com/rometools/utils/Strings.java @@ -0,0 +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); + } + } + +} 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..09dc66f --- /dev/null +++ b/src/test/java/com/rometools/utils/AlternativesTest.java @@ -0,0 +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())); + + } + +} 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..5738169 --- /dev/null +++ b/src/test/java/com/rometools/utils/DatesTest.java @@ -0,0 +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())); + + } + +} diff --git a/src/test/java/com/rometools/utils/DoublesTest.java b/src/test/java/com/rometools/utils/DoublesTest.java new file mode 100644 index 0000000..342f990 --- /dev/null +++ b/src/test/java/com/rometools/utils/DoublesTest.java @@ -0,0 +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)); + + } + +} diff --git a/src/test/java/com/rometools/utils/IntegersTest.java b/src/test/java/com/rometools/utils/IntegersTest.java new file mode 100644 index 0000000..6893b08 --- /dev/null +++ b/src/test/java/com/rometools/utils/IntegersTest.java @@ -0,0 +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())); + + } + +} 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..25e7861 --- /dev/null +++ b/src/test/java/com/rometools/utils/ListsTest.java @@ -0,0 +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 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/LongsTest.java b/src/test/java/com/rometools/utils/LongsTest.java new file mode 100644 index 0000000..0955cfd --- /dev/null +++ b/src/test/java/com/rometools/utils/LongsTest.java @@ -0,0 +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)); + + } + +} 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..ce72aa4 --- /dev/null +++ b/src/test/java/com/rometools/utils/StringsTest.java @@ -0,0 +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")); + } + +} diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore new file mode 100644 index 0000000..e69de29