equals between two Bundle - Android Android OS

Android examples for Android OS:Bundle

Description

equals between two Bundle

Demo Code


import android.os.Bundle;
import android.os.Parcel;
import java.util.Arrays;
import java.util.Set;

public class Main{
    public static boolean equals(Bundle left, Bundle right) {

        if (left == right) {
            return true;
        }/*from w ww.j  a va 2 s.c  o  m*/

        if (left == null || right == null) {
            return false;
        }

        final Set<String> leftSet = left.keySet();
        final Set<String> rightSet = right.keySet();

        if (leftSet.size() != rightSet.size()) {
            return false;
        }

        Object leftValue;
        Object rightValue;

        for (String key : leftSet) {

            if (!rightSet.contains(key)) {
                return false;
            }

            leftValue = left.get(key);
            rightValue = right.get(key);

            if (leftValue instanceof Bundle) {

                if (!(rightValue instanceof Bundle)) {
                    return false;
                }

                if (!BundleUtils.equals((Bundle) leftValue,
                        (Bundle) rightValue)) {
                    return false;
                }
            }

            if (leftValue != null ? !leftValue.equals(rightValue)
                    : rightValue != null) {
                return false;
            }

        }

        return true;
    }
}

Related Tutorials