-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,496 additions
and
1 deletion.
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
106 changes: 106 additions & 0 deletions
106
...form/support/support/src/main/java/de/iip_ecosphere/platform/support/CollectionUtils.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,106 @@ | ||
/** | ||
* ****************************************************************************** | ||
* Copyright (c) {2020} The original author or authors | ||
* | ||
* All rights reserved. This program and the accompanying materials are made | ||
* available under the terms of the Eclipse Public License 2.0 which is available | ||
* at http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 | ||
* which is available at https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 OR EPL-2.0 | ||
********************************************************************************/ | ||
|
||
package de.iip_ecosphere.platform.support; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* Some useful additional collection methods. | ||
* | ||
* @author Holger Eichelberger, SSE | ||
*/ | ||
public class CollectionUtils { | ||
|
||
/** | ||
* Turns given {@code elements} into a list. | ||
* | ||
* @param <T> the element type | ||
* @param elements the elements | ||
* @return the list containing all {@code elements} | ||
*/ | ||
@SafeVarargs | ||
public static <T> List<T> toList(T... elements) { | ||
List<T> result = new ArrayList<T>(); | ||
for (T e : elements) { | ||
result.add(e); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Turns given {@code elements} to {@code list}. | ||
* | ||
* @param <T> the element type | ||
* @param list the list to be modified as a side effect | ||
* @param elements the elements | ||
* @return the list with all {@code elements} added | ||
*/ | ||
@SafeVarargs | ||
public static <T> List<T> addAll(List<T> list, T... elements) { | ||
for (T e : elements) { | ||
list.add(e); | ||
} | ||
return list; | ||
} | ||
|
||
/** | ||
* Turns given {@code elements} to {@code set}. | ||
* | ||
* @param <T> the element type | ||
* @param set the set to be modified as a side effect | ||
* @param elements the elements | ||
* @return the set with all {@code elements} added | ||
*/ | ||
@SafeVarargs | ||
public static <T> Set<T> addAll(Set<T> set, T... elements) { | ||
for (T e : elements) { | ||
set.add(e); | ||
} | ||
return set; | ||
} | ||
|
||
/** | ||
* Turns the elements in the iterator into a list. | ||
* | ||
* @param <T> the element type | ||
* @param iterator the iterator | ||
* @return the list with all elements in {@code iterator} | ||
*/ | ||
public static <T> List<T> toList(Iterator<T> iterator) { | ||
List<T> result = new ArrayList<T>(); | ||
while (iterator.hasNext()) { | ||
result.add(iterator.next()); | ||
} | ||
return result; | ||
} | ||
|
||
/** | ||
* Turns the elements in the iterator into a set. | ||
* | ||
* @param <T> the element type | ||
* @param iterator the iterator | ||
* @return the set with all elements in {@code iterator} | ||
*/ | ||
public static <T> Set<T> toSet(Iterator<T> iterator) { | ||
Set<T> result = new HashSet<T>(); | ||
while (iterator.hasNext()) { | ||
result.add(iterator.next()); | ||
} | ||
return result; | ||
} | ||
|
||
} |
Oops, something went wrong.