diff options
author | Jisi Liu <jisi.liu@gmail.com> | 2015-02-06 15:30:48 -0800 |
---|---|---|
committer | Jisi Liu <jisi.liu@gmail.com> | 2015-02-06 15:30:48 -0800 |
commit | 1536e93349b0f8e703060e5efddb91169c93786d (patch) | |
tree | fd45bd2fea6e970e7157eee439c98e92cfeff7df /javanano/src/main | |
parent | b0f194885e79960210dab17c6bc68674b1eab764 (diff) | |
download | protobuf-1536e93349b0f8e703060e5efddb91169c93786d.tar.gz protobuf-1536e93349b0f8e703060e5efddb91169c93786d.tar.bz2 protobuf-1536e93349b0f8e703060e5efddb91169c93786d.zip |
Implement Equals for nano.
Diffstat (limited to 'javanano/src/main')
-rw-r--r-- | javanano/src/main/java/com/google/protobuf/nano/InternalNano.java | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java b/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java index a9a459dd..044c30dd 100644 --- a/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java +++ b/javanano/src/main/java/com/google/protobuf/nano/InternalNano.java @@ -491,4 +491,44 @@ public final class InternalNano { } return size; } + + /** + * Checks whether two {@link Map} are equal. We don't use the default equals + * method of {@link Map} because it compares by identity not by content for + * byte arrays. + */ + public static <K, V> boolean equals(Map<K, V> a, Map<K, V> b) { + if (a == b) { + return true; + } + if (a == null) { + return b.size() == 0; + } + if (b == null) { + return a.size() == 0; + } + if (a.size() != b.size()) { + return false; + } + for (Entry<K, V> entry : a.entrySet()) { + if (!b.containsKey(entry.getKey())) { + return false; + } + if (!equalsMapValue(entry.getValue(), b.get(entry.getKey()))) { + return false; + } + } + return true; + } + + private static boolean equalsMapValue(Object a, Object b) { + if (a == null || b == null) { + throw new IllegalStateException( + "keys and values in maps cannot be null"); + } + if (a instanceof byte[] && b instanceof byte[]) { + return Arrays.equals((byte[]) a, (byte[]) b); + } + return a.equals(b); + } } |