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

Implement LinkedMap class #124

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

Conversation

sweep-ai[bot]
Copy link

@sweep-ai sweep-ai bot commented Oct 19, 2023

PR Feedback (click)

  • 👍 Sweep Did Well
  • 👎 Sweep Needs Improvement

Description

This PR implements a new class called LinkedMap that provides a map-like data structure with a set of keys and a list of values that are linked. The class adheres to the limitations of an Apex defined type.

Summary of Changes

  • Created a new class LinkedMap.cls in the force-app/main/default/classes/ directory.
  • Added a private variable linkedMap of type Map<Set<String>, List<Object>> to store the key-value pairs.
  • Implemented methods put, get, remove, containsKey, keySet, and values in the LinkedMap.cls class to provide functionality for adding, retrieving, removing, checking, and retrieving all keys and values in the map.
  • Created a new test class LinkedMapTests.cls in the force-app/main/default/classes/ directory to cover all methods in the LinkedMap.cls class with comprehensive test cases.

Please review and merge this PR to incorporate the new LinkedMap class into the repository.

Fixes #123.


🎉 Latest improvements to Sweep:

  • Sweep can now passively improve your repository! Check out Rules to learn more.

💡 To get Sweep to edit this pull request, you can:

  • Comment below, and Sweep can edit the entire PR
  • Comment on a file, Sweep will only modify the commented file
  • Edit the original issue to get Sweep to recreate the PR from scratch

Summary by CodeRabbit

  • New Feature: Introduced a LinkedMap class that provides a map-like data structure with linked keys and values. This feature enhances data handling by allowing users to add, retrieve, remove, and check elements in the map more efficiently.
  • Test: Added LinkedMapTests class to ensure the robustness of the new LinkedMap class. The tests cover all key functionalities including adding, retrieving, removing, and checking elements in the map.

@sweep-ai
Copy link
Author

sweep-ai bot commented Oct 19, 2023

Rollback Files For Sweep

  • Rollback changes to force-app/main/default/classes/LinkedMapTests.cls
  • Rollback changes to force-app/main/default/classes/LinkedMap.cls

@sweep-ai
Copy link
Author

sweep-ai bot commented Oct 19, 2023

Apply Sweep Rules to your PR?

  • Apply: Leftover TODOs in the code should be handled.
  • Apply: All new business logic should have corresponding unit tests in the tests/ directory.
  • Apply: Any clearly inefficient or repeated code should be optimized or refactored.

@github-actions
Copy link

github-actions bot commented Oct 19, 2023

Image description CodeRabbit

Walkthrough

This changeset introduces a new class LinkedMap that mimics the behavior of a map with linked keys and values. It provides methods for adding, retrieving, removing, and checking elements in the map. To ensure the functionality of these methods, a test class LinkedMapTests is also introduced with comprehensive test cases.

Changes

File Summary
force-app/main/default/classes/LinkedMap.cls Introduces a new class LinkedMap implementing a map-like data structure with linked keys and values. Includes methods for adding, retrieving, removing, checking, and retrieving all keys and values in the map.
force-app/main/default/classes/LinkedMapTests.cls Adds a test class LinkedMapTests to cover the methods in LinkedMap with comprehensive test cases. The test cases verify the functionality of the put, get, remove, containsKey, keySet, and values methods.

🐇💻

In the land of code, where logic intertwines,

A LinkedMap was born, linking keys and lines.

With tests by its side, it stands tall and bright,

Ensuring each function performs just right.

So let's celebrate this change, for it's no small feat,

In the world of software, it's quite an elite! 🎉🥳


Uplevel your code reviews with CodeRabbit Pro

CodeRabbit Pro

If you like this project, please support us by purchasing the Pro version. The Pro version has advanced context, superior noise reduction and several proprietary improvements compared to the open source version. Moreover, CodeRabbit Pro is free for open source projects.

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Image description CodeRabbit

Commits Files that changed from the base of the PR and between b36452f and cddce6e commits.
Files selected (2)
  • force-app/main/default/classes/LinkedMap.cls (1)
  • force-app/main/default/classes/LinkedMapTests.cls (1)
Files skipped from review due to trivial changes (1)
  • force-app/main/default/classes/LinkedMapTests.cls
Review comments generated (6)
  • Review: 3
  • LGTM: 3

Tips

Chat with Image description CodeRabbit Bot (@coderabbitai)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @coderabbitai in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Pausing incremental reviews

  • Add @coderabbitai: ignore anywhere in the PR description to pause further reviews from the bot.

Comment on lines 16 to 18
public List<Object> get(Set<String> keys) {
return this.linkedMap.get(keys);
}

Choose a reason for hiding this comment

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

Image description CodeRabbit

The get method returns null when the keys are not found in the map. It would be better to return an empty list instead to avoid potential NullPointerExceptions.

    public List<Object> get(Set<String> keys) {
-       return this.linkedMap.get(keys);
+       return this.linkedMap.getOrDefault(keys, new List<Object>());
    }

Copy link
Collaborator

Choose a reason for hiding this comment

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

This isn't a thing.
Want to put a null-safe operator @codefriar ?

Copy link
Author

@sweep-ai sweep-ai bot Oct 28, 2023

Choose a reason for hiding this comment

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

🚀 Wrote Changes

Done.

Comment on lines +20 to +22
public List<Object> remove(Set<String> keys) {
return this.linkedMap.remove(keys);
}

Choose a reason for hiding this comment

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

Image description CodeRabbit

Similar to the get method, the remove method also returns null when the keys are not found in the map. Consider returning an empty list instead.

    public List<Object> remove(Set<String> keys) {
-       return this.linkedMap.remove(keys);
+       return this.linkedMap.remove(keys) != null ? this.linkedMap.remove(keys) : new List<Object>();
    }

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

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

Image description CodeRabbit

Commits Files that changed from the base of the PR and between cddce6e and 1dbe37c commits.
Files selected (1)
  • force-app/main/default/classes/LinkedMap.cls (1)
Review comments generated (8)
  • Review: 2
  • LGTM: 6

Tips

Chat with Image description CodeRabbit Bot (@coderabbitai)

  • Reply on review comments left by this bot to ask follow-up questions. A review comment is a comment on a diff or a file.
  • Invite the bot into a review comment chain by tagging @coderabbitai in a reply.

Code suggestions

  • The bot may make code suggestions, but please review them carefully before committing since the line number ranges may be misaligned.
  • You can edit the comment made by the bot and manually tweak the suggestion if it is slightly off.

Pausing incremental reviews

  • Add @coderabbitai: ignore anywhere in the PR description to pause further reviews from the bot.

Comment on lines +8 to +14
public void put(Set<String> keys, List<Object> values) {
if (this.linkedMap.containsKey(keys)) {
this.linkedMap.get(keys).addAll(values);
} else {
this.linkedMap.put(keys, values);
}
}

Choose a reason for hiding this comment

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

Image description CodeRabbit

The put method currently allows for duplicate values to be added to the list of values for a given key set. If this is not the intended behavior and you want to prevent duplicates, consider using a Set<Object> instead of a List<Object>. However, if maintaining the order of insertion is important, then the current implementation is fine.

Comment on lines +32 to +34
public List<List<Object>> values() {
return this.linkedMap.values();
}

Choose a reason for hiding this comment

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

Image description CodeRabbit

In the values() method, you are returning the original list of values from the map. This could potentially allow outside code to modify the internal state of your LinkedMap object. To prevent this, consider returning a deep copy of the values.

- public List<List<Object>> values() {
-     return this.linkedMap.values();
- }
+ public List<List<Object>> values() {
+     List<List<Object>> deepCopyValues = new List<List<Object>>();
+     for (List<Object> valueList : this.linkedMap.values()) {
+         deepCopyValues.add(new List<Object>(valueList));
+     }
+     return deepCopyValues;
+ }

Comment on lines +1 to +35
public class LinkedMap {
private Map<Set<String>, List<Object>> linkedMap;

public LinkedMap() {
this.linkedMap = new Map<Set<String>, List<Object>>();
}

public void put(Set<String> keys, List<Object> values) {
if (this.linkedMap.containsKey(keys)) {
this.linkedMap.get(keys).addAll(values);
} else {
this.linkedMap.put(keys, values);
}
}

public List<Object> get(Set<String> keys) {
return this.linkedMap.containsKey(keys) ? this.linkedMap.get(keys) : new List<Object>();
}

public List<Object> remove(Set<String> keys) {
return this.linkedMap.remove(keys);
}

public Boolean containsKey(Set<String> keys) {
return this.linkedMap.containsKey(keys);
}

public Set<Set<String>> keySet() {
return this.linkedMap.keySet();
}

public List<List<Object>> values() {
return this.linkedMap.values();
}
}

Check warning

Code scanning / PMD

Missing ApexDoc comment Warning

Missing ApexDoc comment
Comment on lines +4 to +6
public LinkedMap() {
this.linkedMap = new Map<Set<String>, List<Object>>();
}

Check warning

Code scanning / PMD

Missing ApexDoc comment Warning

Missing ApexDoc comment
Comment on lines +8 to +14
public void put(Set<String> keys, List<Object> values) {
if (this.linkedMap.containsKey(keys)) {
this.linkedMap.get(keys).addAll(values);
} else {
this.linkedMap.put(keys, values);
}
}

Check warning

Code scanning / PMD

Missing ApexDoc comment Warning

Missing ApexDoc comment
Comment on lines +16 to +18
public List<Object> get(Set<String> keys) {
return this.linkedMap.containsKey(keys) ? this.linkedMap.get(keys) : new List<Object>();
}

Check warning

Code scanning / PMD

Missing ApexDoc comment Warning

Missing ApexDoc comment
Comment on lines +20 to +22
public List<Object> remove(Set<String> keys) {
return this.linkedMap.remove(keys);
}

Check warning

Code scanning / PMD

Missing ApexDoc comment Warning

Missing ApexDoc comment
Comment on lines +24 to +26
public Boolean containsKey(Set<String> keys) {
return this.linkedMap.containsKey(keys);
}

Check warning

Code scanning / PMD

Missing ApexDoc comment Warning

Missing ApexDoc comment
Comment on lines +28 to +30
public Set<Set<String>> keySet() {
return this.linkedMap.keySet();
}

Check warning

Code scanning / PMD

Missing ApexDoc comment Warning

Missing ApexDoc comment
Comment on lines +32 to +34
public List<List<Object>> values() {
return this.linkedMap.values();
}

Check warning

Code scanning / PMD

Missing ApexDoc comment Warning

Missing ApexDoc comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sweep Assigns Sweep to an issue or pull request.
Projects
None yet
1 participant