Java Iterable equal(Iterable it1, Iterable it2)

Here you can find the source of equal(Iterable it1, Iterable it2)

Description

equal

License

Open Source License

Declaration

public static boolean equal(Iterable it1, Iterable it2) 

Method Source Code

//package com.java2s;
/**//  w w  w .  ja  v  a  2s.  c o m
 * Copyright (C) 2012 - present by OpenGamma Inc. and the OpenGamma group of companies
 *
 * Please see distribution for license.
 */

import java.util.Iterator;

public class Main {
    public static boolean equal(Object o1, Object o2) {
        Iterator i1 = null;
        Iterator i2 = null;

        if (o1 instanceof Iterator) {
            i1 = (Iterator) o1;
        } else if (o1 instanceof Iterable) {
            i1 = ((Iterable) o1).iterator();
        }
        if (o2 instanceof Iterator) {
            i2 = (Iterator) o2;
        } else if (o2 instanceof Iterable) {
            i2 = ((Iterable) o2).iterator();
        }

        if (i1 != null && i2 != null) {
            return equal(i1, i2);
        } else {
            if (o1 == null && o2 == null) {
                return true;
            } else if (o1 != null && o2 != null) {
                return o1.equals(o2) || o2.equals(o1);
            } else {
                return false;
            }
        }
    }

    public static boolean equal(Iterable it1, Iterable it2) {
        Iterator i1 = it1.iterator();
        Iterator i2 = it2.iterator();
        return equal(i1, i2);
    }

    public static boolean equal(Iterator i1, Iterator i2) {
        if (i1 == i2)
            return true;
        if ((i1.hasNext() && !i2.hasNext()) || (!i1.hasNext() && i2.hasNext())) {
            return false;
        }
        while (i1.hasNext() && i2.hasNext()) {
            Object e1 = i1.next();
            Object e2 = i2.next();
            if (e1 != e2 && !equal(e1, e2))
                return false;
            if ((i2.hasNext() && !i1.hasNext()) || (!i2.hasNext() && i1.hasNext()))
                return false;
        }
        return true;
    }
}

Related

  1. commafy(Iterable col, StringBuilder builder)
  2. containsSame(Iterable coll, Object obj)
  3. convertIterable(final Iterable> iterables)
  4. createDelimitedString(Iterable iterable, String delimiter)
  5. createUpdateTemplate(String table, Iterable columns)
  6. equalsIterablesInOrder(Iterable i1, Iterable i2)
  7. fillFromIterable(C c, Iterable i)
  8. getAny(Iterable iterable)
  9. getByIndex(Iterable iterable, int index)