All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.xmlunit.util.Linqy Maven / Gradle / Ivy

There is a newer version: 2.10.0
Show newest version
/*
  This file is licensed to You 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.xmlunit.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * A couple of (functional) sequence processing constructs.
 */
public final class Linqy {

    private Linqy() { /* no instances */ }

    /**
     * Turns the iterable into a list.
     * @param i the iterable
     * @param  element type
     * @return a list containing all elements of the Iterable passed in
     */
    public static  List asList(Iterable i) {
        if (i instanceof Collection) {
            return new ArrayList((Collection) i);
        }
        ArrayList a = new ArrayList();
        for (E e : i) {
            a.add(e);
        }
        return a;
    }

    /**
     * Turns an iterable into its type-safe cousin.
     * @param i the iterable
     * @param  target element type
     * @return a type-safe iterable containing all elements of the Iterable passed in
     */
    public static  Iterable cast(final Iterable i) {
        return map(i, new Mapper() {
                public E apply(Object o) {
                    return (E) o;
                }
            });
    }

    /**
     * An iterable containing a single element.
     * @param single the element of the iterable to return
     * @param  element type
     * @return an Iterable returning {@code single} once and only once
     */
    public static  Iterable singleton(final E single) {
        return new Iterable() {
            @Override
            public Iterator iterator() {
                return new OnceOnlyIterator(single);
            }
        };
    }

    /**
     * Create a new iterable by applying a mapper function to each
     * element of a given sequence.
     * @param from the iterable to transform
     * @param mapper the function to apply to each element of {@code from}
     * @param  source element type
     * @param  target element type
     * @return an iterable where each element is the result of applying the function to an element of the original
     * iterable
     */
    public static  Iterable map(final Iterable from,
                                         final Mapper mapper) {
        return new Iterable() {
            @Override
            public Iterator iterator() {
                return new MappingIterator(from.iterator(), mapper);
            }
        };
    }

    /**
     * Exclude all elements from an iterable that don't match a given
     * predicate.
     * @param sequence the iterable to filter
     * @param filter the predicate to apply
     * @param  element type
     * @return an iterable containing all elements of the original sequence that match the predicate
     */
    public static  Iterable filter(final Iterable sequence,
                                         final Predicate filter) {
        return new Iterable() {
            @Override
            public Iterator iterator() {
                return new FilteringIterator(sequence.iterator(), filter);
            }
        };
    }

    /**
     * Count the number of elements in a sequence.
     * @param seq the sequence to count
     * @return the number of elements in the sequence
     */
    public static int count(Iterable seq) {
        if (seq instanceof Collection) {
            return ((Collection) seq).size();
        }
        int c = 0;
        Iterator it = seq.iterator();
        while (it.hasNext()) {
            c++;
            it.next();
        }
        return c;
    }

    /**
     * Determines whether a given predicate holds true for at least
     * one element.
     *
     * 

Returns false for an empty sequence.

* * @param sequence the sequence to examine * @param predicate the predicate to test * @param element type * @return true if any element of the sequence matches the predicate */ public static boolean any(final Iterable sequence, final Predicate predicate) { for (T t : sequence) { if (predicate.test(t)) { return true; } } return false; } /** * Determines whether a given predicate holds true for all * elements. * *

Returns true for an empty sequence.

* * @param sequence the sequence to examine * @param predicate the predicate to test * @param element type * @return true if all elements of the sequence match the predicate */ public static boolean all(final Iterable sequence, final Predicate predicate) { for (T t : sequence) { if (!predicate.test(t)) { return false; } } return true; } private static class OnceOnlyIterator implements Iterator { private final E element; private boolean iterated = false; private OnceOnlyIterator(E element) { this.element = element; } @Override public void remove() { throw new UnsupportedOperationException(); } @Override public E next() { if (iterated) { throw new NoSuchElementException(); } iterated = true; return element; } @Override public boolean hasNext() { return !iterated; } } private static class MappingIterator implements Iterator { private final Iterator i; private final Mapper mapper; private MappingIterator(Iterator i, Mapper mapper) { this.i = i; this.mapper = mapper; } @Override public void remove() { i.remove(); } @Override public T next() { return mapper.apply(i.next()); } @Override public boolean hasNext() { return i.hasNext(); } } private static class FilteringIterator implements Iterator { private final Iterator i; private final Predicate filter; private T lookAhead = null; private FilteringIterator(Iterator i, Predicate filter) { this.i = i; this.filter = filter; hasNext(); // allow next() to be called without hasNext() } @Override public void remove() { i.remove(); } @Override public T next() { if (lookAhead == null) { throw new NoSuchElementException(); } T next = lookAhead; lookAhead = null; return next; } @Override public boolean hasNext() { while (lookAhead == null && i.hasNext()) { T next = i.next(); if (filter.test(next)) { lookAhead = next; } } return lookAhead != null; } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy