Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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 org.cdlflex.ui.util;
 
 import java.util.List;
 import java.util.Map;

Such λ. So Fn. Wow.
 
 public final class Collections {
 
     private Collections() {
         // Much utility.
     }

    
Returns an empty list from java.util.Collections.

Parameters:
<T> the element type
Returns:
empty list
 
     public static <T> List<T> emptyList() {
         return java.util.Collections.emptyList();
     }

    
Returns an empty map from java.util.Collections.

Parameters:
<K> the map key type
<V> the map value type
Returns:
an empty map
 
     public static <K, V> Map<K, V> emptyMap() {
         return java.util.Collections.emptyMap();
     }

    
Checks if the given Collection is null or empty.

Parameters:
collection the collection to check
Returns:
true if the Collection is null or empty.
 
     public static boolean isEmpty(Collection<?> collection) {
         return collection == null || collection.isEmpty();
     }

    
Applies the given visitor to each element in the collection.

Parameters:
collection the collection to iterate
visitor the visitor
<T> collection type
<E> exception type
Throws:
E an exception
 
     public static <T, E extends Exceptionvoid walk(Iterable<T> collectionCheckedVisitor<T, E> visitorthrows E {
         for (T element : collection) {
             visitor.visit(element);
         }
     }

    
Applies the given visitor to each element in the collection. If the visitor returns BREAK, the loop is exited.

Parameters:
collection the collection to iterate
visitor the visitor
<T> collection type
<E> exception type
Throws:
E an exception
 
     public static <T, E extends Exceptionvoid walk(Iterable<T> collectionTerminatingCheckedVisitor<T, E> visitor)
         throws E {
         for (T element : collection) {
             if (visitor.call(element) == .) {
                 break;
             }
         }
     }

    
Returns a list obtained by applying the given callback to each element in the list.

Parameters:
collection the collection to iterate
fn the callback
<A> argument type
<R> return type
<E> exception type
Returns:
a list of elements
Throws:
E an exception
    public static <A, R, E extends ExceptionList<R> map(Collection<A> collectionCheckedCallback<A, R, E> fn)
        throws E {
        List<R> result = new ArrayList<>(collection.size());
        for (A object : collection) {
            result.add(fn.call(object));
        }
        return result;
    }

    
Returns a list of all elements of the collection that satisfy the predicate.

Parameters:
collection the collection to iterate
predicate the predicate
<T> collection element type
Returns:
a list of all elements of the collection that satisfy the predicate
    public static <T> List<T> filter(Collection<T> collectionCallback<T, Booleanpredicate) {
        List<T> result = new ArrayList<>(collection.size());
        for (T object : collection) {
            if (predicate.call(object)) {
                result.add(object);
            }
        }
        return result;
    }

    
Retain all elements of the given iterable that satisfy the given predicate. Others will be removed via java.util.Iterator.remove().

Parameters:
iterable the iterable, iterators must support java.util.Iterator.remove()
predicate the predicate
<T> element type
    public static <T> void retain(Iterable<T> iterableCallback<T, Booleanpredicate) {
        Iterator<T> iterator = iterable.iterator();
        while (iterator.hasNext()) {
            if (!predicate.call(iterator.next())) {
                iterator.remove();
            }
        }
    }

    
Removes all objects from the given collection that satisfy the given predicate. Requires a collection that supports java.util.Iterator.remove().

Parameters:
collection the collection
predicate the predicate
<T> collection type
    public static <T> void remove(Iterable<T> collectionCallback<T, Booleanpredicate) {
        Iterator<T> iterator = collection.iterator();
        while (iterator.hasNext()) {
            if (predicate.call(iterator.next())) {
                iterator.remove();
            }
        }
    }

    
Counts the amount of elements that satisfy the given predicate.

Parameters:
collection the collection to iterate
predicate the predicate
<T> collection element type
Returns:
the amount of elements that satisfy the given predicate.
    public static <T> int count(Collection<T> collectionCallback<T, Booleanpredicate) {
        int cnt = 0;
        for (T object : collection) {
            if (predicate.call(object)) {
                cnt++;
            }
        }
        return cnt;
    }

    
Returns the first element of the collection that satisfies the predicate.

Parameters:
collection the collection to iterate
predicate the predicate
<T> collection element type
Returns:
the first element of the collection that satisfies the predicate
    public static <T> T first(Iterable<T> collectionCallback<T, Booleanpredicate) {
        for (T object : collection) {
            if (predicate.call(object)) {
                return object;
            }
        }
        return null;
    }

    
Determines if all elements of the collection satisfies the predicate.

Parameters:
collection the collection to iterate
predicate the predicate
<T> collection element type
Returns:
true if all element satisfy the predicate
    public static <T> boolean all(Iterable<T> collectionCallback<T, Booleanpredicate) {
        boolean bool = true;
        for (T object : collection) {
            bool &= predicate.call(object);
        }
        return bool;
    }

    
Determines if any element of the collection satisfies the predicate.

Parameters:
collection the collection to iterate
predicate the predicate
<T> collection element type
Returns:
true as soon as an element is found that satisfies the predicate
    public static <T> boolean any(Iterable<T> collectionCallback<T, Booleanpredicate) {
        for (T object : collection) {
            if (predicate.call(object)) {
                return true;
            }
        }
        return false;
    }

    
Adds every element in the given Iterable into a new list and returns it.

Parameters:
iterable the iterable
<T> the element type
Returns:
a new array list of all elements in the iterable
    public static <T> List<T> asList(Iterable<T> iterable) {
        List<T> list = new ArrayList<>();
        for (T e : iterable) {
            list.add(e);
        }
        return list;
    }

    
Sorts a list.

Parameters:
list list to sort
<T> Collection element type
See also:
java.util.Collections.sort(java.util.List)
    public static <T extends Comparable<? super T>> void sort(List<T> list) {
        java.util.Collections.sort(list);
    }

    
Sorts a list.

Parameters:
list list to sort
comparator comparator to use
<T> Collection element type
See also:
java.util.Collections.sort(java.util.List,java.util.Comparator)
    public static <T> void sort(List<T> listComparator<T> comparator) {
        java.util.Collections.sort(listcomparator);
    }

    
A functional interface for a callback that may throw a checked exception.

Parameters:
<A> argument type
<R> return type
<E> exception type
    public interface CheckedCallback<A, R, E extends Exception> {

        
Execute a call on the given object.

Parameters:
object the target object
Returns:
a return value
Throws:
E an exception
        R call(A objectthrows E;
    }

    
A functional interface for a callback that may throw an unchecked exception.

Parameters:
<A> argument type
<R> return type
    public interface Callback<A, R> extends CheckedCallback<A, R, RuntimeException> {
        @Override
        R call(A object);
    }

    
A callback that returns a boolean.

Parameters:
<T> element type
    public interface Predicate<T> extends Callback<T, Boolean> {
        @Override
        Boolean call(T object);
    }

    
A functional interface for visiting elements in a collection that may throw a checked exception.

Parameters:
<T> element type
<E> exception type
    public interface CheckedVisitor<T, E extends Exception> {

        
Visit the given object.

Parameters:
object the object to visit
Throws:
E an exception
        void visit(T objectthrows E;
    }

    
A functional interface for visiting elements in a collection that may throw an unchecked exception.

Parameters:
<T> element type
    public interface Visitor<T> extends CheckedVisitor<T, RuntimeException> {
        @Override
        void visit(T object);
    }

    
A functional interface for visiting elements in a collection that may throw a checked exception and provide an additional loop termination action.

Parameters:
<T> element type
<E> exception type
    public interface TerminatingCheckedVisitor<T, E extends Exceptionextends CheckedCallback<T, VisitorAction, E> {
        @Override
        VisitorAction call(T objectthrows E;
    }

    
A functional interface for visiting elements in a collection that may throw an unchecked exception and provide an additional loop termination action.

Parameters:
<T> element type
    public interface TerminatingVisitor<T> extends TerminatingCheckedVisitor<T, RuntimeException>,
            Callback<T, VisitorAction> {
        @Override
        VisitorAction call(T object);
    }

    
The return type of a TerminatingCheckedVisitor or TerminatingVisitor.
    public static enum VisitorAction {
        CONTINUE,
        BREAK
    }

    
Predicate implementation that returns true if the given object is not null.

Parameters:
<T> element type
    public static class NotNullFilter<T> implements Predicate<T> {
        @Override
        public Boolean call(T object) {
            return object != null;
        }
    }
New to GrepCode? Check out our FAQ X