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

Use visitor for json creation of locations #170

Merged
merged 13 commits into from
Apr 19, 2023

Conversation

nimakarimipour
Copy link
Member

@nimakarimipour nimakarimipour commented Apr 4, 2023

This PR updates the logic for json creation from Location instances using the visitor pattern architecture added in #169

@nimakarimipour nimakarimipour added the refactoring/simplification Refactoring Simplification label Apr 4, 2023
@nimakarimipour nimakarimipour self-assigned this Apr 4, 2023
Comment on lines 46 to 47
/** A singleton instance of this visitor. */
public static final LocationToJsonVisitor INSTANCE = new LocationToJsonVisitor();
Copy link
Member

Choose a reason for hiding this comment

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

Why make this a singleton? Since there is no state in the object, and allocating the object should be cheap, I think this is overkill / premature optimization.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thank you for noticing this. I thought since there is not state, we should make it singleton. But as you said, this is cheap and not a good optimization overall. I fixed it here af081c1.

nimakarimipour added a commit that referenced this pull request Apr 19, 2023
This PR adds the appropriate architecture and API to apply a visitor for `Location` instances. This is preparation for the upcoming refactoring. #170 and #171
Base automatically changed from nimak/add-visitor-loc to master April 19, 2023 19:58
@nimakarimipour nimakarimipour requested a review from msridhar April 19, 2023 21:07
Copy link
Member

@msridhar msridhar left a comment

Choose a reason for hiding this comment

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

couple more minor comments

INDEX
}

@SuppressWarnings("unchecked")
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need these unchecked suppressions?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is required for putting object of any type in JSON. The JSONObject implements raw type of Map and every call to put is risky in view of javac. The only way to resolve this warning is to change the json dependency to org.json from com.googlecode.json-simple:json-simple since it is compiled with a very old javac (the latest release is for 2012). We have an open PR that is not landed yet as it has a low priority #123 that replaces the dependency with org.json and can get rid of all @SuppressWarnings("unchecked").

Comment on lines 90 to 92
public JSONObject visit(Location location) {
return location.accept(this, null);
}
Copy link
Member

Choose a reason for hiding this comment

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

This method is unnecessary, right? At any caller you could just call Location.accept directly with a new visitor? I think that is standard and we can remove this method

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes exactly, removed it 6f245da.

@nimakarimipour nimakarimipour requested a review from msridhar April 19, 2023 21:50
@nimakarimipour nimakarimipour merged commit 4e6d8f5 into master Apr 19, 2023
@nimakarimipour nimakarimipour deleted the nimak/visitor-for-json branch April 19, 2023 23:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactoring/simplification Refactoring Simplification
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants