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

org.wildfly.common.iteration.CompositeIterable Maven / Gradle / Ivy

There is a newer version: 2.0.1
Show newest version
/*
 * JBoss, Home of Professional Open Source.
 * Copyright 2019 Red Hat, Inc., and individual contributors
 * as indicated by the @author tags.
 *
 * 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.wildfly.common.iteration;

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

/**
 * Used for iterating over an series of iterables, thus avoiding the need to allocate/populate a new list containing all elements.
 * More efficient than the alternative when the number of iterables is arbitrary and small relative to the size of each iterable.
 * @author Paul Ferraro
 */
public class CompositeIterable implements Iterable {

    private final List> iterables;

    /**
     * Constructs a new composite iterable.
     * @param iterables a series of iterables
     */
    @SafeVarargs
    public CompositeIterable(Iterable... iterables) {
        this(Arrays.asList(iterables));
    }

    /**
     * Constructs a new composite iterable.
     * @param iterables a series of iterables
     */
    public CompositeIterable(List> iterables) {
        this.iterables = iterables;
    }

    @Override
    public Iterator iterator() {
        List> iterators = new ArrayList<>(this.iterables.size());
        for (Iterable elements : this.iterables) {
            iterators.add(elements.iterator());
        }
        return new CompositeIterator<>(iterators);
    }

    @Override
    public int hashCode() {
        int result = 1;
        for (Iterable elements : this.iterables) {
            for (T element : elements) {
                result = 31 * result + ((element != null) ? element.hashCode() : 0);
            }
        }
        return result;
    }

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof Iterable)) return false;
        @SuppressWarnings("unchecked")
        Iterator otherElements = ((Iterable) object).iterator();
        for (Iterable iterable : this.iterables) {
            Iterator elements = iterable.iterator();
            while (elements.hasNext() && otherElements.hasNext()) {
                elements.next().equals(otherElements.next());
            }
            if (elements.hasNext()) return false;
        }
        return !otherElements.hasNext();
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append('[');
        Iterator> iterables = this.iterables.iterator();
        while (iterables.hasNext()) {
            Iterator elements = iterables.next().iterator();
            while (elements.hasNext()) {
                if (builder.length() > 1) {
                    builder.append(',').append(' ');
                }
                builder.append(elements.next());
            }
        }
        return builder.append(']').toString();
    }
}