-
Notifications
You must be signed in to change notification settings - Fork 495
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #4108: Remove org.apache.commons.collections* imports
- Loading branch information
Showing
13 changed files
with
165 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
extended/src/main/java/apoc/util/ExtendedCollectionUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package apoc.util; | ||
|
||
import java.lang.reflect.Array; | ||
import java.util.Collection; | ||
import java.util.Enumeration; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
public class ExtendedCollectionUtils { | ||
|
||
/** | ||
* NOTE: equivalent to `org.apache.commons.collections4.CollectionUtils` homonym method | ||
* | ||
* Gets the size of the collection/iterator specified. | ||
* <p> | ||
* This method can handles objects as follows | ||
* </p> | ||
* <ul> | ||
* <li>Collection - the collection size | ||
* <li>Map - the map size | ||
* <li>Array - the array size | ||
* <li>Iterator - the number of elements remaining in the iterator | ||
* <li>Enumeration - the number of elements remaining in the enumeration | ||
* </ul> | ||
* | ||
* @param object the object to get the size of, may be null | ||
* @return the size of the specified collection or 0 if the object was null | ||
* @throws IllegalArgumentException thrown if object is not recognized | ||
* @since 3.1 | ||
*/ | ||
public static int size(final Object object) { | ||
if (object == null) { | ||
return 0; | ||
} | ||
int total = 0; | ||
if (object instanceof Map<?,?>) { | ||
total = ((Map<?, ?>) object).size(); | ||
} else if (object instanceof Collection<?>) { | ||
total = ((Collection<?>) object).size(); | ||
} else if (object instanceof Iterable<?>) { | ||
total = sizeIterable((Iterable<?>) object); | ||
} else if (object instanceof Object[]) { | ||
total = ((Object[]) object).length; | ||
} else if (object instanceof Iterator<?>) { | ||
total = sizeIterator((Iterator<?>) object); | ||
} else if (object instanceof Enumeration<?>) { | ||
final Enumeration<?> it = (Enumeration<?>) object; | ||
while (it.hasMoreElements()) { | ||
total++; | ||
it.nextElement(); | ||
} | ||
} else { | ||
try { | ||
total = Array.getLength(object); | ||
} catch (final IllegalArgumentException ex) { | ||
throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName()); | ||
} | ||
} | ||
return total; | ||
} | ||
|
||
/** | ||
* Returns the number of elements contained in the given iterator. | ||
* <p> | ||
* A <code>null</code> or empty iterator returns {@code 0}. | ||
* | ||
* @param iterator the iterator to check, may be null | ||
* @return the number of elements contained in the iterator | ||
* @since 4.1 | ||
*/ | ||
public static int sizeIterator(final Iterator<?> iterator) { | ||
int size = 0; | ||
if (iterator != null) { | ||
while (iterator.hasNext()) { | ||
iterator.next(); | ||
size++; | ||
} | ||
} | ||
return size; | ||
} | ||
|
||
/** | ||
* Returns the number of elements contained in the given iterator. | ||
* <p> | ||
* A <code>null</code> or empty iterator returns {@code 0}. | ||
* | ||
* @param iterable the iterable to check, may be null | ||
* @return the number of elements contained in the iterable | ||
*/ | ||
public static int sizeIterable(final Iterable<?> iterable) { | ||
if (iterable instanceof Collection<?>) { | ||
return ((Collection<?>) iterable).size(); | ||
} | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package apoc.util; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.List; | ||
|
||
public class ExtendedListUtils { | ||
|
||
/** | ||
* Returns a new list containing the second list appended to the | ||
* first list. The {@link List#addAll(Collection)} operation is | ||
* used to append the two given lists into a new list. | ||
* | ||
* @param <E> the element type | ||
* @param list1 the first list | ||
* @param list2 the second list | ||
* @return a new list containing the union of those lists | ||
* @throws NullPointerException if either list is null | ||
*/ | ||
public static <E> List<E> union(final List<? extends E> list1, final List<? extends E> list2) { | ||
final ArrayList<E> result = new ArrayList<>(list1.size() + list2.size()); | ||
result.addAll(list1); | ||
result.addAll(list2); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package apoc.util; | ||
|
||
import java.util.Map; | ||
|
||
public class ExtendedMapUtils { | ||
|
||
public static int size(final Map<?, ?> map) { | ||
return map == null ? 0 : map.size(); | ||
} | ||
|
||
public static boolean isEmpty(final Map<?,?> map) { | ||
return map == null || map.isEmpty(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters