Example usage for com.google.common.collect Iterators elementsEqual

List of usage examples for com.google.common.collect Iterators elementsEqual

Introduction

In this page you can find the example usage for com.google.common.collect Iterators elementsEqual.

Prototype

public static boolean elementsEqual(Iterator<?> iterator1, Iterator<?> iterator2) 

Source Link

Document

Determines whether two iterators contain equal elements in the same order.

Usage

From source file:com.linkedin.pinot.common.utils.JsonAssert.java

/**
 * Compare two JSON objects, ignoring field order. For example, objects {a:1, b:2} and {b:2, a:1} are equals, even
 * though they are not using string comparison.
 *
 * @param actual The actual JSON object//from  w w w .j  a va 2s .  com
 * @param expected The expected JSON object
 */
public static void assertEqualsIgnoreOrder(String actual, String expected) {
    try {
        JSONObject actualObject = new JSONObject(actual);
        JSONObject expectedObject = new JSONObject(expected);

        // Check that both objects have the same keys
        Assert.assertTrue(Iterators.elementsEqual(actualObject.sortedKeys(), expectedObject.sortedKeys()),
                "JSON objects don't have the same keys, expected:<"
                        + Iterators.toString(expectedObject.sortedKeys()) + "> but was:<"
                        + Iterators.toString(actualObject.sortedKeys()) + ">");

        // Iterate over all the keys of one element and compare their contents
        Iterator<String> objectKeys = actualObject.keys();
        while (objectKeys.hasNext()) {
            String key = objectKeys.next();
            Object actualValue = actualObject.get(key);
            Object expectedValue = expectedObject.get(key);

            Assert.assertTrue(actualValue.getClass().equals(expectedValue.getClass()),
                    "Objects with key " + key + " don't have the same class, expected:<" + expectedValue
                            + "> but was:<" + actualValue + ">");
            if (actualValue instanceof JSONObject) {
                assertEqualsIgnoreOrder(actualValue.toString(), expectedValue.toString());
            } else {
                Assert.assertEquals(actualValue.toString(), expectedValue.toString(),
                        "Objects with key " + key + " don't have the same value");
            }
        }
    } catch (JSONException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:org.apache.cassandra.hints.HintsTestUtil.java

static void assertPartitionsEqual(AbstractBTreePartition expected, AbstractBTreePartition actual) {
    assertEquals(expected.partitionKey(), actual.partitionKey());
    assertEquals(expected.deletionInfo(), actual.deletionInfo());
    assertEquals(expected.columns(), actual.columns());
    assertTrue(Iterators.elementsEqual(expected.iterator(), actual.iterator()));
}

From source file:com.torodb.mongowp.bson.abst.AbstractBsonArray.java

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }/* w ww. j a v a 2 s . co m*/
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof BsonArray)) {
        return false;
    }
    BsonArray other = (BsonArray) obj;
    if (this.size() != other.size()) {
        return false;
    }
    return Iterators.elementsEqual(this.iterator(), other.iterator());
}

From source file:cc.kave.commons.pointsto.extraction.Callpath.java

@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Callpath other = (Callpath) obj;/*w  w  w .j  a v a 2  s. co  m*/
    if (path == null) {
        if (other.path != null)
            return false;
    } else if (path.size() != other.path.size()) {
        return false;
    } else if (!Iterators.elementsEqual(path.iterator(), other.path.iterator())) {
        return false;
    }

    return true;
}

From source file:org.apache.cassandra.engine.AbstractRow.java

@Override
public boolean equals(Object other) {
    if (!(other instanceof Row))
        return false;

    Row left = this;
    Row right = (Row) other;//from ww  w .j  a  va 2s.  c o m

    if (left.clusteringSize() != right.clusteringSize())
        return false;

    for (int i = 0; i < left.clusteringSize(); i++)
        if (!left.getClusteringColumn(i).equals(right.getClusteringColumn(i)))
            return false;

    return Iterators.elementsEqual(left.iterator(), right.iterator());
}

From source file:com.eightkdata.mongowp.bson.abst.AbstractBsonDocument.java

@Override
public boolean equals(Object obj) {
    if (this == obj) {
        return true;
    }/*from w ww  .  j  a va 2s.c  o  m*/
    if (obj == null) {
        return false;
    }
    if (!(obj instanceof BsonDocument)) {
        return false;
    }
    return Iterators.elementsEqual(this.iterator(), ((BsonDocument) obj).iterator());
}

From source file:com.github.rinde.datgen.pdptw.Dataset.java

@Override
public boolean equals(@Nullable Object other) {
    if (!(other instanceof Dataset<?>)) {
        return false;
    }/*from w w  w  .  j a v  a 2 s. co m*/

    final Dataset<?> o = (Dataset<?>) other;
    return Iterators.elementsEqual(iterator(), o.iterator());
}

From source file:com.google.cloud.bigquery.TableResult.java

@Override
public final boolean equals(Object obj) {
    if (obj == this) {
        return true;
    }/*from   w ww.jav  a2 s  .  co m*/
    if (obj == null || !obj.getClass().equals(TableResult.class)) {
        return false;
    }
    TableResult response = (TableResult) obj;
    return Objects.equals(getNextPageToken(), response.getNextPageToken())
            && Iterators.elementsEqual(getValues().iterator(), response.getValues().iterator())
            && Objects.equals(schema, response.schema) && totalRows == response.totalRows;
}

From source file:org.grouplens.grapht.util.AbstractChain.java

@Override
public boolean equals(Object o) {
    if (o == this) {
        return true;
    } else if (o instanceof AbstractChain) {
        // optimize comparing two chains
        return Iterators.elementsEqual(reverseIterator(), ((AbstractChain) o).reverseIterator());
    } else {//w  w  w.j  a va 2 s.  c om
        return super.equals(o);
    }
}

From source file:org.apache.cassandra.utils.IntervalTree.java

@Override
public boolean equals(Object o) {
    if (!(o instanceof IntervalTree))
        return false;
    IntervalTree that = (IntervalTree) o;
    return Iterators.elementsEqual(iterator(), that.iterator());
}