Skip to content
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

Fix duplicate toUpperCase() calls, use Locale.ROOT #158

Open
wants to merge 11 commits into
base: main
Choose a base branch
from

Conversation

nyurik
Copy link
Contributor

@nyurik nyurik commented Apr 28, 2018

No description provided.

@nyurik
Copy link
Contributor Author

nyurik commented Apr 29, 2018

@Supersuz what do you mean? Your comment is a bit confusing :)

@nyurik
Copy link
Contributor Author

nyurik commented Apr 29, 2018

@shoeper I am not able to merge the changes in this repo. Either you can set up so that I can self-merge after approvals, or you need to merge it yourself. Thx!

P.S. I think all toUpperCase() should be changed to toUpperCase(Locale.ROOT) because some tools may complain about locale ambiguity (even though in this case it shouldn't matter, ROOT makes it clear)

While technically this might not be needed in this specific case,
it is a good practice to be explicit about the locale.
@nyurik
Copy link
Contributor Author

nyurik commented May 5, 2018

@Supersuz or @shoeper - are there any other people that need to review this code, or can you merge it? Thx!

@nyurik nyurik changed the title Remove duplicate toUpperCase() calls Fix duplicate toUpperCase() calls, use Locale.ROOT May 5, 2018
@shoeper
Copy link

shoeper commented May 5, 2018

Sorry, I don't really know.

I cannot merge it.

@nyurik
Copy link
Contributor Author

nyurik commented May 5, 2018

Looking at commit history, @drinckes and @JonMcPherson merged things recently.

@@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you wanted to remove all redundant calls of toUpperCase(), it could be modified to something like this

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 !isValidCodeUpperCase(newCode).
Just a minor improvement...

Copy link
Contributor

Choose a reason for hiding this comment

The 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().

@JonMcPherson
Copy link
Contributor

Also, @drinckes is who you want for merging

@shoeper
Copy link

shoeper commented May 29, 2018

Maybe @zongweil can help.

@zongweil
Copy link
Contributor

I'll take a look within the next couple days.

Copy link
Contributor

@zongweil zongweil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Please pull the latest changes and address these comments.

@@ -153,11 +154,12 @@ public double getEastLongitude() {
* @constructor
*/
public OpenLocationCode(String code) throws IllegalArgumentException {
if (!isValidCode(code.toUpperCase())) {
Copy link
Contributor

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().

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?

Copy link
Member

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?

@@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The 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().

java/com/google/openlocationcode/OpenLocationCode.java Outdated Show resolved Hide resolved
@google google deleted a comment from Sassysuz Apr 21, 2019
This updates your version of OpenLocationCode.java to the most recent upstream version while keeping your changes.
Update to recent version while keeping changes
@google-cla google-cla bot added the cla: yes label May 21, 2021
# Conflicts:
#	java/src/main/java/com/google/openlocationcode/OpenLocationCode.java
@nyurik
Copy link
Contributor Author

nyurik commented May 21, 2021

@zongweil I merged the code and applied your suggestion. See 92e2e40

@nyurik
Copy link
Contributor Author

nyurik commented May 21, 2021

I have done another pass at minor optimizations -- the code now has a toValidCode which returns normalized code or null if it fails. This way constructor can use it directly, without doing duplicate toUpperCase.

@nyurik nyurik requested a review from zongweil June 1, 2021 12:49
nyurik and others added 2 commits November 18, 2021 16:25
…e.java

Co-authored-by: Sonya Alexandrova <sonyaa@users.noreply.github.com>
* @param code The code to check and normalize.
* @return Normalized code if it is a valid full or short code, null otherwise.
*/
private static String toValidCode(String code) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think returning an Optional instead of using null to signify invalid would be slightly cleaner here, what do you think?

@@ -153,11 +154,12 @@ public double getEastLongitude() {
* @constructor
*/
public OpenLocationCode(String code) throws IllegalArgumentException {
if (!isValidCode(code.toUpperCase())) {
Copy link
Member

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants