-
Notifications
You must be signed in to change notification settings - Fork 508
Fix duplicate toUpperCase() calls, use Locale.ROOT #158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
85053db
803cdd5
1e8d0c3
9c8fc6e
15e7afa
92e2e40
9101f25
f0f5dc6
efe3cde
868ed71
731fdcd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
package com.google.openlocationcode; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Locale; | ||
|
||
/** | ||
* Convert locations to and from convenient short codes. | ||
|
@@ -153,11 +154,12 @@ public double getEastLongitude() { | |
* @constructor | ||
*/ | ||
public OpenLocationCode(String code) throws IllegalArgumentException { | ||
if (!isValidCode(code.toUpperCase())) { | ||
String newCode = code.toUpperCase(Locale.ROOT); | ||
if (!isValidCode(newCode)) { | ||
throw new IllegalArgumentException( | ||
"The provided code '" + code + "' is not a valid Open Location Code."); | ||
} | ||
this.code = code.toUpperCase(); | ||
this.code = newCode; | ||
phebehp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
/** | ||
|
@@ -496,7 +498,7 @@ public static boolean isValidCode(String code) { | |
if (code == null || code.length() < 2) { | ||
return false; | ||
} | ||
code = code.toUpperCase(); | ||
code = code.toUpperCase(Locale.ROOT); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you wanted to remove all redundant calls of public static boolean isValidCode(String code) {
return isValidCodeUpperCase(code.toUpperCase(Locale.ROOT));
}
private static boolean isValidCodeUpperCase(String code) {
// do validity checks without calling code.toUpperCase() redundantly
} And the constructor would instead call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for pointing out the redundant call. It's so redundant, in fact, that I think we can just remove the one in the constructor since the code will be cast to uppercase in isValidCode(). |
||
|
||
// There must be exactly one separator. | ||
int separatorPosition = code.indexOf(SEPARATOR); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can get rid of the code.toUpperCase() here all together, since we're calling it inside of isValidCode().
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since this is public, can we make that assumption and document it the constructor comments?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand your question here, can you rephrase? What assumption are you thinking we need to document?