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.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.
     */
    public static  List asList(Iterable i) {
        ArrayList a = new ArrayList();
        for (E e : i) {
            a.add(e);
        }
        return a;
    }

    /**
     * Turns an iterable into its type-safe cousin.
     */
    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.
     */
    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.
     */
    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.
     */
    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.
     */
    public static int count(Iterable seq) {
        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.

*/ 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.

*/ 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