You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by kaching88 <gi...@git.apache.org> on 2015/07/11 13:07:11 UTC

[GitHub] commons-collections pull request: Add CollectionUtils#deepMerge me...

GitHub user kaching88 opened a pull request:

    https://github.com/apache/commons-collections/pull/12

    Add CollectionUtils#deepMerge method.

    This method provides fast and convenient way to merge all deepest content in iterableOfiterablesOfiterables... etc. in one chosen collection. 
    
    Method name is get from conventional Arrays.deep* methods but i am open to community suggestions.
    
    I also fixed some trivial mistakes in javadoc on CollectionUtils page. 

You can merge this pull request into a Git repository by running:

    $ git pull https://github.com/kaching88/commons-collections deepMerge

Alternatively you can review and apply these changes as the patch at:

    https://github.com/apache/commons-collections/pull/12.patch

To close this pull request, make a commit to your master/trunk branch
with (at least) the following in the commit message:

    This closes #12
    
----
commit 69524cebcc5283fa10b964672606b9ccaa0d73a7
Author: kaching88 <wa...@o2.pl>
Date:   2015-07-11T10:59:21Z

    Add CollectionUtils#deepMerge method.

----


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


[GitHub] commons-collections pull request #12: [COLLECTIONS-573] Add CollectionUtils#...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/commons-collections/pull/12


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


[GitHub] commons-collections issue #12: [COLLECTIONS-573] Add CollectionUtils#deepMer...

Posted by chtompki <gi...@git.apache.org>.
Github user chtompki commented on the issue:

    https://github.com/apache/commons-collections/pull/12
  
    Do you mind rebasing to master and re-opening this pull request?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


[GitHub] commons-collections pull request #12: [COLLECTIONS-573] Add CollectionUtils#...

Posted by Klapsa2503 <gi...@git.apache.org>.
Github user Klapsa2503 commented on a diff in the pull request:

    https://github.com/apache/commons-collections/pull/12#discussion_r197633792
  
    --- Diff: src/main/java/org/apache/commons/collections4/CollectionUtils.java ---
    @@ -1889,4 +1889,66 @@ public static int maxSize(final Collection<? extends Object> coll) {
             }
             return collection.iterator().next();
         }
    +    
    +    /**
    +     * Method recursively finds deepest content of nested iterables and 
    +     * merge them into one chosen {@link Collection}. Method accepts 
    +     * {@link Iterable} argument only if it has at least one level of nesting.
    +     * {@code Collection} argument must bound deepest elements type. Because in Java
    +     * don't exist any good and convenient way to check bound type there is no way
    +     * to prevent inserting bad values to wrong bounded types. It's possible to create
    +     * list with non valid bounded type. It will result ClassCastException throw at runtime
    +     * if {@code collection} won't be cast to proper type.      
    +     * <p>
    +     * Current implementation have time complexity {@literal O(n^k)} 
    +     * where {@literal k} is a level of iterables (1 = no nested).
    +     * </p><p>
    +     * <b>Example:</b><br>
    +     * <code>{@literal List<String> list = 
    +     * CollectionUtils.mergeDeep(Set<Set<Set<Set<String>>>> setOfSets, new ArrayList<String>)}</code>
    +     * </p>
    +     * 	If one banch of set contains ("foo","bar"), and second one contains 
    +     * ("faz" "foz") than after method use {@code list} instance contains ("foo", "bar", "faz", "foz").
    +     * @param <E> deepest element type of iterableOfiterables parameter
    +     * @param <T> {@code collection} with bounded {@literal <E>} 
    +     * @param iterableOfIterables an {@code object} which implements {@code Iterable} interface and has
    +     *  nested another {@code iterable} object 
    +     * @param collectionToFill an {@code collection} instance to fill up by deepest {@code iterable} content  
    +     * @return {@code collectionToFIll} parameter filled up by deep content {@code iterableOfIterables} parameter
    +     * @throws NullPointerException when one of a parameters is null
    +     * @throws ClassCastException at runtime if {@code collectionToFill} is not bounded with valid parameter
    +     * @since 4.1 
    +     */
    +	public static <T extends Collection<E>,E> T deepMerge(final Iterable<? extends Iterable<?>> iterableOfIterables, 
    +			final T collectionToFill) {
    +		Iterator<? extends Iterable<?>> iterator = iterableOfIterables.iterator();
    +		if (!iterator.hasNext()) {
    +			return collectionToFill;
    +		}
    +		while (iterator.hasNext()) {
    +			deepMergeRecursion(iterator.next(), collectionToFill);
    +		}
    +		return collectionToFill;
    +	}
    +	
    +	@SuppressWarnings("unchecked")
    +	private static <T extends Collection<E>,E> void deepMergeRecursion(final Iterable<?> iterable,
    +			final T collectionToFill ) {
    +		Iterator<?> iterator = iterable.iterator();
    +		if (!iterator.hasNext()) {
    +			return;
    +		}
    +		Object firstElement = iterator.next();
    +		if (!(firstElement instanceof Iterable<?>)) {
    +			collectionToFill.add((E)firstElement);
    +			while (iterator.hasNext()) {
    +				collectionToFill.add((E)iterator.next());
    +			}
    +			return;
    +		}
    +		deepMergeRecursion((Iterable<?>) firstElement, collectionToFill);
    +		while (iterator.hasNext()) {
    +			deepMergeRecursion((Iterable<?>) iterator.next(), collectionToFill);
    --- End diff --
    
    I suggest to change the order here:
    1. change the condition `if (!(firstElement instanceof Iterable<?>)) {` -> `if (firstElement instanceof Iterable<?>) {`
    2. add the `else `statement
    3. remove `return;`
    
    this will simplify the code as there will be:
    * more clear condition
    * no return statement in the middle of the method
    
    



---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org


[GitHub] commons-collections pull request #12: [COLLECTIONS-573] Add CollectionUtils#...

Posted by asfgit <gi...@git.apache.org>.
Github user asfgit closed the pull request at:

    https://github.com/apache/commons-collections/pull/12


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org
For additional commands, e-mail: dev-help@commons.apache.org