jenkins-bot has submitted this change and it was merged. Change subject: Hygiene: change some if statements to switch statements. ......................................................................
Hygiene: change some if statements to switch statements. JDK 7 added the ability to do switch statements on Strings. Since we're now using JDK 7, this patch changes some particularly convoluted if statements which operate on Strings to use switch statements instead, improving code readability and probably slightly improving performance. Change-Id: I36202c34f48b21347fa761e358dc0014ef3e9ca6 --- M wikipedia/src/main/java/org/wikipedia/Utils.java M wikipedia/src/main/java/org/wikipedia/createaccount/CreateAccountActivity.java M wikipedia/src/main/java/org/wikipedia/createaccount/CreateAccountTask.java M wikipedia/src/main/java/org/wikipedia/login/LoginActivity.java 4 files changed, 75 insertions(+), 57 deletions(-) Approvals: BearND: Looks good to me, approved jenkins-bot: Verified diff --git a/wikipedia/src/main/java/org/wikipedia/Utils.java b/wikipedia/src/main/java/org/wikipedia/Utils.java index b93ebaf..c4b11a2 100644 --- a/wikipedia/src/main/java/org/wikipedia/Utils.java +++ b/wikipedia/src/main/java/org/wikipedia/Utils.java @@ -308,12 +308,13 @@ public static String langCodeToWikiLang(String langCode) { // Convert deprecated language codes to modern ones. // See https://developer.android.com/reference/java/util/Locale.html - if (langCode.equals("iw")) { - return "he"; // Hebrew - } else if (langCode.equals("in")) { - return "id"; // Indonesian - } else if (langCode.equals("ji")) { - return "yi"; // Yiddish + switch (langCode) { + case "iw": + return "he"; // Hebrew + case "in": + return "id"; // Indonesian + case "ji": + return "yi"; // Yiddish } return langCode; diff --git a/wikipedia/src/main/java/org/wikipedia/createaccount/CreateAccountActivity.java b/wikipedia/src/main/java/org/wikipedia/createaccount/CreateAccountActivity.java index 67497b4..2b38650 100644 --- a/wikipedia/src/main/java/org/wikipedia/createaccount/CreateAccountActivity.java +++ b/wikipedia/src/main/java/org/wikipedia/createaccount/CreateAccountActivity.java @@ -201,31 +201,41 @@ public void handleError(CreateAccountResult result) { String errorCode = result.getResult(); - if (errorCode.equals("blocked")) { - if (app.getUserInfoStorage().isLoggedIn()) { - Crouton.makeText(this, R.string.create_account_blocked_error, Style.ALERT).show(); - } else { - Crouton.makeText(this, R.string.create_account_blocked_anon_error, Style.ALERT).show(); - } - } else if (errorCode.equals("acct_creation_throttle_hit")) { - Crouton.makeText(this, R.string.create_account_ip_throttle_error, Style.ALERT).show(); - } else if (errorCode.equals("sorbs_create_account_reason")) { - Crouton.makeText(this, R.string.create_account_open_proxy_error, Style.ALERT).show(); - } else if (errorCode.equals("userexists")) { - //Request focus before displaying error message, so that it pops up on its own - usernameEdit.requestFocus(); - usernameEdit.setError(getString(R.string.create_account_username_exists_error)); - } else if (errorCode.equals("noname")) { - Crouton.makeText(this, R.string.create_account_noname_error, Style.ALERT).show(); - } else if (errorCode.equals("invalidemailaddress")) { - Crouton.makeText(this, R.string.create_account_invalid_email_error, Style.ALERT).show(); - } else if (errorCode.equals("passwordtooshort")) { - //FIXME: Find the value of $wgMinimalPasswordLength and tell the user the minimum pwd length - Crouton.makeText(this, R.string.create_account_password_too_short_error, Style.ALERT).show(); - } else if (errorCode.equals("password-name-match")) { - Crouton.makeText(this, R.string.create_account_password_name_match_error, Style.ALERT).show(); - } else { - Crouton.makeText(this, R.string.create_account_generic_error, Style.ALERT).show(); + switch (errorCode) { + case "blocked": + if (app.getUserInfoStorage().isLoggedIn()) { + Crouton.makeText(this, R.string.create_account_blocked_error, Style.ALERT).show(); + } else { + Crouton.makeText(this, R.string.create_account_blocked_anon_error, Style.ALERT).show(); + } + break; + case "acct_creation_throttle_hit": + Crouton.makeText(this, R.string.create_account_ip_throttle_error, Style.ALERT).show(); + break; + case "sorbs_create_account_reason": + Crouton.makeText(this, R.string.create_account_open_proxy_error, Style.ALERT).show(); + break; + case "userexists": + //Request focus before displaying error message, so that it pops up on its own + usernameEdit.requestFocus(); + usernameEdit.setError(getString(R.string.create_account_username_exists_error)); + break; + case "noname": + Crouton.makeText(this, R.string.create_account_noname_error, Style.ALERT).show(); + break; + case "invalidemailaddress": + Crouton.makeText(this, R.string.create_account_invalid_email_error, Style.ALERT).show(); + break; + case "passwordtooshort": + //FIXME: Find the value of $wgMinimalPasswordLength and tell the user the minimum pwd length + Crouton.makeText(this, R.string.create_account_password_too_short_error, Style.ALERT).show(); + break; + case "password-name-match": + Crouton.makeText(this, R.string.create_account_password_name_match_error, Style.ALERT).show(); + break; + default: + Crouton.makeText(this, R.string.create_account_generic_error, Style.ALERT).show(); + break; } } @@ -320,4 +330,4 @@ } super.onStop(); } -} \ No newline at end of file +} diff --git a/wikipedia/src/main/java/org/wikipedia/createaccount/CreateAccountTask.java b/wikipedia/src/main/java/org/wikipedia/createaccount/CreateAccountTask.java index 3a14641..c971037 100644 --- a/wikipedia/src/main/java/org/wikipedia/createaccount/CreateAccountTask.java +++ b/wikipedia/src/main/java/org/wikipedia/createaccount/CreateAccountTask.java @@ -51,17 +51,18 @@ } JSONObject ca = result.asObject().optJSONObject("createaccount"); String apiResult = ca.optString("result"); - if (apiResult.equals("NeedToken")) { - // We need to just repeat the request. - // Set token and restart the request - token = ca.optString("token"); - return performTask(); - } else if (apiResult.equals("NeedCaptcha")) { - return new CreateAccountCaptchaResult(new CaptchaResult(ca.optJSONObject("captcha").optString("id"))); - } else if (apiResult.equals("Success")) { - return new CreateAccountSuccessResult(ca.optString("username")); - } else { - return new CreateAccountResult(apiResult); + switch (apiResult) { + case "NeedToken": + // We need to just repeat the request. + // Set token and restart the request + token = ca.optString("token"); + return performTask(); + case "NeedCaptcha": + return new CreateAccountCaptchaResult(new CaptchaResult(ca.optJSONObject("captcha").optString("id"))); + case "Success": + return new CreateAccountSuccessResult(ca.optString("username")); + default: + return new CreateAccountResult(apiResult); } } } diff --git a/wikipedia/src/main/java/org/wikipedia/login/LoginActivity.java b/wikipedia/src/main/java/org/wikipedia/login/LoginActivity.java index bab497f..6c4bd81 100644 --- a/wikipedia/src/main/java/org/wikipedia/login/LoginActivity.java +++ b/wikipedia/src/main/java/org/wikipedia/login/LoginActivity.java @@ -198,19 +198,25 @@ } private void handleError(String result) { - if (result.equals("WrongPass")) { - passwordText.requestFocus(); - passwordText.setError(getString(R.string.login_error_wrong_password)); - } else if (result.equals("NotExists")) { - usernameText.requestFocus(); - usernameText.setError(getString(R.string.login_error_wrong_username)); - } else if (result.equals("Blocked")) { - Crouton.makeText(this, R.string.login_error_blocked, Style.ALERT).show(); - } else if (result.equals("Throttled")) { - Crouton.makeText(this, R.string.login_error_throttled, Style.ALERT).show(); - } else { - Crouton.makeText(this, R.string.login_error_unknown, Style.ALERT).show(); - Log.d("Wikipedia", "Login failed with result " + result); + switch (result) { + case "WrongPass": + passwordText.requestFocus(); + passwordText.setError(getString(R.string.login_error_wrong_password)); + break; + case "NotExists": + usernameText.requestFocus(); + usernameText.setError(getString(R.string.login_error_wrong_username)); + break; + case "Blocked": + Crouton.makeText(this, R.string.login_error_blocked, Style.ALERT).show(); + break; + case "Throttled": + Crouton.makeText(this, R.string.login_error_throttled, Style.ALERT).show(); + break; + default: + Crouton.makeText(this, R.string.login_error_unknown, Style.ALERT).show(); + Log.d("Wikipedia", "Login failed with result " + result); + break; } } @@ -233,4 +239,4 @@ super.onSaveInstanceState(outState); outState.putBoolean("loginShowing", true); } -} \ No newline at end of file +} -- To view, visit https://gerrit.wikimedia.org/r/181692 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I36202c34f48b21347fa761e358dc0014ef3e9ca6 Gerrit-PatchSet: 3 Gerrit-Project: apps/android/wikipedia Gerrit-Branch: master Gerrit-Owner: Deskana <dga...@wikimedia.org> Gerrit-Reviewer: BearND <bsitzm...@wikimedia.org> Gerrit-Reviewer: Brion VIBBER <br...@wikimedia.org> Gerrit-Reviewer: Dbrant <dbr...@wikimedia.org> Gerrit-Reviewer: jenkins-bot <> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits