Skip to content Skip to sidebar Skip to footer

Comparing Android Intent Objects

I have 2 android intent objects that can be persisted as URLs and then rehydrated back into intent objects. I'm wondering what is the most effective way to compare any 2 intent ob

Solution 1:

That's probably as good as it gets, at least conceptually. However, I don't think your algorithm covers cases where b has a key that a does not.

I'd get both keySet() values and run an equals() on those, to confirm they both have the same keys. Then, iterate over one and run equals() on the value pair.

Solution 2:

Improving upon @aostiles' answer by adding the missing return statement and also conditions to compare arrays in extras:

private boolean intentsAreEqual (Intent a, Intent b)
    {
        if (a.filterEquals(b)) {
            if (a.getExtras() != null && b.getExtras() != null) {
                // check if the keysets are the same sizeif (a.getExtras().keySet().size() != b.getExtras().keySet().size()) returnfalse;
                // compare all of a's extras to bfor (String key : a.getExtras().keySet()) {
                    if (!b.getExtras().containsKey(key)) {
                        returnfalse;
                    }
                    elseif (a.getExtras().get(key).getClass().isArray() && b.getExtras().get(key).getClass().isArray()) {
                        if (!Arrays.equals((Object[]) a.getExtras().get(key), (Object[]) b.getExtras().get(key))) {
                            returnfalse;
                        }
                    }
                    elseif (!a.getExtras().get(key).equals(b.getExtras().get(key))) {
                        returnfalse;
                    }
                }
                // compare all of b's extras to afor (String key : b.getExtras().keySet()) {
                    if (!a.getExtras().containsKey(key)) {
                        returnfalse;
                    }
                    elseif (b.getExtras().get(key).getClass().isArray() && a.getExtras().get(key).getClass().isArray()) {
                        if (!Arrays.equals((Object[]) b.getExtras().get(key), (Object[]) a.getExtras().get(key))) {
                            returnfalse;
                        }
                    }
                    elseif (!b.getExtras().get(key).equals(a.getExtras().get(key))) {
                        returnfalse;
                    }
                }
                returntrue;
            }
            if (a.getExtras() == null && b.getExtras() == null)
            {
                returntrue;
            }
            // either a has extras and b doesn't or b has extras and a doesn'treturnfalse;
        }
        else
        {
            returnfalse;
        }
    }

Solution 3:

This is pretty much an implementation of what CommonsWare suggested combined with Ben's code but also covers the case where either a has extras and b does not or b has extras and a does not.

private boolean areEqual(Intent a, Intent b) {
    if (a.filterEquals(b)) {
        if (a.getExtras() != null && b.getExtras() != null) {
            // check if the keysets are the same sizeif (a.getExtras().keySet().size() != b.getExtras().keySet().size()) returnfalse;
            // compare all of a's extras to bfor (String key : a.getExtras().keySet()) {
                if (!b.getExtras().containsKey(key)) {
                    returnfalse;
                } elseif (!a.getExtras().get(key).equals(b.getExtras().get(key))) {
                    returnfalse;
                }
            }
            // compare all of b's extras to afor (String key : b.getExtras().keySet()) {
                if (!a.getExtras().containsKey(key)) {
                    returnfalse;
                } elseif (!b.getExtras().get(key).equals(a.getExtras().get(key))) {
                    returnfalse;
                }
            }
        }
        if (a.getExtras() == null && b.getExtras() == null) returntrue;
        // either a has extras and b doesn't or b has extras and a doesn'treturnfalse;
    } else {
        returnfalse;
    }
}

Post a Comment for "Comparing Android Intent Objects"