From 9d88b0af243e00f784f330e9e2c6899301f5ae97 Mon Sep 17 00:00:00 2001 From: Mikkel Nygaard Ravn Date: Sat, 22 Apr 2017 14:04:03 +0200 Subject: [PATCH] Add hasArgument convenience to MethodCall (flutter/engine#3612) --- .../io/flutter/plugin/common/MethodCall.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/MethodCall.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/MethodCall.java index b6a423aebb..b30ac2eef3 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/MethodCall.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/common/MethodCall.java @@ -19,9 +19,9 @@ public final class MethodCall { /** * Arguments for the call. * - * Consider using {@link #arguments()} for cases where a particular run-time type is expected. - * Consider using {@link #argument(String)} when that run-time type is a {@link Map} - * or a {@link JSONObject}. + *

Consider using {@link #arguments()} for cases where a particular run-time type is expected. + * Consider using {@link #argument(String)} when that run-time type is {@link Map} or + * {@link JSONObject}.

*/ public final Object arguments; @@ -72,4 +72,28 @@ public final class MethodCall { throw new ClassCastException(); } } + + /** + * Returns whether this method call involves a mapping for the given argument key, + * assuming {@link #arguments} is a {@link Map} or a {@link JSONObject}. The value associated + * with the key, as returned by {@link #argument(String)}, is not considered, and may be + * {@code null}. + * + * @param key the String key. + * @return {@code true}, if {@link #arguments} is a {@link Map} containing key, or a + * {@link JSONObject} with a mapping for key. + * @throws ClassCastException if {@link #arguments} can be cast to neither {@link Map} nor + * {@link JSONObject}. + */ + public boolean hasArgument(String key) { + if (arguments == null) { + return false; + } else if (arguments instanceof Map) { + return ((Map) arguments).containsKey(key); + } else if (arguments instanceof JSONObject) { + return ((JSONObject) arguments).has(key); + } else { + throw new ClassCastException(); + } + } }