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

org.arp.javautil.collections.Iterators Maven / Gradle / Ivy

Go to download

JavaUtil is a utility library to speed the development of Java software. We developed it over multiple years during our internal development efforts, and it has reached a point of stability where we have decided to make it available to the outside world. The JavaUtil package aims to fill in the gaps of the Apache Commons and similar utility libraries out there. Features include convenience classes for string, collections, arrays, dates, iterators, colors, logging, unit testing, a little bit of basic statistics, database queries, caching and more.

There is a newer version: 4.5
Show newest version
/*
 * #%L
 * JavaUtil
 * %%
 * Copyright (C) 2012 - 2013 Emory University
 * %%
 * 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.
 * #L%
 */
package org.arp.javautil.collections;

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

public class Iterators {

    private Iterators() {
    }

    /**
     * Tests two iterators for equality, meaning that they have the same
     * elements enumerated in the same order.
     *
     * @param itr1
     *            an Iterator.
     * @param itr2
     *            an Iterator.
     * @return true if the two iterators are not
     *         null and equal, false otherwise.
     */
    public static boolean equal(Iterator itr1, Iterator itr2) {
        if (itr1 == null || itr2 == null) {
            return false;
        } else {
            while (itr1.hasNext() && itr2.hasNext()) {
                Object i = itr1.next();
                Object i2 = itr2.next();
                if ((i == null && i2 != null) || !(i.equals(i2))) {
                    return false;
                }
            }
            if (itr1.hasNext() || itr2.hasNext()) {
                return false;
            }
            return true;
        }
    }

    /**
     * Returns an iterator as a list.
     *
     * @param itr
     *            an Iterator.
     * @return a List.
     */
    public static  List asList(Iterator itr) {
        List l = new ArrayList<>();
        if (itr != null) {
            while (itr.hasNext()) {
                l.add(itr.next());
            }
        }
        return l;
    }

    /**
     * Returns an iterator as a collection.
     *
     * @param itr
     *            an Iterator.
     * @return a Collection.
     */
    public static  Collection asCollection(Iterator itr) {
        return asList(itr);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy